@@ -6,4 +6,65 @@ navigation:
6
6
7
7
# Advanced topics
8
8
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!
0 commit comments