Skip to content

Commit 37617c8

Browse files
author
Greg Bowler
committed
docs: schema validation example
1 parent 93bf43f commit 37617c8

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed

example/03-schema.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
// The JSON we will validate. Notice that one of the array keys is not a string,
3+
// and the "colour" property is misspelled.
4+
use Gt\Json\JsonObjectBuilder;
5+
use Gt\Json\Schema\ValidationError;
6+
use Gt\Json\Schema\Validator;
7+
8+
require __DIR__ . "/../vendor/autoload.php";
9+
10+
$jsonString = <<<JSON
11+
{
12+
"name": "Cody",
13+
"colr": "orange",
14+
"food": [
15+
"Biscuits",
16+
"Mushrooms",
17+
12345
18+
]
19+
}
20+
JSON;
21+
22+
$schemaString = <<<JSON
23+
{
24+
"\$schema": "https://json-schema.org/draft/2020-12/schema",
25+
"title": "Simple object schema",
26+
"type": "object",
27+
"properties": {
28+
"name": {
29+
"type": "string",
30+
"minLength": 1
31+
},
32+
"colour": {
33+
"type": "string"
34+
},
35+
"food": {
36+
"type": "array",
37+
"items": {
38+
"type": "string"
39+
}
40+
}
41+
},
42+
"required": [
43+
"name",
44+
"colour",
45+
"food"
46+
],
47+
"additionalProperties": false
48+
}
49+
JSON;
50+
51+
$builder = new JsonObjectBuilder();
52+
53+
$schema = $builder->fromJsonString($schemaString);
54+
$json = $builder->fromJsonString($jsonString);
55+
56+
$validator = new Validator($schema);
57+
$result = $validator->validate($json);
58+
59+
if($result instanceof ValidationError) {
60+
echo "Error validating JSON!", PHP_EOL;
61+
62+
foreach($result->getErrorList() as $propertyName => $errorString) {
63+
echo "$propertyName: $errorString", PHP_EOL;
64+
}
65+
}
66+
else {
67+
echo "Everything is OK!", PHP_EOL;
68+
}

src/JsonObjectBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function fromFile(string $filePath):JsonObject {
9898
throw new FileNotFoundException($filePath);
9999
}
100100

101-
return self::fromJsonString(file_get_contents($filePath));
101+
return self::fromJsonString(file_get_contents($filePath) ?: "");
102102
}
103103

104104
}

src/Schema/ValidationError.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class ValidationError extends ValidationResult implements JsonSerializable {
1111
/** @param array<string> $errorList */
1212
public function __construct(
13-
private JsonKvpObject $schema,
13+
private JsonObject $schema,
1414
private array $errorList,
1515
) {}
1616

src/Schema/Validator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class Validator {
1010
public function __construct(
11-
private ?JsonKvpObject $schema = null,
11+
private ?JsonObject $schema = null,
1212
) {}
1313

1414
public function validate(JsonObject $json):ValidationResult {

0 commit comments

Comments
 (0)