<?php
use JsonSchema\Uri\UriRetriever;
use JsonSchema\Validator;
use Zend\Debug\Debug;
// json config which should be valid to the schema
$data = <<<EOT
{
"name": "foo",
"value": "bar"
}
EOT;
$wrongdata = <<<EOT
{
"name": "foo",
"value": "bar",
"foobarbaz": "value"
}
EOT;
// the json schema
$schemaPath = __DIR__ . '/resources/jsonschema.json';
Debug::dump(file_get_contents($schemaPath));
// retrieve schema for validation
$retriever = new UriRetriever();
$schema = $retriever->retrieve('file://' . $schemaPath);
$validator = new Validator();
// do the schema validation
$validator->check(json_decode($data), $schema);
Debug::dump($validator->isValid());
$validator->check(json_decode($wrongdata), $schema);
Debug::dump($validator->isValid());
Debug::dump($validator->getErrors());
string(389) "{ "name": "Config", "type": "object", "additionalProperties": false, "properties": { "name": { "type": "string", "description": "Name of the option", "required": true }, "value": { "description": "value of the option", "type": "string", "required": true } } } "
bool(true)
bool(false)
array(1) { [0] => array(5) { ["property"] => string(0) "" ["pointer"] => string(0) "" ["message"] => string(93) "The property foobarbaz is not defined and the definition does not allow additional properties" ["constraint"] => string(14) "additionalProp" ["context"] => int(1) } }