djx / easy-json-schema
此包最新版本(1.1)没有提供许可证信息。
1.1
2020-11-29 09:43 UTC
Requires
- opis/json-schema: ^1.0
Requires (Dev)
- phpunit/phpunit: ^8.2
This package is auto-updated.
Last update: 2024-09-29 05:42:59 UTC
README
英语 | 简体中文
Easy Json Schema
当然,我推荐您使用protobuf。
- 使用PHP对象轻松定义Json-Schema(支持draft-07和draft-06)
- 使用opis/json-schema作为验证器
优点
- 易于定义,避免复杂的json-schema
- 更具可读性
- 更易于维护
安装
composer require "foamzou/easy-json-schema"
用法
use Foamzou\EasyJsonSchema\Manager\Validator;
use Foamzou\EasyJsonSchema\Manager\Parser;
use Foamzou\EasyJsonSchema\Type\{
Str, Integer
};
// define a schema
$schema = new Obj([
'name' => (new Integer)->min(2)->max(5),
'age' => (new Str)->max(120),
]);
$data = [
'name' => 'foam',
'age' => 18,
];
// generate json-schema
$jsonSchema = Parser::run($schema);
// check is valid with data,errorMessage will return while valid failed
$bool = Validator::getInstance()->isValid($jsonSchema, $data, $errorMessage);
定义模式
以下是与json-schema的比较。您可以看到使用PHP对象的定义更简单、更易读、更易于维护。
Easy-Json-Schema
new Obj([
'name' => (new Integer)->min(2)->max(5),
'age' => (new Str)->min(16)->max(120),
'skill' => (new Arr)->items((new Obj([
'name' => (new Str())->min(1)->max(64),
'value' => (new Num())->min(0)->max(100)->multipleOf(0.25),
]))->requiredAll()->additionalProp(false)),
]);
Json-Schema
{
"type": "object",
"properties": {
"name": {
"type": "integer",
"minimum": 2,
"maximum": 5
},
"age": {
"type": "string",
"minLength": 16,
"maxLength": 120
},
"skill": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"minLength": 1,
"maxLength": 64
},
"value": {
"type": "number",
"minimum": 0,
"maximum": 100,
"multipleOf": 0.25
}
},
"required": [
"name",
"value"
],
"additionalProperties": false
}
}
}
}
数据类型
use Foamzou\EasyJsonSchema\Type\{
Str, Integer, Num, Obj, Boolean, Arr, Nul
};
字符串
(new Str)->min(1)->max(23)->pattern('/regex/')->contentEncoding()->contentMediaType()
数字
(new Num)->min(1)->max(20); // >=1 and <=20
(new Num)->exMin(1)->exMax(20)->multipleOf(5);// >1 and <20 and divisible by 5
整数
(new Integer)->min(1)->max(20); // >=1 and <=20
(new Integer)->exMin(1)->exMax(20)->multipleOf(5);// >1 and <20 and divisible by 5
布尔值
new Boolean
空值
new Nul
数组
(new Arr)->items(new obj([
'name' => new Integer,
'age' => new Str,
]))->min('Array length minimum')
->max('Array length minimum')
->uniq('bool: Whether the element is required to be unique')
->additionalItems('extra element')
->contains('contains elements');
对象
(new Obj([
'name' => new Str,
'age' => new Integer,
'childObj' => new Obj([ // can be nested
'newName' => new Str,
'newAge' => new Integer,
])
]))->required(['name', 'age']);
关键字
枚举
new Enum([1, 2, 3]);
常量
new Constant('mustBeMe');
AnyOf, OneOf, AllOf
// new objects
new OneOf([
new Integer,
new Str,
]);
// call method
(new Arr())->items(new Integer)->oneOf([
[
"items"=> [
"exclusiveMinimum"=> 0
]
],
[
"items"=> [
"exclusiveMaximum"=> 0
]
],
[
"items"=> [
"const"=> 0
]
]
]);
not
new Not(new Str)
if.then.else
// new objects
(new Kif(new Integer()))
->then(["minLength" => 3])
->else(["const" => 0]);
// call method
(new Obj)->if([
'properties' => [
'gender' => new Constant('female')
]
])->then([
'properties' => [
'gender' => new Constant('female'),
'age' => (new Integer)->min(16),
]
])->else([
'properties' => [
'gender' => new Constant('male'),
'age' => (new Integer)->min(18),
]
]);
重用模式
当需要重用其他模式时,可以使用require
new Obj([
'name' => (new Str()),
'location' => require __DIR__ . '/Location.php',
]);
常用方法
所有对象都有一个desc方法来描述对象
(new Obj([
'name' => (new Str)->desc('username'),
'age' => (new Integer)->desc('user age'),
]))->desc('I am a request object');
基于Type的对象具有default方法来设置默认值
(new Str)->default('jay');
将EasyJsonSchema编译为Json Schema
use Foamzou\EasyJsonSchema\Manager\Parser;
$schema = (new Obj([
'name' => (new Str)->desc('username'),
'age' => (new Integer)->desc('user age'),
]))->desc('I am a request object');
$jsonSchema = Parser::run($schema);
检查数据和获取错误信息
如果数据与模式不匹配,则isValid将返回false,否则将返回true
use Foamzou\EasyJsonSchema\Manager\Validator;
$validator = Validator::getInstance();
$isValid = $validator->isValid($jsonSchema, json_encode($data), $errMessage, $errList);
当isValid返回false时,$errMessage和$errList将具有值
$errMessage是一个语义字符串,$errList是包含Opis\JsonSchema\ValidationError的项的数组
当$errMessage不是您希望的时候,您可以使用$errList来构建您想要的错误信息