mirror of
https://github.com/justinrainbow/json-schema.git
synced 2025-01-29 11:43:04 +01:00
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:
parent
df60688604
commit
12f5e2d626
15
README.md
15
README.md
@ -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']);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
Loading…
x
Reference in New Issue
Block a user