2017 © Pedro PelĂĄez
 

library json-schema

A library to validate a json schema.

image

justinrainbow/json-schema

A library to validate a json schema.

  • Tuesday, June 12, 2018
  • by justinrainbow
  • Repository
  • 58 Watchers
  • 1375 Stars
  • 16,415,612 Installations
  • PHP
  • 305 Dependents
  • 6 Suggesters
  • 261 Forks
  • 48 Open issues
  • 45 Versions
  • 9 % Grown

The README.md

JSON Schema for PHP

Build Status Latest Stable Version Total Downloads, (*1)

A PHP Implementation for validating JSON Structures against a given Schema with support for Schemas of Draft-3 or Draft-4. Features of newer Drafts might not be supported. See Table of All Versions of Everything to get an overview of all existing Drafts., (*2)

See json-schema for more details., (*3)

Installation

Library

git clone https://github.com/jsonrainbow/json-schema.git

Composer

Install PHP Composer, (*4)

composer require justinrainbow/json-schema

Usage

For a complete reference see Understanding JSON Schema., (*5)

Note: features of Drafts newer than Draft-4 might not be supported!, (*6)

Basic usage

<?php

$data = json_decode(file_get_contents('data.json'));

// Validate
$validator = new JsonSchema\Validator;
$validator->validate($data, (object)['$ref' => 'file://' . realpath('schema.json')]);

if ($validator->isValid()) {
    echo "The supplied JSON validates against the schema.\n";
} else {
    echo "JSON does not validate. Violations:\n";
    foreach ($validator->getErrors() as $error) {
        printf("[%s] %s\n", $error['property'], $error['message']);
    }
}

Type coercion

If you're validating data passed to your application via HTTP, you can cast strings and booleans to the expected types defined by your schema:, (*7)

<?php

use JsonSchema\SchemaStorage;
use JsonSchema\Validator;
use JsonSchema\Constraints\Factory;
use JsonSchema\Constraints\Constraint;

$request = (object)[
    'processRefund'=>"true",
    'refundAmount'=>"17"
];

$validator->validate(
    $request, (object) [
    "type"=>"object",
        "properties"=>(object)[
            "processRefund"=>(object)[
                "type"=>"boolean"
            ],
            "refundAmount"=>(object)[
                "type"=>"number"
            ]
        ]
    ],
    Constraint::CHECK_MODE_COERCE_TYPES
); // validates!

is_bool($request->processRefund); // true
is_int($request->refundAmount); // true

A shorthand method is also available:, (*8)

$validator->coerce($request, $schema);
// equivalent to $validator->validate($data, $schema, Constraint::CHECK_MODE_COERCE_TYPES);

Default values

If your schema contains default values, you can have these automatically applied during validation:, (*9)

<?php

use JsonSchema\Validator;
use JsonSchema\Constraints\Constraint;

$request = (object)[
    'refundAmount'=>17
];

$validator = new Validator();

$validator->validate(
    $request,
    (object)[
        "type"=>"object",
        "properties"=>(object)[
            "processRefund"=>(object)[
                "type"=>"boolean",
                "default"=>true
            ]
        ]
    ],
    Constraint::CHECK_MODE_APPLY_DEFAULTS
); //validates, and sets defaults for missing properties

is_bool($request->processRefund); // true
$request->processRefund; // true

With inline references

<?php

use JsonSchema\SchemaStorage;
use JsonSchema\Validator;
use JsonSchema\Constraints\Factory;

$jsonSchema = <<<'JSON'
{
    "type": "object",
    "properties": {
        "data": {
            "oneOf": [
                { "$ref": "#/definitions/integerData" },
                { "$ref": "#/definitions/stringData" }
            ]
        }
    },
    "required": ["data"],
    "definitions": {
        "integerData" : {
            "type": "integer",
            "minimum" : 0
        },
        "stringData" : {
            "type": "string"
        }
    }
}
JSON;

// Schema must be decoded before it can be used for validation
$jsonSchemaObject = json_decode($jsonSchema);

// The SchemaStorage can resolve references, loading additional schemas from file as needed, etc.
$schemaStorage = new SchemaStorage();

// This does two things:
// 1) Mutates $jsonSchemaObject to normalize the references (to file://mySchema#/definitions/integerData, etc)
// 2) Tells $schemaStorage that references to file://mySchema... should be resolved by looking in $jsonSchemaObject
$schemaStorage->addSchema('file://mySchema', $jsonSchemaObject);

// Provide $schemaStorage to the Validator so that references can be resolved during validation
$jsonValidator = new Validator(new Factory($schemaStorage));

// JSON must be decoded before it can be validated
$jsonToValidateObject = json_decode('{"data":123}');

// Do validation (use isValid() and getErrors() to check the result)
$jsonValidator->validate($jsonToValidateObject, $jsonSchemaObject);

Configuration Options

