hasbridge / json-schema-validator
PHP 5.3 实现的 json schema 验证
0.2.0
2012-02-27 14:25 UTC
Requires
- php: >=5.3.2
This package is not auto-updated.
Last update: 2024-09-22 03:52:39 UTC
README
此库使用 https://json-schema.fullstack.org.cn 中找到的 schema 进行 JSON schema 验证。请注意,它尚未实现所有功能,但支持基本的验证。JSON schema 草案可以在 http://tools.ietf.org/html/draft-zyp-json-schema-03 找到。
要求
- PHP 5.3 或更高版本(需要命名空间和闭包支持)
用法
$someJson = '{"foo":"bar"}';
$jsonObject = json_decode($someJson);
$validator = new JsonValidator('/path/to/yourschema.json');
$validator->validate($jsonObject);
支持的类型
类型可以定义为单个字符串类型名称,也可以定义为允许的类型名称数组。
- string
- number
- integer
- boolean
- object
- array
- null
- any
支持的定义
并非所有定义都得到支持,但以下是支持的列表
- properties (对象)
- additionalProperties (对象)
- required (所有)
- pattern (字符串)
- minLength (字符串)
- maxLength (字符串)
- format (字符串, 数字, 整数)
- minimum (数字, 整数)
- maximum (数字, 整数)
- exclusiveMinimum (数字, 整数)
- exclusiveMaximum (数字, 整数)
- divisibleBy (数字, 整数)
- enum (数组)
- minItems (数组)
- maxItems (数组)
- uniqueItems (数组)
- items (数组)
- disallow (所有)
以下定义尚未支持
- patternProperties
- dependencies
- extends
- id
- $ref
- $schema