2017 © Pedro Peláez
 

library schemavalidator

SchemaValidator is a PHP implementation for validating JSON against a Schema (also a string), the JSON and Schema are both linted with https://github.com/cmpayments/jsonlint. This library is optimized for speed and performance.

image

cmpayments/schemavalidator

SchemaValidator is a PHP implementation for validating JSON against a Schema (also a string), the JSON and Schema are both linted with https://github.com/cmpayments/jsonlint. This library is optimized for speed and performance.

  • Thursday, February 2, 2017
  • by boywijnmaalen
  • Repository
  • 3 Watchers
  • 5 Stars
  • 1,287 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 2 Open issues
  • 12 Versions
  • 5 % Grown

The README.md

Schema Validator for PHP Build Status

License Latest Stable Version Scrutinizer Code Quality Total Downloads Reference Status, (*1)

SchemaValidator is a PHP implementation for validating JSON against a Schema (also a string), the JSON and Schema are both linted with https://github.com/cmpayments/jsonlint. This library is optimized for speed and performance., (*2)

Installation

For a quick install with Composer use:, (*3)

$ composer require cmpayments/schemavalidator

Schema Validator for PHP can easily be used within another app if you have a PSR-4 autoloader, or it can be installed through Composer., (*4)

Usage

// use the required classes

use CMPayments\Cache\Cache;
use CMPayments\Json\Json;
use CMPayments\SchemaValidator\SchemaValidator;;

Example #1 - simple; validate JSON input for syntax errors

$data = '{"testProperty": {"length": 7.0, "superfluousProperty": 12.0}}';

// the constructor only accepts Strings as input
$json = new Json($data);

// validate() returns a boolean whether the $data input is valid JSON or not
$isValid = $json->validate(); // $isValid = true since we only want to know if $data is valid JSON or not (we are not validating any schema's yet)

var_dump('Example 1:', $isValid); // true

Example #2 - advanced; validate JSON input for syntax errors and validate the JSON against a schema

$data   = '{"testProperty": {"length": 7, "superfluousProperty": 12.0}}';
$schema = '{"type": "object","properties": {"testProperty": {"type": "object","properties": {"length": {"type": "number"}}}},"additionalProperties": true}';

$json = new Json($data);

// validate()' first argument only accepts Strings , the second parameter is a passthru parameter which picks up error along the way (if any)
$isValid = $json->validate($schema, $errors);

if ($isValid) { // $isValid = true since additional properties are now allowed

    // Get the decoded version of the $input (which was a String when inputted)
    var_dump('Example 2:', $json->getDecodedJSON()); // returns:

//    object(stdClass)[11]
//      public 'testProperty' =>
//        object(stdClass)[10]
//          public 'length' => float 7
//          public 'superfluousProperty' => float 12
} else {

    // in case $isValid should be false, $errors is the passthru variable and it now contains an array with all the errors that occurred.
    var_dump($errors);
}

// example data for example 3 & 4
$data   = '{"testProperty": {"length": 7.0, "superfluousProperty": 12.0}}';
$schema = '{"type": "object","properties": {"testProperty": {"type": "object","properties": {"length": {"type": "number"}}}},"additionalProperties": false}';

Example #3 - Simple SchemaValidator example; if you are not interested if the JSON (string) is valid or not (since the input is an object)

// SchemaValidator constructor:
// first argument is the JSON input and only accepts it when it is an object (mandatory)
// second argument is the Schema input and only accepts it when it is an object (mandatory)
// third argument must be an instance of Cache() (optional)
try {

    $validator = new SchemaValidator(json_decode($data), json_decode($schema));

    if (!$validator->isValid()) { // returns false again additional properties are not allowed

        var_dump('Example 3:', $validator->getErrors()); // returns:

//        array(size = 1)
//          0 =>
//            array(size = 3)
//              'code' => int 101
//              'args' => string '/testProperty/superfluousProperty' (length = 33)
//              'message' => string 'The Data property ' / testProperty / superfluousProperty' is not an allowed property' (length = 80)
    }
} catch (\Exception $e) {

    var_dump('Example 3:', $e->getMessage());
}

Example #4 - Advanced SchemaValidator example; if you are not interested if the JSON (string) is valid or not (since the input is an object) but you want to specify some caching options

