Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sgpinkus/jsonschema

A PHP Json Schema validator.
https://github.com/sgpinkus/jsonschema

json-schema php validation

Last synced: about 1 month ago
JSON representation

A PHP Json Schema validator.

Awesome Lists containing this project

README

        

# Overview [![Build Status](https://api.travis-ci.org/sgpinkus/JsonSchema.png)](https://travis-ci.org/sgpinkus/JsonSchema)
Draft v6 compliant JSON Schema validator for PHP:

* Modular design.
* Simple interface for validation.
* JsonRef dereferencing is handled by an external PHP library [JsonRef](http://jsonref.org). You can easily replace it with a different one.
* Easily extensible with custom constraints.
* Draft v4 compatible.

# Installation

composer install sgpinkus/jsonschema

# Test

git clone ... && cd JsonSchema
git submodule update --init
composer test

# Usage
In the simplest case, where you have a standalone JSON schema with no `$refs`:

```php
validate($doc);
if($valid === true)
print "OK\n";
else
print $valid;

// Addendum: use JsonDocs::getPointer() to get a sub doc then validate it.
$valid = $schema->validate(JsonDocs::getPointer($doc, '/users/1'));
if($valid === true)
print "OK\n";
else
print "$valid";
```

If you have any `$refs` in your JSON schema, you need to use the `JsonRef` wrapper class to load and dereference the JSON schema documents:

```php
loadDocStr($schema, 'file:///tmp/some-unique-fake-uri'));
$valid = $schema->validate(json_decode($json));
if($valid === true)
print "OK\n";
else
print $valid;
```

To implement custom constraints extend the `Constraint` class and implement abstract methods, then
register the constraint when creating the `JsonSchema` instance:

```php
modulo = $modulo;
}

public static function getName() {
return 'modulo';
}

public function validate($doc, $context) {
if(is_int($doc) && $doc % $this->modulo !== 0) {
return new ValidationError($this, "$doc is not modulo {$this->modulo}", $context);
}
return true;
}

public static function build($context) {
if(!is_int($context->modulo)) {
throw new ConstraintParseException("The value of 'modulo' MUST be an integer.");
}

return new static($context->modulo);
}
}

$doc = 7;
$schema = '{
"type": "integer",
"modulo": 2
}';
$schema = new JsonSchema($schema, ['ModuloConstraint']);
$valid = $schema->validate($doc);
if($valid === true)
print "OK\n";
else
print $valid;
```

Also see [cli-validator.php](cli-validator.php) for example code.

# TODO
See [TODO](TODO.md).

# CONFORMANCE
See [conformance notes](CONFORMANCE.md).