Skip to content

Validate using a schema on disk

Danny van der Sluijs edited this page Feb 6, 2024 · 1 revision

About

This library support validating JSON documents using a schema on disk.

Pro/Cons

  • + Easier to use when dealing with larger JSON schemas
  • - Additional disk i/o for reading the schema

Example

<?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']);
    }
}
Clone this wiki locally