aleksandr.tm / json-schema
用于验证JSON模式的库。
5.2.13
2023-06-19 11:16 UTC
Requires
- php: >=7.0.33
Requires (Dev)
- friendsofphp/php-cs-fixer: ~2.2.20||~2.15.1
- json-schema/json-schema-test-suite: 1.2.0
- phpunit/phpunit: ^4.8.35
- 5.x-dev
- 5.2.13
- 5.2.12
- 5.2.11
- 5.2.10
- 5.2.9
- 5.2.8
- 5.2.7
- 5.2.6
- 5.2.5
- 5.2.4
- 5.2.3
- 5.2.2
- 5.2.1
- 5.2.0
- 5.1.0
- dev-master / 5.0.x-dev
- 5.0.0
- 4.1.0
- 4.0.1
- 4.0.0
- 3.0.1
- 3.0.0
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.6.1
- 1.6.0
- 1.5.0
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.7
- 1.3.6
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.4
- 1.2.3
- 1.2.1
- 1.1.0
- dev-schema-builder
This package is not auto-updated.
Last update: 2024-09-24 18:38:37 UTC
README
一个用于验证给定Schema
的JSON
结构的PHP实现。
有关详细信息,请参阅json-schema。
安装
库
git clone https://github.com/justinrainbow/json-schema.git
Composer
composer require justinrainbow/json-schema
使用方法
<?php $data = json_decode(file_get_contents('data.json')); // Validate $validator = new JsonSchema\Validator; $validator->validate($data, (object)['$ref' => 'file://' . realpath('schema.json')]); if ($validator->isValid()) { echo "The supplied JSON validates against the schema.\n"; } else { echo "JSON does not validate. Violations:\n"; foreach ($validator->getErrors() as $error) { echo sprintf("[%s] %s\n", $error['property'], $error['message']); } }
类型强制转换
如果您正在验证通过HTTP传递给您的应用程序的数据,您可以强制转换字符串和布尔值为您的模式定义的预期类型
<?php use JsonSchema\SchemaStorage; use JsonSchema\Validator; use JsonSchema\Constraints\Factory; use JsonSchema\Constraints\Constraint; $request = (object)[ 'processRefund'=>"true", 'refundAmount'=>"17" ]; $validator->validate( $request, (object) [ "type"=>"object", "properties"=>(object)[ "processRefund"=>(object)[ "type"=>"boolean" ], "refundAmount"=>(object)[ "type"=>"number" ] ] ], Constraint::CHECK_MODE_COERCE_TYPES ); // validates! is_bool($request->processRefund); // true is_int($request->refundAmount); // true
还提供了一种简写方法
$validator->coerce($request, $schema); // equivalent to $validator->validate($data, $schema, Constraint::CHECK_MODE_COERCE_TYPES);
默认值
如果您的模式包含默认值,您可以在验证期间自动应用这些值
<?php use JsonSchema\Validator; use JsonSchema\Constraints\Constraint; $request = (object)[ 'refundAmount'=>17 ]; $validator = new Validator(); $validator->validate( $request, (object)[ "type"=>"object", "properties"=>(object)[ "processRefund"=>(object)[ "type"=>"boolean", "default"=>true ] ] ], Constraint::CHECK_MODE_APPLY_DEFAULTS ); //validates, and sets defaults for missing properties is_bool($request->processRefund); // true $request->processRefund; // true
使用内联引用
<?php use JsonSchema\SchemaStorage; use JsonSchema\Validator; use JsonSchema\Constraints\Factory; $jsonSchema = <<<'JSON' { "type": "object", "properties": { "data": { "oneOf": [ { "$ref": "#/definitions/integerData" }, { "$ref": "#/definitions/stringData" } ] } }, "required": ["data"], "definitions": { "integerData" : { "type": "integer", "minimum" : 0 }, "stringData" : { "type": "string" } } } JSON; // Schema must be decoded before it can be used for validation $jsonSchemaObject = json_decode($jsonSchema); // The SchemaStorage can resolve references, loading additional schemas from file as needed, etc. $schemaStorage = new SchemaStorage(); // This does two things: // 1) Mutates $jsonSchemaObject to normalize the references (to file://mySchema#/definitions/integerData, etc) // 2) Tells $schemaStorage that references to file://mySchema... should be resolved by looking in $jsonSchemaObject $schemaStorage->addSchema('file://mySchema', $jsonSchemaObject); // Provide $schemaStorage to the Validator so that references can be resolved during validation $jsonValidator = new Validator( new Factory($schemaStorage)); // JSON must be decoded before it can be validated $jsonToValidateObject = json_decode('{"data":123}'); // Do validation (use isValid() and getErrors() to check the result) $jsonValidator->validate($jsonToValidateObject, $jsonSchemaObject);
配置选项
有几个标志可用于更改验证器的行为。这些可以作为第三个参数传递给Validator::validate()
,或者可以作为第三个参数传递给Factory::__construct()
,如果您希望它们在多个validate()
调用之间持久化。
请注意,使用Constraint::CHECK_MODE_COERCE_TYPES
或Constraint::CHECK_MODE_APPLY_DEFAULTS
将修改您的原始数据。
运行测试
composer test # run all unit tests composer testOnly TestClass # run specific unit test class composer testOnly TestClass::testMethod # run specific unit test method composer style-check # check code style for errors composer style-fix # automatically fix code style errors