PHP与PHP扩展实现的JSON结构验证工具。
JSON Schema 用于描述JSON数据的结构和类型,如同DTD与XML的关系。
本实现用于使用 PHP 调用 JSON Schema 对 JSON 数据进行验证。
建议PHP版本 PHP 5 >= 5.2.0、PECL json >= 1.2.0。
压缩包: json-schema-php_latest.zip
版本库:
git clone git://github.com/eixom/json-schema-php.git
dll:
- php_jsonschema-2.0-x86-5.2-zts.dll
- php_jsonschema-2.0-x86-5.2-nts.dll
- php_jsonschema-2.0-x86-5.3-zts.dll
- php_jsonschema-2.0-x86-5.3-nts.dll
格式 | 地址 | checksum | 备注 |
chm | chm | checksum | 无法查看:文件属性,解除锁定 |
html | zip | checksum | |
single | zip | checksum | |
checksum |
[ /]# cd /php-src/ext/jsonschema
[jsonschema]# /path/of/php/bin/phpize
[jsonschema]# ./configure --with-php-config=/path/of/php/bin/php-config
[jsonschema]# make
[jsonschema]# make install
[jsonschema]# cp modules/jsonschema.so /path/of/php/lib/php_jsonschema.so (just in case:) (extension_dir=lib/)
[jsonschema]# make clean
$value = new stdClass();
$value->name = 'a name';
$value->age = 23;
$value->height = 183.5;
$jsonSchema = new JsonSchema();
var_dump($jsonSchema->getSchema($value));
结果
array(
'type' => 'object',
'properties' =>
array(
'name' =>
array(
'type' => 'string',
'format' => 'regex',
'pattern' => '/^[a-z0-9]+$/i',
'minLength' => 0,
'maxLength' => 2147483647,
),
'age' =>
array(
'type' => 'integer',
'default' => 0,
'minimum' => 0,
'maximum' => 2147483647,
'exclusiveMinimum' => 0,
'exclusiveMaximum' => 2147483647,
),
'height' =>
array(
'type' => 'number',
'default' => 0,
'minimum' => 0,
'maximum' => 2147483647,
'exclusiveMinimum' => 0,
'exclusiveMaximum' => 2147483647,
)
)
)
$value = new stdClass();
$value->name = 'a name';
$value->age = 23;
$value->height = 183.5;
$schema = array(
'type' => 'object',
'properties' =>
array(
'name' =>
array(
'type' => 'string',
'format' => 'regex',
'pattern' => '/^[a-z0-9 ]+$/i',
'minLength' => 0,
'maxLength' => 2147483647,
),
'age' =>
array(
'type' => 'integer',
'default' => 0,
'minimum' => 0,
'maximum' => 2147483647,
'exclusiveMinimum' => 0,
'exclusiveMaximum' => 2147483647,
),
'height' =>
array(
'type' => 'number',
'default' => 0,
'minimum' => 0,
'maximum' => 2147483647,
'exclusiveMinimum' => 0,
'exclusiveMaximum' => 2147483647,
)
)
);
$jsonSchema = new JsonSchema();
var_dump($jsonSchema->validate($schema, $value)), PHP_EOL;
email: system128 at gmail dot com
qq: 59.43.59.0