openregion / php-json-schema-generator
JSON Schema 生成器。
2.2.0
2024-03-14 12:50 UTC
Requires
- php: >=7.4.0
- ext-json: *
Requires (Dev)
- justinrainbow/json-schema: ^5.2
- league/json-guard: 1.*
- league/json-reference: 1.*
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^9.5.0
README
从以下地址分支而来 evaisse/php-json-schema-generator
最初从以下地址分支而来 solvire/php-json-schema-generator
以下是对 json schema(及其工具)的介绍
- https://json-schema.fullstack.org.cn — 参考
- http://www.jsonschemavalidator.net - 验证器(并非100%有效)
- https://openapis.org.cn - 使用 json schema 定义 REST API 文档
- https://jsonschema.net/#/editor - json schema 方便的编辑器
要验证您的结构是否符合给定的模式,您可以使用
快速入门
使用 composer 安装
composer require openregion/php-json-schema-generator
最简单的情况
$output = JSONSchemaGenerator\Generator::fromJson('{"a":{"b":2}'); // $output ==> json string // { // "$schema": "https://json-schema.fullstack.org.cn/draft-04/schema#", // "type": "object", // "properties": { // "a": { // "type": "object", // "properties": { // "b": { // "type": "integer" // } // }, // "required": ["b"] // } // }, // "required": ["a"] // }
默认配置值
[ 'schema_id' => null, 'properties_required_by_default' => true, 'schema_uri' => 'https://json-schema.fullstack.org.cn/draft-04/schema#', 'schema_title' => null, 'schema_description' => null, 'schema_type' => null, "items_schema_collect_mode" => 0, 'schema_required_field_names' => [] ]
高级用法
$result = Generator::fromJson($this->addressJson1, [ 'schema_id' => 'http://foo.bar/schema' ]); /* { "$schema": "https://json-schema.fullstack.org.cn/draft-04/schema#", "id": "http://foo.bar/schema", "type": "object", "properties": { "a": { "type": "object", "id": "http://foo.bar/schema/a", "properties": { "b": { "id": "http://foo.bar/schema/a/b", "type": "integer" } } } } */ // if you want items as strict lists instead of "anyOf" type $result = Generator::fromJson($this->addressJson1, [ 'schema_id' => 'http://bar.foo/schema2', 'schema_title' => 'coucouc', 'schema_description' => 'desc', "items_schema_collect_mode" => Definition::ITEMS_AS_LIST, ]); /* { "$schema":"http:\/\/json-schema.org\/draft-04\/schema#", ... "properties": { "phoneNumber":{ "id":"http:\/\/bar.foo\/schema2\/phoneNumber", "type":"array", "items": [ {"id":"http:\/\/bar.foo\/schema2\/0",...}, {"id":"http:\/\/bar.foo\/schema2\/1",...}} */
对于更高级的用法,请参阅 tests/JSONSchemaGenerator/Tests/GeneratorTest.php
测试
只需运行 phpunit 即可
composer test
使用以下方式调试
DEBUG=true composer test -- --filter="SearchWord" # for filtering *SearchWord* test case with output debugging
路线图
-
调整模式比较,使用属性重新排序来比较两个模式,以比较它们的语义值,而不是仅仅比较它们的 JSON 形式。例如,
{ a: 1, b: 2 }
和{ b: 2, a: 1 }
应该产生相同的模式。 -
在大多数字段中提供一个允许空值的选项
("type": ["string", "null"]}