Added documentation for how to resolve references

This also is easier when you use the UriRetriever object, so adding that too.
This commit is contained in:
Tyler Akins 2012-12-21 17:46:29 -06:00
parent df60688604
commit 12f5e2d626

View File

@ -22,15 +22,26 @@ See [json-schema](http://json-schema.org/) for more details.
```php
<?php
// Get the schema and data as objects
$retriever = new JsonSchema\Uri\UriRetriever;
$schema = $retriever->retrieve('file://' . realpath('schema.json'));
$data = json_decode(file_get_contents('data.json'));
// If you use $ref or if you are unsure, resolve those references here
// This modifies the $schema object
$refResolver = new JsonSchema\RefResolver($retriever);
$refResolver->resolve($schema, 'file://' . __DIR__);
// Validate
$validator = new JsonSchema\Validator();
$validator->check(json_decode($json), json_decode($schema));
$validator->check($data, $resolvedSchema);
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) {
echo sprintf("[%s] %s\n",$error['property'], $error['message']);
echo sprintf("[%s] %s\n", $error['property'], $error['message']);
}
}
```