Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/sgpinkus/jsonschema
- Owner: sgpinkus
- Created: 2015-07-27T13:50:34.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2022-10-08T01:28:42.000Z (over 2 years ago)
- Last Synced: 2024-11-14T16:47:49.101Z (2 months ago)
- Topics: json-schema, php, validation
- Language: PHP
- Homepage:
- Size: 254 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
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).