A number of flags are available to alter the behavior of the validator. These can be passed as the third argument to Validator::validate(), or can be provided as the third argument to Factory::__construct() if you wish to persist them across multiple validate() calls., (*10)

Flag Description
Constraint::CHECK_MODE_NORMAL Validate in 'normal' mode - this is the default
Constraint::CHECK_MODE_TYPE_CAST Enable fuzzy type checking for associative arrays and objects
Constraint::CHECK_MODE_COERCE_TYPES Convert data types to match the schema where possible
Constraint::CHECK_MODE_EARLY_COERCE Apply type coercion as soon as possible
Constraint::CHECK_MODE_APPLY_DEFAULTS Apply default values from the schema if not set
Constraint::CHECK_MODE_ONLY_REQUIRED_DEFAULTS When applying defaults, only set values that are required
Constraint::CHECK_MODE_EXCEPTIONS Throw an exception immediately if validation fails
Constraint::CHECK_MODE_DISABLE_FORMAT Do not validate "format" constraints
Constraint::CHECK_MODE_VALIDATE_SCHEMA Validate the schema as well as the provided document

Please note that using CHECK_MODE_COERCE_TYPES or CHECK_MODE_APPLY_DEFAULTS will modify your original data., (*11)

CHECK_MODE_EARLY_COERCE has no effect unless used in combination with CHECK_MODE_COERCE_TYPES. If enabled, the validator will use (and coerce) the first compatible type it encounters, even if the schema defines another type that matches directly and does not require coercion., (*12)

Running the tests

composer test                            # run all unit tests
composer testOnly TestClass              # run specific unit test class
composer testOnly TestClass::testMethod  # run specific unit test method
composer style-check                     # check code style for errors
composer style-fix                       # automatically fix code style errors

The Versions

12/06 2018

dev-master

9999999-dev https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

14/02 2018

5.x-dev

5.9999999.9999999.9999999-dev https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

14/02 2018

5.2.7

5.2.7.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

10/11 2017

dev-6.0.0-dev

dev-6.0.0-dev https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

21/10 2017

5.2.6

5.2.6.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

10/10 2017

5.2.5

5.2.5.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

04/10 2017

5.2.3

5.2.3.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

04/10 2017

5.2.4

5.2.4.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

03/10 2017

5.2.2

5.2.2.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

16/05 2017

5.2.1

5.2.1.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

22/03 2017

5.2.0

5.2.0.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

22/02 2017

5.1.0

5.1.0.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

15/02 2017

5.0.0

5.0.0.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

22/12 2016

4.1.0

4.1.0.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

09/11 2016

4.0.1

4.0.1.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

10/10 2016

4.0.0

4.0.0.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

26/08 2016

3.0.1

3.0.1.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

15/08 2016

3.0.0

3.0.0.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

02/06 2016

2.0.5

2.0.5.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

24/05 2016

2.0.4

2.0.4.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

10/05 2016

2.0.3

2.0.3.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

09/05 2016

2.0.2

2.0.2.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

28/04 2016

2.0.1

2.0.1.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

14/04 2016

2.0.0

2.0.0.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

25/01 2016

1.6.1

1.6.1.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.29

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

06/01 2016

v1.6.0

1.6.0.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.2

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

09/09 2015

1.5.0

1.5.0.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.2

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

14/07 2015

1.4.4

1.4.4.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.2

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

13/07 2015

1.4.3

1.4.3.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.2

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

14/06 2015

1.4.2

1.4.2.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.2

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

27/03 2015

1.4.1

1.4.1.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

23/03 2015

1.4.0

1.4.0.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

25/08 2014

1.3.7

1.3.7.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

05/03 2014

1.3.6

1.3.6.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

13/12 2013

1.3.5

1.3.5.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

08/12 2013

1.3.4

1.3.4.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

22/07 2013

1.3.3

1.3.3.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

10/06 2013

1.3.2

1.3.2.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

The Development Requires

by Bruno Prieto Reis
by Justin Rainbow

schema json

21/02 2013

1.3.1

1.3.1.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

by Bruno Prieto Reis
by Justin Rainbow

schema json

18/02 2013

1.3.0

1.3.0.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

by Bruno Prieto Reis
by Justin Rainbow

schema json

31/01 2013

1.2.4

1.2.4.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

by Bruno Prieto Reis
by Justin Rainbow

schema json

29/01 2013

1.2.3

1.2.3.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

by Bruno Prieto Reis
by Justin Rainbow

schema json

18/08 2012

1.2.1

1.2.1.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

by Bruno Prieto Reis
by Justin Rainbow

schema json

04/06 2012

dev-schema-builder

dev-schema-builder https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

BSD-3-Clause

The Requires

 

by Bruno Prieto Reis
by Justin Rainbow

schema json

03/01 2012

1.1.0

1.1.0.0 https://github.com/justinrainbow/json-schema

A library to validate a json schema.

  Sources   Download

NewBSD

The Requires

  • php >=5.3.0

 

by Bruno Prieto Reis
by Justin Rainbow

schema json