Skip to content

Commit a5f2777

Browse files
committed
docs: Add advanced topics
1 parent f225d90 commit a5f2777

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

_docs/advanced-topics.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,65 @@ navigation:
66

77
# Advanced topics
88

9-
This page needs to be written, want to help out? Checkout GitHub repo!
9+
## Validation using schema on disk
10+
Validation of a JSON document can be done using a schema located on disk.
11+
12+
```php
13+
<?php
14+
15+
$data = json_decode(file_get_contents('data.json'));
16+
17+
// Validate
18+
$validator = new JsonSchema\Validator;
19+
$validator->validate($data, (object)['$ref' => 'file://' . realpath('schema.json')]);
20+
21+
if ($validator->isValid()) {
22+
echo "The supplied JSON validates against the schema.\n";
23+
} else {
24+
echo "JSON does not validate. Violations:\n";
25+
foreach ($validator->getErrors() as $error) {
26+
printf("[%s] %s\n", $error['property'], $error['message']);
27+
}
28+
}
29+
```
30+
31+
## Validation using inline schema
32+
Validation of a JSON document can be done using a inline schema. This requires some additional setup of the schema storage
33+
```php
34+
<?php
35+
36+
$data = json_decode(file_get_contents('data.json'));
37+
$jsonSchemaAsString = <<<'JSON'
38+
{
39+
"type": "object",
40+
"properties": {
41+
"name": { "type": "string"},
42+
"email": {"type": "string"}
43+
},
44+
"required": ["name","email"]
45+
}
46+
JSON;
47+
48+
$jsonSchema = json_decode($jsonSchemaAsString);
49+
$schemaStorage = new JsonSchema\SchemaStorage();
50+
$schemaStorage->addSchema('internal://mySchema', $jsonSchema);
51+
$validator = new JsonSchema\Validator(
52+
new JsonSchema\Constraints\Factory($schemaStorage)
53+
);
54+
$validator->validate($data, $jsonSchemaObject);
55+
56+
if ($validator->isValid()) {
57+
echo "The supplied JSON validates against the schema.\n";
58+
} else {
59+
echo "JSON does not validate. Violations:\n";
60+
foreach ($validator->getErrors() as $error) {
61+
printf("[%s] %s\n", $error['property'], $error['message']);
62+
}
63+
}
64+
```
65+
66+
## Validation using online schema
67+
This paragraph needs to be written, want to help out? Checkout GitHub repo!
68+
69+
## Using custom error messages
70+
This paragraph needs to be written, want to help out? Checkout GitHub repo!

_docs/community-guide.md

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)