Skip to content

Commit 897dc57

Browse files
Add "Validation using online schema" documentation (#12)
* Initial plan * Add content for 'Validation using online schema' section in advanced-topics.md Co-authored-by: DannyvdSluijs <618940+DannyvdSluijs@users.noreply.github.com> * Apply review suggestions: add () to Validator instantiation and explicit false to json_decode calls Co-authored-by: DannyvdSluijs <618940+DannyvdSluijs@users.noreply.github.com> Agent-Logs-Url: https://github.com/jsonrainbow/docs/sessions/002d7fa9-b47f-4da9-8ec1-c9a01f54edeb --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: DannyvdSluijs <618940+DannyvdSluijs@users.noreply.github.com>
1 parent 9eb5da9 commit 897dc57

1 file changed

Lines changed: 55 additions & 1 deletion

File tree

_docs/advanced-topics.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,61 @@ if ($validator->isValid()) {
6464
```
6565

6666
## Validation using online schema
67-
This paragraph needs to be written, want to help out? Checkout GitHub repo!
67+
Validation of a JSON document can be done using a schema hosted online. The validator will automatically fetch the schema from the provided URL using its built-in `UriRetriever`.
68+
69+
```php
70+
<?php
71+
72+
$data = json_decode(file_get_contents('data.json'), false);
73+
74+
// Validate against an online schema by passing the URL as a $ref
75+
$validator = new JsonSchema\Validator();
76+
$validator->validate($data, (object)['$ref' => 'https://example.com/your/schema.json']);
77+
78+
if ($validator->isValid()) {
79+
echo "The supplied JSON validates against the schema.\n";
80+
} else {
81+
echo "JSON does not validate. Violations:\n";
82+
foreach ($validator->getErrors() as $error) {
83+
printf("[%s] %s\n", $error['property'], $error['message']);
84+
}
85+
}
86+
```
87+
88+
If you need more control over how the remote schema is retrieved (e.g. to cache it or pre-load it), you can use `UriRetriever` and `SchemaStorage` explicitly:
89+
90+
```php
91+
<?php
92+
93+
use JsonSchema\SchemaStorage;
94+
use JsonSchema\Validator;
95+
use JsonSchema\Constraints\Factory;
96+
use JsonSchema\Uri\UriRetriever;
97+
98+
$schemaUrl = 'https://example.com/your/schema.json';
99+
100+
// Fetch the remote schema
101+
$retriever = new UriRetriever();
102+
$schema = $retriever->retrieve($schemaUrl);
103+
104+
// Register the fetched schema so that any $ref inside it resolves correctly
105+
$schemaStorage = new SchemaStorage($retriever);
106+
$schemaStorage->addSchema($schemaUrl, $schema);
107+
108+
$validator = new Validator(new Factory($schemaStorage));
109+
110+
$data = json_decode(file_get_contents('data.json'), false);
111+
$validator->validate($data, $schema);
112+
113+
if ($validator->isValid()) {
114+
echo "The supplied JSON validates against the schema.\n";
115+
} else {
116+
echo "JSON does not validate. Violations:\n";
117+
foreach ($validator->getErrors() as $error) {
118+
printf("[%s] %s\n", $error['property'], $error['message']);
119+
}
120+
}
121+
```
68122

69123
## Using custom error messages
70124
This paragraph needs to be written, want to help out? Checkout GitHub repo!

0 commit comments

Comments
 (0)