try {

    // create new Cache object
    $cache = new Cache();

    // There are currently 2 cache options that can be set
    // 'debug' (boolean), if true you'll be notified if your cache directory is writable or not (any validation will be considered false when the cache directory is not writable and debug is true).
    // 'directory' (string), the absolute location where the cached schema's should be stored, by default this is '/src/CMPayments/SchemaValidator/cache/'
    $cache->setOptions(['debug' => true, 'directory' => 'absolute/path/to/your/cache/directory']); // currently this does not exist yet

    $validator = new SchemaValidator(json_decode($data), json_decode($schema), $cache);

    if (!$validator->isValid()) { // returns false again additional properties are not allowed but firstly because the current cache directory 'absolute/path/to/your/cache/directory' is not writable (since it doesn't exist).

        var_dump('Example 4:', (!$validator->isValid() ? 'false' : 'true'));
        //var_dump($validator->getErrors());
    }
} catch (\Exception $e) {

    var_dump('Example 4: ', $validator->isValid(), $e->getMessage()); // false, The cache directory 'absolute/path/to/your/cache/directory' is not writable
}

Other examples

Example; test input when input is an array

try {

    $data   = ["length" => 7.0, "superfluousProperty" => 12.0];
    $schema = '{"type": "array","items": {"type": "number"}}';

    $validator = new SchemaValidator((($data)), json_decode($schema));

    var_dump('Example 5:', $validator->isValid()); // true
    //var_dump($validator->getErrors());
} catch (\Exception $e) {

    var_dump('Example 5', $e->getMessage());
}

Example; test input when input is a boolean

try {

    $data   = true;
    $schema = '{"type": "boolean"}';

    $validator = new SchemaValidator((($data)), json_decode($schema));

    var_dump('Example 6:', $validator->isValid());  // true
//    var_dump($validator->getErrors());
} catch (\Exception $e) {

    var_dump('Example 6:', $e->getMessage());
}

Example; test input when input is a number (float)

try {

    $data   = 1.4;
    $schema = '{"type": "boolean"}';

    $validator = new SchemaValidator((($data)), json_decode($schema));

    var_dump('Example 7:', $validator->isValid());  // false
    var_dump($validator->getErrors()); // message reads: The Data property '/' needs to be a 'boolean' but got a 'number' (with value '1.4')
} catch (\Exception $e) {

    var_dump('Example 7:', $e->getMessage());
}

Example; test input when input is a number (integer)

try {

    $data   = 22;
    $schema = '{"type": "number"}';

    $validator = new SchemaValidator((($data)), json_decode($schema));

    var_dump('Example 8:', $validator->isValid());  // true
    //var_dump($validator->getErrors());
} catch (\Exception $e) {

    var_dump('Example 8:', $e->getMessage());
}

Example; test input when input is a string

try {

    $data   = 'test12345';
    $schema = '{"type": "string"}';

    $validator = new SchemaValidator((($data)), json_decode($schema));

    var_dump('Example 9:', $validator->isValid());  // true
//    var_dump($validator->getErrors());
} catch (\Exception $e) {

    var_dump('Example 9:', $e->getMessage());
}

// Example; simple one-line example
$isValid = (new Json('true'))->validate(null, $errors);
(var_dump('Example 10: ', $isValid, $errors));

Requirements

  • PHP 5.4+
  • [optional] PHPUnit 3.5+ to execute the test suite (phpunit --version)

Submitting bugs and feature requests

Bugs and feature request are tracked on GitHub, (*5)

Todo

Author

Boy Wijnmaalen - boy.wijnmaalen@cmtelecom.com - https://twitter.com/boywijnmaalen, (*6)

License

Schema Validator is licensed under the MIT License - see the LICENSE file for details, (*7)

The Versions

02/02 2017

dev-master

9999999-dev https://github.com/cmpayments/schemavalidator

SchemaValidator is a PHP implementation for validating JSON against a Schema (also a string), the JSON and Schema are both linted with https://github.com/cmpayments/jsonlint. This library is optimized for speed and performance.

  Sources   Download

MIT

The Requires

 

parser lint php json validator validation syntax linter syntaxcheck schemavalidation

02/02 2017

v0.1.1

0.1.1.0 https://github.com/cmpayments/schemavalidator

SchemaValidator is a PHP implementation for validating JSON against a Schema (also a string), the JSON and Schema are both linted with https://github.com/cmpayments/jsonlint. This library is optimized for speed and performance.

  Sources   Download

MIT

The Requires

 

