Skip to content

eixom/json-schema-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Json Schema PHP

Build Status

PHP与PHP扩展实现的JSON结构验证工具。

JSON Schema 用于描述JSON数据的结构和类型,如同DTD与XML的关系。

本实现用于使用 PHP 调用 JSON Schema 对 JSON 数据进行验证。

建议PHP版本 PHP 5 >= 5.2.0、PECL json >= 1.2.0。

下载(2012-10-03 更新)

压缩包: json-schema-php_latest.zip

版本库:

git clone git://github.com/eixom/json-schema-php.git

dll:

手册下载(2012-10-03 更新)

格式 地址 checksum 备注
chm chm checksum 无法查看:文件属性,解除锁定
html zip checksum
single zip checksum
pdf pdf 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

生成 JSON Schema

$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,
        )
    )
)

使用 JSON Schema 验证 JSON

$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

以往版本

http://code.google.com/p/json-schema-php/

About

JSON Schema Implement by PHP & PHP extension

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages