php-json-schema-validator
JSON schema validator class, which provides validation of JSON files according to draft-06 specification, published on 2017-04-15., (*1)
, (*2)
Contents
, (*3)
Installation
If you use Composer to manage the dependencies simply add a dependency on mszewcz/php-json-schema-validator to your project's composer.json file. Here is a minimal example of a composer.json:, (*4)
{
"require": {
"mszewcz/php-json-schema-validator": ">=1.0"
}
}
You can also clone or download this respository., (*5)
php-json-schema-validator meets PSR-4 autoloading standards. If using the Composer please include its autoloader file:, (*6)
require_once 'vendor/autoload.php';
If you cloned or downloaded this repository, you will have to code your own PSR-4 style autoloader implementation., (*7)
, (*8)
Usage
require 'vendor/autoload.php';
try {
$utils = new MS\Json\Utils\Utils();
$schema = Utils::decode($jsonSchemaDefinition);
$json = Utils::decode($jsonToValidate);
$validator = new MS\Json\SchemaValidator\Validator($schema);
$result = $validator->validate($json);
} catch (\Exception $e) {
echo $e->getMessage();
}
If you don't want to use Utils class to decode JSONs, you should do that the following way:, (*9)
$schema = \json_decode($jsonSchemaDefinition, true);
$json = \json_decode($jsonToValidate, true);
, (*10)
Supported elements
php-json-schema-validator supports validation against:
* additionalItems
* additionalProperties
* allOf
* anyOf
* const
* contains
* dependencies
* enum
* exclusiveMaximum
* exclusiveMinimum
* format (date-time, email, host, ipv4, ipv6 & uri)
* items
* maximum
* minimum
* maxItems
* maxLength
* maxProperties
* minItems
* minLength
* minProperties
* multipleOf
* not
* oneOf
* pattern
* patternProperties
* properties
* propertyNames
* required
* type
* uniqueItems, (*11)
It also supports $ref element, so you can use in-json definitions and references without any problems., (*12)
, (*13)
Contributing
Contributions are welcome. Please send your contributions through GitHub pull requests, (*14)
Pull requests for bug fixes must be based on latest stable release from the master branch whereas pull requests for new features must be based on the developer branch., (*15)
Due to time constraints, I'm not always able to respond as quickly as I would like. If you feel you're waiting too long for merging your pull request please remind me here., (*16)
Coding standards
We follow PSR-2 coding style and PSR-4 autoloading standards. Be sure you're also following them before sending your pull request., (*17)
, (*18)
License
php-json-schema-validator is licensed under the MIT License - see the LICENSE file for details., (*19)