parser lint php json validator validation syntax linter syntaxcheck schemavalidation

02/02 2017

dev-rfc.5.21

dev-rfc.5.21 https://github.com/cmpayments/schemavalidator

SchemaValidator is a PHP implementation for validating JSON against a Schema (also a string), the JSON and Schema are both linted with https://github.com/cmpayments/jsonlint. This library is optimized for speed and performance.

  Sources   Download

MIT

The Requires

 

parser lint php json validator validation syntax linter syntaxcheck schemavalidation

02/12 2016

v0.1.0

0.1.0.0 https://github.com/cmpayments/schemavalidator

SchemaValidator is a PHP implementation for validating JSON against a Schema (also a string), the JSON and Schema are both linted with https://github.com/cmpayments/jsonlint. This library is optimized for speed and performance.

  Sources   Download

MIT

The Requires

 

parser lint php json validator validation syntax linter syntaxcheck schemavalidation

25/03 2016

v0.0.11

0.0.11.0 https://github.com/cmpayments/schemavalidator

SchemaValidator is a PHP implementation for validating JSON against a Schema (also a string), the JSON and Schema are both linted with https://github.com/cmpayments/jsonlint. This library is optimized for speed and performance.

  Sources   Download

MIT

The Requires

 

parser lint php json validator validation syntax linter syntaxcheck schemavalidation

24/03 2016

v0.0.10

0.0.10.0 https://github.com/cmpayments/schemavalidator

SchemaValidator is a PHP implementation for validating JSON against a Schema (also a string), the JSON and Schema are both linted with https://github.com/cmpayments/jsonlint. This library is optimized for speed and performance.

  Sources   Download

MIT

The Requires

 

The Development Requires

parser lint php json validator validation syntax linter syntaxcheck schemavalidation

03/03 2016

v0.0.9

0.0.9.0 https://github.com/cmpayments/schemavalidator

SchemaValidator is a PHP implementation for validating JSON against a Schema (also a string), the JSON and Schema are both linted with https://github.com/cmpayments/jsonlint. This library is optimized for speed and performance.

  Sources   Download

MIT

The Requires

 

The Development Requires

parser lint php json validator validation syntax linter syntaxcheck schemavalidation

02/03 2016

v0.0.8

0.0.8.0 https://github.com/cmpayments/schemavalidator

SchemaValidator is a PHP implementation for validating JSON against a Schema (also a string), the JSON and Schema are both linted with https://github.com/cmpayments/jsonlint. This library is optimized for speed and performance.

  Sources   Download

MIT

The Requires

 

The Development Requires

parser lint php json validator validation syntax linter syntaxcheck schemavalidation

25/02 2016

v0.0.7

0.0.7.0 https://github.com/cmpayments/schemavalidator

SchemaValidator is a PHP implementation for validating JSON against a Schema (also a string), the JSON and Schema are both linted with https://github.com/cmpayments/jsonlint. This library is optimized for speed and performance.

  Sources   Download

MIT

The Requires

 

The Development Requires

parser lint php json validator validation syntax linter syntaxcheck schemavalidation

24/02 2016

v0.0.5

0.0.5.0 https://github.com/cmpayments/schemavalidator

SchemaValidator is a PHP implementation for validating JSON against a Schema (also a string), the JSON and Schema are both linted with https://github.com/cmpayments/jsonlint. This library is optimized for speed and performance.

  Sources   Download

MIT

The Requires

 

The Development Requires

parser lint php json validator validation syntax linter syntaxcheck schemavalidation

23/02 2016

v0.0.4

0.0.4.0 https://github.com/cmpayments/schemavalidator

SchemaValidator is a PHP implementation for validating JSON against a Schema (also a string), the JSON and Schema are both linted with https://github.com/cmpayments/jsonlint. This library is optimized for speed and performance.

  Sources   Download

MIT

The Requires

 

The Development Requires

parser lint php json validator validation syntax linter syntaxcheck schemavalidation

21/01 2016

v0.0.3

0.0.3.0 https://github.com/cmpayments/schemavalidator

SchemaValidator is a PHP implementation for validating JSON against a Schema (also a string), the JSON and Schema are both linted with https://github.com/cmpayments/jsonlint. This library is optimized for speed and performance.

  Sources   Download

MIT

The Requires

 

The Development Requires

parser lint php json validator validation syntax linter syntaxcheck schemavalidation