Re: [RFC] JSON Schema validation support
| From: | Jakub Zelenka | Date: | Fri, 04 Jul 2025 22:11:25 +0000 |
| Subject: | Re: [RFC] JSON Schema validation support | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-127886@lists.php.net to get a copy of this message | ||
Hi,
On Fri, Jul 4, 2025 at 11:49 PM Valentin Udaltsov <
udaltsov.valentin@gmail.com> wrote:
> On Sta, 5 July 2025 at 00:04, Jakub Zelenka <bukka@php.net> wrote:
>
>> Hello,
>>
>> I would like introduce and open discussion for RFC proposing the addition
>> of JSON Schema validation support to JSON extension:
>>
>> https://wiki.php.net/rfc/json_schema_validation
>>
>> If this is successful, it should be just the first for further
>> improvements (see the future scope).
>>
>> Kind regards,
>>
>> Jakub
>>
>
> Hi, Jakub!
>
> I like the idea. I have a couple of initial thoughts:
>
> 1. Wouldn't it make sense to place all the new classes under the
>
Json
> namespace, similar to how it’s done in ext-random?
>
This would be widely inconsistent with the current interface / exception
class in the extension. The ext-random was a new extension so it's a
completely different case. I think it makes much more sense to keep the
extension consistent than introducing new interface.
> 2. If there’s no way to interact with the JsonSchema class,
> what purpose
> does it serve? Wouldn’t it be simpler to just extend
> json_decode() with
> an optional ?string $schema = null parameter?
>
There are couple of reasons for this:
- It allows re-using the same parsed schema without a need to parse it
multiple times for each decoding / validation - the class internally holds
parsed representation of the schema.
- We might want to introduce additional flags that will impact the parsing
(e.g. default version)
- It might allow a way to better cache schema between requests so it could
further improve performance in the future
- It will allow the possibility to get schema from other sources than
string - e.g. from PHP array or even automatic creation from the class
(that's why the JsonSchema is created using static method and not using
constructor). This might be especially useful for object mapping.
Kind regards,
Jakub