mszewcz/php-json-schema-validator

JSON Schema Validator with draft-06 specification support

1.0.0 2017-11-02 03:07 UTC

This package is auto-updated.

Last update: 2024-04-16 08:19:29 UTC


README

JSON schema validator class, which provides validation of JSON files according to draft-06 specification, published on 2017-04-15.

Build Status Codacy Badge Codacy Badge

Contents

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:

{
    "require": {
        "mszewcz/php-json-schema-validator": ">=1.0"
    }
}

You can also clone or download this respository.

php-json-schema-validator meets PSR-4 autoloading standards. If using the Composer please include its autoloader file:

require_once 'vendor/autoload.php';

If you cloned or downloaded this repository, you will have to code your own PSR-4 style autoloader implementation.

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:

$schema = \json_decode($jsonSchemaDefinition, true);
$json   = \json_decode($jsonToValidate, true);

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

It also supports $ref element, so you can use in-json definitions and references without any problems.

Contributing

Contributions are welcome. Please send your contributions through GitHub pull requests

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.

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.

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.

License

php-json-schema-validator is licensed under the MIT License - see the LICENSE file for details.