Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-os-example-mbed5-blinky by
dumi_doc-master/schema/test.php@44:c1d8923072ba, 2017-07-18 (annotated)
- Committer:
- TMBOY
- Date:
- Tue Jul 18 16:27:22 2017 +0800
- Revision:
- 44:c1d8923072ba
?
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| TMBOY | 44:c1d8923072ba | 1 | <?php |
| TMBOY | 44:c1d8923072ba | 2 | $time1=microtime(true); |
| TMBOY | 44:c1d8923072ba | 3 | require("vendor/autoload.php"); |
| TMBOY | 44:c1d8923072ba | 4 | use JsonSchema\SchemaStorage; |
| TMBOY | 44:c1d8923072ba | 5 | use JsonSchema\Validator; |
| TMBOY | 44:c1d8923072ba | 6 | use JsonSchema\Constraints\Factory; |
| TMBOY | 44:c1d8923072ba | 7 | |
| TMBOY | 44:c1d8923072ba | 8 | |
| TMBOY | 44:c1d8923072ba | 9 | // Schema must be decoded before it can be used for validation |
| TMBOY | 44:c1d8923072ba | 10 | $jsonSchemaObject = json_decode(file_get_contents("dumi_schema.json")); |
| TMBOY | 44:c1d8923072ba | 11 | $newsSchemaObject = json_decode(file_get_contents("resource/news.schema.json")); |
| TMBOY | 44:c1d8923072ba | 12 | $weatherSchemaObject = json_decode(file_get_contents("resource/weather.schema.json")); |
| TMBOY | 44:c1d8923072ba | 13 | $musicSchemaObject = json_decode(file_get_contents("resource/music.schema.json")); |
| TMBOY | 44:c1d8923072ba | 14 | |
| TMBOY | 44:c1d8923072ba | 15 | // The SchemaStorage can resolve references, loading additional schemas from file as needed, etc. |
| TMBOY | 44:c1d8923072ba | 16 | $schemaStorage = new SchemaStorage(); |
| TMBOY | 44:c1d8923072ba | 17 | |
| TMBOY | 44:c1d8923072ba | 18 | // This does two things: |
| TMBOY | 44:c1d8923072ba | 19 | // 1) Mutates $jsonSchemaObject to normalize the references (to file://mySchema#/definitions/integerData, etc) |
| TMBOY | 44:c1d8923072ba | 20 | // 2) Tells $schemaStorage that references to file://mySchema... should be resolved by looking in $jsonSchemaObject |
| TMBOY | 44:c1d8923072ba | 21 | $schemaStorage->addSchema('https://xiaodu.baidu.com/schema/dumi_schema.json', $jsonSchemaObject); |
| TMBOY | 44:c1d8923072ba | 22 | $schemaStorage->addSchema('https://xiaodu.baidu.com/schema/news.schema.json', $newsSchemaObject); |
| TMBOY | 44:c1d8923072ba | 23 | $schemaStorage->addSchema('https://xiaodu.baidu.com/schema/weather.schema.json', $weatherSchemaObject); |
| TMBOY | 44:c1d8923072ba | 24 | $schemaStorage->addSchema('https://xiaodu.baidu.com/schema/music.schema.json', $musicSchemaObject); |
| TMBOY | 44:c1d8923072ba | 25 | |
| TMBOY | 44:c1d8923072ba | 26 | // Provide $schemaStorage to the Validator so that references can be resolved during validation |
| TMBOY | 44:c1d8923072ba | 27 | $jsonValidator = new Validator( new Factory($schemaStorage)); |
| TMBOY | 44:c1d8923072ba | 28 | |
| TMBOY | 44:c1d8923072ba | 29 | // JSON must be decoded before it can be validated |
| TMBOY | 44:c1d8923072ba | 30 | $jsonToValidateObject = json_decode(file_get_contents("testdata/news.json")); |
| TMBOY | 44:c1d8923072ba | 31 | var_dump($jsonToValidateObject); |
| TMBOY | 44:c1d8923072ba | 32 | |
| TMBOY | 44:c1d8923072ba | 33 | // Do validation (use isValid() and getErrors() to check the result) |
| TMBOY | 44:c1d8923072ba | 34 | $jsonValidator->check($jsonToValidateObject, $jsonSchemaObject); |
| TMBOY | 44:c1d8923072ba | 35 | |
| TMBOY | 44:c1d8923072ba | 36 | if ($jsonValidator->isValid()) { |
| TMBOY | 44:c1d8923072ba | 37 | echo "The supplied JSON validates against the schema.\n"; |
| TMBOY | 44:c1d8923072ba | 38 | } else { |
| TMBOY | 44:c1d8923072ba | 39 | echo "JSON does not validate. Violations:\n"; |
| TMBOY | 44:c1d8923072ba | 40 | foreach ($jsonValidator->getErrors() as $error) { |
| TMBOY | 44:c1d8923072ba | 41 | echo sprintf("[%s] %s\n", $error['property'], $error['message']); |
| TMBOY | 44:c1d8923072ba | 42 | } |
| TMBOY | 44:c1d8923072ba | 43 | } |
| TMBOY | 44:c1d8923072ba | 44 | var_dump(microtime(true)-$time1); |
