-
Notifications
You must be signed in to change notification settings - Fork 357
Validate using a schema on disk
Danny van der Sluijs edited this page Feb 6, 2024
·
1 revision
This library support validating JSON documents using a schema on disk.
+ Easier to use when dealing with larger JSON schemas
- Additional disk i/o for reading the schema
<?php
$data = json_decode(file_get_contents('data.json'));
// Validate
$validator = new JsonSchema\Validator;
$validator->validate($data, (object)['$ref' => 'file://' . realpath('schema.json')]);
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) {
printf("[%s] %s\n", $error['property'], $error['message']);
}
}