pxhub/php-json-schema-generator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

pxhub/php-json-schema-generator

最新稳定版本:v1.0.3

Composer 安装命令:

composer require pxhub/php-json-schema-generator

包简介

A JSON Schema Generator.

README 文档

README

Build Status codecov

Originaly forked from evaisse/php-json-schema-generator

List of changes:

  • Collect examples for each scalar type.
  • Items properties merged to single list instead of 'anyOf' list.

Introduction to json schema below (and tools) :

To validate your structure against a given schema, you can use :

Quickstart

Install using composer

composer require evaisse/php-json-schema-generator

Most simple case

$output = JSONSchemaGenerator\Generator::fromJson('{"a":{"b":2}');
 
// $output ==> json string
// {
//   "$schema": "http://json-schema.org/draft-04/schema#",
//   "type": "object",
//   "properties": {
//     "a": {
//       "type": "object",
//       "properties": {
//         "b": {
//           "type": "integer"
//         }
//       },
//       "required": ["b"]
//     }
//   },
//   "required": ["a"]
// }

Default configuration values

[
    'schema_id'                      => null,
    'properties_required_by_default' => true,
    'schema_uri'                     => 'http://json-schema.org/draft-04/schema#',
    'schema_title'                   => null,
    'schema_description'             => null,
    'schema_type'                    => null,
    "items_schema_collect_mode"      => 0,
    'schema_required_field_names'    => []
]

Advanced usage

$result = Generator::fromJson($this->addressJson1, [
    'schema_id' => 'http://foo.bar/schema'
]);

/*

  {
    "$schema": "http://json-schema.org/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 properties list
$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",...}}
*/

For more advanced usage, see tests/JSONSchemaGenerator/Tests/GeneratorTest.php

Testing

just run phpunit through

composer test

debug with

DEBUG=true composer test -- --filter="SearchWord" # for filtering *SearchWord* test case with output debugging

Roadmap

  • Adjust schema comparison using re-ordering of properties to compare two schema against their semantic values instead of just comparing their JSON form. For example { a: 1, b: 2 }, and { b: 2, a: 1 } should result in the same schema.

  • provide an option to allow null values in most fields ("type": ["string", "null"]}

统计信息

  • 总下载量: 957
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 22
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-13