PHPackages                             cmpayments/schemavalidator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. cmpayments/schemavalidator

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

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.

v0.1.1(9y ago)84.2k↓100%1[2 issues](https://github.com/cmpayments/json-schema-validator/issues)MITPHPPHP ^5.4 || ^7.0

Since Jan 21Pushed 9y ago2 watchersCompare

[ Source](https://github.com/cmpayments/json-schema-validator)[ Packagist](https://packagist.org/packages/cmpayments/schemavalidator)[ Docs](https://github.com/cmpayments/schemavalidator)[ RSS](/packages/cmpayments-schemavalidator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (12)Used By (0)

Schema Validator for PHP [![Build Status](https://camo.githubusercontent.com/65d3b6634d2c8444ef37e196ab255c445754e1bb2faa1c9c06d4aff1572eced6/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f636d7061796d656e74732f6a736f6e2d736368656d612d76616c696461746f722e737667)](https://travis-ci.org/cmpayments/json-schema-validator)
============================================================================================================================================================================================================================================================================================================================================

[](#schema-validator-for-php-)

[![License](https://camo.githubusercontent.com/70dc6c2f1e8c567a6d3da837ccb45236a87c305ed1c42e7d3a6ae7dcdfefc32d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f636d7061796d656e74732f736368656d6176616c696461746f722e737667)](https://camo.githubusercontent.com/70dc6c2f1e8c567a6d3da837ccb45236a87c305ed1c42e7d3a6ae7dcdfefc32d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f636d7061796d656e74732f736368656d6176616c696461746f722e737667)[![Latest Stable Version](https://camo.githubusercontent.com/9b0e513af2f8a5c0179834d0eed4d095f0e247a29a107f16cec004fe2739962f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636d7061796d656e74732f736368656d6176616c696461746f722e737667)](https://packagist.org/packages/cmpayments/schemavalidator)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/e49373f51e9c31c10a31fe067cf994b51f70f33cfe11a561b9629569d619431a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f636d7061796d656e74732f6a736f6e2d736368656d612d76616c696461746f722f6261646765732f7175616c6974792d73636f72652e706e67)](https://scrutinizer-ci.com/g/cmpayments/json-schema-validator/)[![Total Downloads](https://camo.githubusercontent.com/1b0bfbc9becc597c45d1423bd0ee824d025470d14a0b3a1e2f75712b3478ba92/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636d7061796d656e74732f736368656d6176616c696461746f722e737667)](https://packagist.org/packages/cmpayments/schemavalidator)[![Reference Status](https://camo.githubusercontent.com/8e60484d200b450a03529e865ac99b47ce350d0efc4bc75f0fc5e617a3984964/68747470733a2f2f7777772e76657273696f6e6579652e636f6d2f7068702f636d7061796d656e74733a736368656d6176616c696461746f722f7265666572656e63655f62616467652e737667)](https://www.versioneye.com/php/cmpayments:schemavalidator/references)

SchemaValidator is a PHP implementation for validating JSON against a Schema (also a string), the JSON and Schema are both linted with . This library is optimized for speed and performance.

Installation
------------

[](#installation)

For a quick install with Composer use:

```
$ composer require cmpayments/schemavalidator

```

Schema Validator for PHP can easily be used within another app if you have a [PSR-4](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md)autoloader, or it can be installed through [Composer](https://getcomposer.org/).

Usage
-----

[](#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

[](#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

[](#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)

[](#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

[](#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

[](#other-examples)

#### Example; test input when input is an array

[](#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

[](#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)

[](#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)

[](#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

[](#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
------------

[](#requirements)

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

Submitting bugs and feature requests
------------------------------------

[](#submitting-bugs-and-feature-requests)

Bugs and feature request are tracked on [GitHub](https://github.com/cmpayments/schemavalidator/issues)

Todo
----

[](#todo)

- [Pattern Validation](http://json-schema.org/latest/json-schema-validation.html#anchor33)
- [allOf (Validation keywords for any instance type)](http://json-schema.org/latest/json-schema-validation.html#anchor82)
- [anyOf (Validation keywords for any instance type)](http://json-schema.org/latest/json-schema-validation.html#anchor85)
- [allOf (Validation keywords for any instance type)](http://json-schema.org/latest/json-schema-validation.html#anchor88)
- [not (Validation keywords for any instance type)](http://json-schema.org/latest/json-schema-validation.html#anchor91)
- [title &amp; description Metadata](http://json-schema.org/latest/json-schema-validation.html#anchor98)
- [hostname (Defined Format Attributes)](http://json-schema.org/latest/json-schema-validation.html#anchor114)
- [ipv4 (Defined Format Attributes)](http://json-schema.org/latest/json-schema-validation.html#anchor117)
- [ipv6 (Defined Format Attributes)](http://json-schema.org/latest/json-schema-validation.html#anchor120)
- [uri (Defined Format Attributes)](http://json-schema.org/latest/json-schema-validation.html#anchor123)

Author
------

[](#author)

Boy Wijnmaalen -  -

License
-------

[](#license)

Schema Validator is licensed under the MIT License - see the LICENSE file for details

###  Health Score

30

—

LowBetter than 65% of packages

Maintenance15

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 50% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~42 days

Recently: every ~84 days

Total

10

Last Release

3384d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a8499c634f7815dcfe01986aa16c99df0b9f466b9bde8b826c757a5ce3839ed4?d=identicon)[boywijnmaalen](/maintainers/boywijnmaalen)

---

Top Contributors

[![boywijnmaalen](https://avatars.githubusercontent.com/u/3985741?v=4)](https://github.com/boywijnmaalen "boywijnmaalen (2 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (2 commits)")

---

Tags

phpjsonvalidatorvalidationparserlinterlintsyntaxsyntaxcheckschemavalidation

### Embed Badge

![Health badge](/badges/cmpayments-schemavalidator/health.svg)

```
[![Health](https://phpackages.com/badges/cmpayments-schemavalidator/health.svg)](https://phpackages.com/packages/cmpayments-schemavalidator)
```

###  Alternatives

[seld/jsonlint

JSON Linter

1.3k217.8M206](/packages/seld-jsonlint)[neilime/php-css-lint

Powerful &amp; memory efficient CSS linter for PHP

131.8M3](/packages/neilime-php-css-lint)[evaisse/php-json-schema-generator

A JSON Schema Generator.

20298.5k1](/packages/evaisse-php-json-schema-generator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
