ex

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Committer:
TMBOY
Date:
Tue Jul 18 16:27:22 2017 +0800
Revision:
44:c1d8923072ba
?

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TMBOY 44:c1d8923072ba 1 # JSON Schema for PHP
TMBOY 44:c1d8923072ba 2
TMBOY 44:c1d8923072ba 3 [![Build Status](https://travis-ci.org/justinrainbow/json-schema.svg?branch=master)](https://travis-ci.org/justinrainbow/json-schema)
TMBOY 44:c1d8923072ba 4 [![Latest Stable Version](https://poser.pugx.org/justinrainbow/json-schema/v/stable.png)](https://packagist.org/packages/justinrainbow/json-schema)
TMBOY 44:c1d8923072ba 5 [![Total Downloads](https://poser.pugx.org/justinrainbow/json-schema/downloads.png)](https://packagist.org/packages/justinrainbow/json-schema)
TMBOY 44:c1d8923072ba 6
TMBOY 44:c1d8923072ba 7 A PHP Implementation for validating `JSON` Structures against a given `Schema`.
TMBOY 44:c1d8923072ba 8
TMBOY 44:c1d8923072ba 9 See [json-schema](http://json-schema.org/) for more details.
TMBOY 44:c1d8923072ba 10
TMBOY 44:c1d8923072ba 11 ## Installation
TMBOY 44:c1d8923072ba 12
TMBOY 44:c1d8923072ba 13 ### Library
TMBOY 44:c1d8923072ba 14
TMBOY 44:c1d8923072ba 15 $ git clone https://github.com/justinrainbow/json-schema.git
TMBOY 44:c1d8923072ba 16
TMBOY 44:c1d8923072ba 17 ### Dependencies
TMBOY 44:c1d8923072ba 18
TMBOY 44:c1d8923072ba 19 #### [`Composer`](https://github.com/composer/composer) (*will use the Composer ClassLoader*)
TMBOY 44:c1d8923072ba 20
TMBOY 44:c1d8923072ba 21 $ wget http://getcomposer.org/composer.phar
TMBOY 44:c1d8923072ba 22 $ php composer.phar require justinrainbow/json-schema:~2.0
TMBOY 44:c1d8923072ba 23
TMBOY 44:c1d8923072ba 24 ## Usage
TMBOY 44:c1d8923072ba 25
TMBOY 44:c1d8923072ba 26 ### Basic usage
TMBOY 44:c1d8923072ba 27
TMBOY 44:c1d8923072ba 28 ```php
TMBOY 44:c1d8923072ba 29 <?php
TMBOY 44:c1d8923072ba 30
TMBOY 44:c1d8923072ba 31 $data = json_decode(file_get_contents('data.json'));
TMBOY 44:c1d8923072ba 32
TMBOY 44:c1d8923072ba 33 // Validate
TMBOY 44:c1d8923072ba 34 $validator = new JsonSchema\Validator;
TMBOY 44:c1d8923072ba 35 $validator->check($data, (object)['$ref' => 'file://' . realpath('schema.json')]);
TMBOY 44:c1d8923072ba 36
TMBOY 44:c1d8923072ba 37 if ($validator->isValid()) {
TMBOY 44:c1d8923072ba 38 echo "The supplied JSON validates against the schema.\n";
TMBOY 44:c1d8923072ba 39 } else {
TMBOY 44:c1d8923072ba 40 echo "JSON does not validate. Violations:\n";
TMBOY 44:c1d8923072ba 41 foreach ($validator->getErrors() as $error) {
TMBOY 44:c1d8923072ba 42 echo sprintf("[%s] %s\n", $error['property'], $error['message']);
TMBOY 44:c1d8923072ba 43 }
TMBOY 44:c1d8923072ba 44 }
TMBOY 44:c1d8923072ba 45 ```
TMBOY 44:c1d8923072ba 46 ###Type Coercion
TMBOY 44:c1d8923072ba 47 If you're validating data passed to your application via HTTP, you can cast strings and booleans to the expected types defined by your schema:
TMBOY 44:c1d8923072ba 48 ```php
TMBOY 44:c1d8923072ba 49 use JsonSchema\SchemaStorage;
TMBOY 44:c1d8923072ba 50 use JsonSchema\Validator;
TMBOY 44:c1d8923072ba 51 use JsonSchema\Constraints\Factory;
TMBOY 44:c1d8923072ba 52 use JsonSchema\Constraints\Constraint;
TMBOY 44:c1d8923072ba 53
TMBOY 44:c1d8923072ba 54 $request = (object)[
TMBOY 44:c1d8923072ba 55 'processRefund'=>"true",
TMBOY 44:c1d8923072ba 56 'refundAmount'=>"17"
TMBOY 44:c1d8923072ba 57 ];
TMBOY 44:c1d8923072ba 58
TMBOY 44:c1d8923072ba 59 $factory = new Factory( null, null, Constraint::CHECK_MODE_TYPE_CAST | Constraint::CHECK_MODE_COERCE );
TMBOY 44:c1d8923072ba 60
TMBOY 44:c1d8923072ba 61 $validator = new Validator($factory);
TMBOY 44:c1d8923072ba 62 $validator->check($request, (object) [
TMBOY 44:c1d8923072ba 63 "type"=>"object",
TMBOY 44:c1d8923072ba 64 "properties"=>(object)[
TMBOY 44:c1d8923072ba 65 "processRefund"=>(object)[
TMBOY 44:c1d8923072ba 66 "type"=>"boolean"
TMBOY 44:c1d8923072ba 67 ],
TMBOY 44:c1d8923072ba 68 "refundAmount"=>(object)[
TMBOY 44:c1d8923072ba 69 "type"=>"number"
TMBOY 44:c1d8923072ba 70 ]
TMBOY 44:c1d8923072ba 71 ]
TMBOY 44:c1d8923072ba 72 ]); // validates!
TMBOY 44:c1d8923072ba 73
TMBOY 44:c1d8923072ba 74 is_bool($request->processRefund); // true
TMBOY 44:c1d8923072ba 75 is_int($request->refundAmount); // true
TMBOY 44:c1d8923072ba 76 ```
TMBOY 44:c1d8923072ba 77
TMBOY 44:c1d8923072ba 78 Note that the ```CHECK_MODE_COERCE``` flag will only take effect when an object is passed into the ```check``` method.
TMBOY 44:c1d8923072ba 79
TMBOY 44:c1d8923072ba 80 ### With inline references
TMBOY 44:c1d8923072ba 81
TMBOY 44:c1d8923072ba 82 ```php
TMBOY 44:c1d8923072ba 83 <?php
TMBOY 44:c1d8923072ba 84
TMBOY 44:c1d8923072ba 85 use JsonSchema\SchemaStorage;
TMBOY 44:c1d8923072ba 86 use JsonSchema\Validator;
TMBOY 44:c1d8923072ba 87 use JsonSchema\Constraints\Factory;
TMBOY 44:c1d8923072ba 88
TMBOY 44:c1d8923072ba 89 $jsonSchema = <<<'JSON'
TMBOY 44:c1d8923072ba 90 {
TMBOY 44:c1d8923072ba 91 "type": "object",
TMBOY 44:c1d8923072ba 92 "properties": {
TMBOY 44:c1d8923072ba 93 "data": {
TMBOY 44:c1d8923072ba 94 "oneOf": [
TMBOY 44:c1d8923072ba 95 { "$ref": "#/definitions/integerData" },
TMBOY 44:c1d8923072ba 96 { "$ref": "#/definitions/stringData" }
TMBOY 44:c1d8923072ba 97 ]
TMBOY 44:c1d8923072ba 98 }
TMBOY 44:c1d8923072ba 99 },
TMBOY 44:c1d8923072ba 100 "required": ["data"],
TMBOY 44:c1d8923072ba 101 "definitions": {
TMBOY 44:c1d8923072ba 102 "integerData" : {
TMBOY 44:c1d8923072ba 103 "type": "integer",
TMBOY 44:c1d8923072ba 104 "minimum" : 0
TMBOY 44:c1d8923072ba 105 },
TMBOY 44:c1d8923072ba 106 "stringData" : {
TMBOY 44:c1d8923072ba 107 "type": "string"
TMBOY 44:c1d8923072ba 108 }
TMBOY 44:c1d8923072ba 109 }
TMBOY 44:c1d8923072ba 110 }
TMBOY 44:c1d8923072ba 111 JSON;
TMBOY 44:c1d8923072ba 112
TMBOY 44:c1d8923072ba 113 // Schema must be decoded before it can be used for validation
TMBOY 44:c1d8923072ba 114 $jsonSchemaObject = json_decode($jsonSchema);
TMBOY 44:c1d8923072ba 115
TMBOY 44:c1d8923072ba 116 // The SchemaStorage can resolve references, loading additional schemas from file as needed, etc.
TMBOY 44:c1d8923072ba 117 $schemaStorage = new SchemaStorage();
TMBOY 44:c1d8923072ba 118
TMBOY 44:c1d8923072ba 119 // This does two things:
TMBOY 44:c1d8923072ba 120 // 1) Mutates $jsonSchemaObject to normalize the references (to file://mySchema#/definitions/integerData, etc)
TMBOY 44:c1d8923072ba 121 // 2) Tells $schemaStorage that references to file://mySchema... should be resolved by looking in $jsonSchemaObject
TMBOY 44:c1d8923072ba 122 $schemaStorage->addSchema('file://mySchema', $jsonSchemaObject);
TMBOY 44:c1d8923072ba 123
TMBOY 44:c1d8923072ba 124 // Provide $schemaStorage to the Validator so that references can be resolved during validation
TMBOY 44:c1d8923072ba 125 $jsonValidator = new Validator( new Factory($schemaStorage));
TMBOY 44:c1d8923072ba 126
TMBOY 44:c1d8923072ba 127 // JSON must be decoded before it can be validated
TMBOY 44:c1d8923072ba 128 $jsonToValidateObject = json_decode('{"data":123}');
TMBOY 44:c1d8923072ba 129
TMBOY 44:c1d8923072ba 130 // Do validation (use isValid() and getErrors() to check the result)
TMBOY 44:c1d8923072ba 131 $jsonValidator->check($jsonToValidateObject, $jsonSchemaObject);
TMBOY 44:c1d8923072ba 132 ```
TMBOY 44:c1d8923072ba 133
TMBOY 44:c1d8923072ba 134 ## Running the tests
TMBOY 44:c1d8923072ba 135
TMBOY 44:c1d8923072ba 136 $ vendor/bin/phpunit