Skip to content

Commit 2dff144

Browse files
committed
feat(demo): add simple demo script
1 parent 31b2954 commit 2dff144

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

demo/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## JsonSchema\Validator Demo
2+
3+
This demo script uses the example from the root README.md and provides sample JSON files for testing `JsonSchema\Validator`.
4+
5+
To change or replace the JSON schema document, please edit `/path/to/json-schema/demo/schema.json`.
6+
7+
To change or replace the JSON document that is validated, please edit `/path/to/json-schema/demo/data.json`.
8+
9+
To run the demo, change the path in the following example and run it in your terminal.
10+
11+
```
12+
cd /path/to/json-schema/demo
13+
php demo.php // The supplied JSON validates against the schema.
14+
```

demo/data.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"foo":"bar"
3+
}

demo/demo.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
require(__DIR__ . '/../vendor/autoload.php');
3+
4+
$data = json_decode(file_get_contents('data.json'));
5+
6+
// Validate
7+
$validator = new JsonSchema\Validator;
8+
$validator->check($data, (object)['$ref' => 'file://' . realpath('schema.json')]);
9+
10+
if ($validator->isValid()) {
11+
echo "The supplied JSON validates against the schema.\n";
12+
} else {
13+
echo "JSON does not validate. Violations:\n";
14+
foreach ($validator->getErrors() as $error) {
15+
echo sprintf("[%s] %s\n", $error['property'], $error['message']);
16+
}
17+
}

demo/schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "object"
3+
}

0 commit comments

Comments
 (0)