mszewcz / php-json-schema-validator
支持 draft-06 规范的 JSON Schema 验证器
Requires
- php: >=7.1.0
- mszewcz/php-json-utils: >=1.0
Requires (Dev)
- codacy/coverage: dev-master
- phpunit/phpunit: ^6.3
This package is auto-updated.
Last update: 2024-09-16 09:07:35 UTC
README
JSON schema validator 类,根据 draft-06 规范进行 JSON 文件的验证,发布于 2017-04-15。
内容
安装
如果您使用 Composer 管理依赖项,只需在项目的 composer.json 文件中添加对 mszewcz/php-json-schema-validator
的依赖即可。以下是一个 composer.json 的最小示例:
{
"require": {
"mszewcz/php-json-schema-validator": ">=1.0"
}
}
您也可以克隆或下载此存储库。
php-json-schema-validator 符合 PSR-4 自动加载标准。如果您使用 Composer,请包括其自动加载文件。
require_once 'vendor/autoload.php';
如果您克隆或下载了此存储库,您将需要自己编写 PSR-4 样式的自动加载实现。
用法
require 'vendor/autoload.php'; try { $utils = new MS\Json\Utils\Utils(); $schema = $utils->decode($jsonSchemaDefinition); $json = $utils->decode($jsonToValidate); $validator = new MS\Json\SchemaValidator\Validator($schema); $result = $validator->validate($json); } catch (\Exception $e) { echo $e->getMessage(); }
如果您不想使用 Utils 类来解码 JSON,应按照以下方式进行操作
$schema = \json_decode($jsonSchemaDefinition, true); $json = \json_decode($jsonToValidate, true);
支持的元素
php-json-schema-validator 支持以下验证
- additionalItems
- additionalProperties
- allOf
- anyOf
- const
- contains
- dependencies
- enum
- exclusiveMaximum
- exclusiveMinimum
- format (date-time, email, host, ipv4, ipv6 & uri)
- items
- maximum
- minimum
- maxItems
- maxLength
- maxProperties
- minItems
- minLength
- minProperties
- multipleOf
- not
- oneOf
- pattern
- patternProperties
- properties
- propertyNames
- required
- type
- uniqueItems
它还支持 $ref
元素,因此您可以无任何问题地使用 in-json 定义和引用。
贡献
欢迎贡献。请通过 GitHub pull requests 发送您的贡献
修复错误的 pull requests 必须基于来自 master
分支的最新稳定版本,而新功能的 pull requests 必须基于 developer
分支。
由于时间限制,我可能无法像我希望的那样快速响应。如果您感觉您等待合并 pull request 过长,请在此提醒我。
编码标准
我们遵循 PSR-2 编码风格和 PSR-4 自动加载标准。在发送 pull request 之前,请确保您也遵循这些标准。
许可
php-json-schema-validator 采用 MIT 许可证 - 有关详细信息,请参阅 LICENSE
文件。