Is there any PHP library that validates a JSON object against a JSON Schema?
To determine if the JSON output is genuine, PHP includes a method called json_decode(), which was introduced in PHP 5.3. It is a PHP built-in function that is used to decode a JSON string. It creates a PHP variable from a JSON encoded text.
Overview. JSON Schema is a declarative language for validating the format and structure of a JSON Object. It allows us to specify the number of special primitives to describe exactly what a valid JSON Object will look like.
The simplest way to check if JSON is valid is to load the JSON into a JObject or JArray and then use the IsValid(JToken, JsonSchema) method with the JSON Schema. To get validation error messages, use the IsValid(JToken, JsonSchema, IList<String> ) or Validate(JToken, JsonSchema, ValidationEventHandler) overloads.
The additionalProperties keyword is used to control the handling of extra stuff, that is, properties whose names are not listed in the properties keyword or match any of the regular expressions in the patternProperties keyword. By default any additional properties are allowed.
I'm using Justin Rainbow's JSON Schema for PHP https://github.com/justinrainbow/json-schema and I'm quite happy about it.
It's a fork of http://jsonschemaphpv.sourceforge.net/
You can easily install it via Composer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With