当前位置: 软件>php软件
json-schema-php
本文导语: JSON Schema 用于描述JSON数据的结构和类型。如同DTD与XML的关系。 本实现用于使用 PHP 调用 JSON Schema 对 JSON 数据进行验证。 生成 JSON Schema 由JSON生成一个全格式的Schema,方便编辑修改(勿随便直接使用在实践中)。 $value = new stdCla...
JSON Schema 用于描述JSON数据的结构和类型。如同DTD与XML的关系。
本实现用于使用 PHP 调用 JSON Schema 对 JSON 数据进行验证。
生成 JSON Schema由JSON生成一个全格式的Schema,方便编辑修改(勿随便直接使用在实践中)。
$value = new stdClass(); $value->name = 'a name'; $value->age = 23; $value->height = 183.5; $jsonSchema = new JsonSchema(json_encode($value)); echo $jsonSchema->getSchema();
结果(真实结果格式化后)
{
"type":"object",
"properties":{
"name":{
"type":"string",
"format":"regex",
"pattern":"/^[a-z0-9]+$/i",
"minLength":0,
"maxLength":2147483647
},
"age":{
"type":"integer",
"default":0,
"minimum":0,
"maximum":2147483647,
"exclusiveMinimum":0,
"exclusiveMaximum":2147483647
},
"height":{
"type":"number",
"default":0,
"minimum":0,
"maximum":2147483647,
"exclusiveMinimum":0,
"exclusiveMaximum":2147483647
}
}
}
使用 JSON Schema 验证 JSON
$userType = '
{
"id": "user",
"description": "user info",
"type": "object",
"properties": {
"account": {"type": "string"},
"email": {"type": "string", "required": true},
"noexist": {"type": "string", "required": false}
}
}';
$type = array();
$type['users'][] = array('account' => 'userA', 'email' => 'userA@example.com');
$type['users'][] = array('account' => 'userB', 'email' => 'userB@example.com');
$type['users'][] = array('account' => 'userC', 'email' => 'userC@example.com');
$jsonSchema = new JsonSchema(json_encode($type));
$jsonSchema->addTypes($userType);
$jsonSchema->validate('
{
"type":"object",
"properties":{
"users":{
"type":"array",
"items":{
"$ref":"user"
}
}
}
}');
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。