justinrainbow/json-schema
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The justinrainbow/json-schema is a PHP implementation for validating JSON structures against a provided schema. This library comes in handy when you want to verify if a JSON adheres to the rules of a specified JSON schema.
Utilizing the justinrainbow/json-schema library is fairly straightforward, once installed. Installation can be done via Composer using the command composer require justinrainbow/json-schema
. Here's a simple example of how to use it:
<?php
$data = json_decode(file_get_contents('data.json'));
$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) {
echo sprintf("[%s] %s\n", $error['property'], $error['message']);
}
}
In this code, $data
is the object instance of the JSON being validated and schema.json
is the JSON schema against which $data
is being validated.
The documentation for the justinrainbow/json-schema library is hosted on the json-schema official website. More comprehensive usage examples and detailed descriptions of the library’s features can be found there. Users intending to exploit the library to its full potential are advised to review the documentation carefully. Apart from that, the GitHub repository for this library also serves as a valuable resource for understanding the library and provides several usage and installation instructions.