This page needs to be written, want to help out? Checkout GitHub repo!
+Validation using schema on disk
+Validation of a JSON document can be done using a schema located on disk.
+<?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']);
+ }
+}
+
+Validation using inline schema
+Validation of a JSON document can be done using a inline schema. This requires some additional setup of the schema storage
+<?php
+
+$data = json_decode(file_get_contents('data.json'));
+$jsonSchemaAsString = <<<'JSON'
+{
+ "type": "object",
+ "properties": {
+ "name": { "type": "string"},
+ "email": {"type": "string"}
+ },
+ "required": ["name","email"]
+}
+JSON;
+
+$jsonSchema = json_decode($jsonSchemaAsString);
+$schemaStorage = new JsonSchema\SchemaStorage();
+$schemaStorage->addSchema('internal://mySchema', $jsonSchema);
+$validator = new JsonSchema\Validator(
+ new JsonSchema\Constraints\Factory($schemaStorage)
+);
+$validator->validate($data, $jsonSchemaObject);
+
+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']);
+ }
+}
+
+Validation using online schema
+This paragraph needs to be written, want to help out? Checkout GitHub repo!
+Using custom error messages
+All the errors returned from the library contain a unique constraint name. Using this constraint name you can replace the +error message with own, adjusting for your tone of voice or language.
+<?php
+
+$data = '{"age": "John Doe"}'
+$jsonSchemaAsString = ' { "type": "object", "properties": { "age": { "type": "integer" } } } ';
+
+$jsonSchema = json_decode($jsonSchemaAsString);
+$schemaStorage = new JsonSchema\SchemaStorage();
+$schemaStorage->addSchema('internal://mySchema', $jsonSchema);
+$validator = new JsonSchema\Validator(
+ new JsonSchema\Constraints\Factory($schemaStorage)
+);
+$validator->validate($data, $jsonSchemaObject);
+
+foreach ($validator->getErrors() as $error) {
+ echo sprintf(
+ customErrorMessagePatternFunction($error['constraint']['name']),
+ ...$error['constraint']['params']
+ );
+}
+