定制 ropi/json-schema-generator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

ropi/json-schema-generator

Composer 安装命令:

composer require ropi/json-schema-generator

包简介

JSON Schema Generator

README 文档

README

This library is a PHP based implementation for generating JSON Schemas, which can be easily extended with your own keywords and drafts. The library is most suitable for the case when you have many instances/datasets and a schema must be generated based on these instances/datasets.

The following drafts are natively supported:

Requirements

  • PHP ^8.1

Installation

The library can be installed from a command line interface by using composer.

composer require ropi/json-schema-generator

Basic usage

<?php
$instances = [
    (object) [
        'firstname' => 'Foo',
        'age' => 18
    ],
    (object) [
        'firstname' => 'Bar',
        'age' => 29,
        'country' => 'DE'
    ]
];

$config = new \Ropi\JsonSchemaGenerator\Config\GenerationConfig(
    draft: new \Ropi\JsonSchemaGenerator\Draft\Draft202012()
);

$generator = new \Ropi\JsonSchemaGenerator\JsonSchemaGenerator($config);

foreach ($instances as $instance) {
    $generator->recordInstance($instance);
}

echo json_encode($generator->generateSchema(), JSON_PRETTY_PRINT);

Output of above example:

{
    "$schema": "https:\/\/json-schema.org\/draft\/2020-12\/schema",
    "type": "object",
    "required": [
        "firstname",
        "age"
    ],
    "properties": {
        "firstname": {
            "type": "string",
            "enum": [
                "Foo",
                "Bar"
            ]
        },
        "age": {
            "type": "integer",
            "enum": [
                18,
                29
            ]
        },
        "country": {
            "type": "string",
            "enum": [
                "DE"
            ]
        }
    },
    "additionalProperties": false
}

The generated schema is as restrictive as possible. The more instances are recorded, the better the generated schema is. Here is an example with more instances:

<?php
$instances = [
    (object) [
        'firstname' => 'Foo',
        'age' => 18
    ],
    (object) [
        'firstname' => 'Bar',
        'age' => 29,
        'country' => 'DE'
    ],
    (object) [
        'firstname' => 'Abc',
        'age' => 31,
        'country' => 'DE'
    ],
    (object) [
        'firstname' => 'Def',
        'age' => 44,
        'country' => 'AT'
    ],
    (object) [
        'firstname' => 'Ghi',
        'age' => 23,
        'country' => 'FR'
    ],
    (object) [
        'firstname' => 'Jkl',
        'age' => 33
    ],
    (object) [
        'firstname' => 'Mnop',
        'age' => 34
    ],
    (object) [
        'firstname' => 'Qrstuvw',
        'age' => 50
    ],
    (object) [
        'firstname' => 'xyz',
        'age' => 56
    ]
];

$config = new \Ropi\JsonSchemaGenerator\Config\GenerationConfig(
    draft: new \Ropi\JsonSchemaGenerator\Draft\Draft202012(),
    maxEnumSize: 8
);

$generator = new \Ropi\JsonSchemaGenerator\JsonSchemaGenerator($config);

foreach ($instances as $instance) {
    $generator->recordInstance($instance);
}

echo json_encode($generator->generateSchema(), JSON_PRETTY_PRINT);

Output of above example:

{
  "$schema": "https:\/\/json-schema.org\/draft\/2020-12\/schema",
  "type": "object",
  "required": [
    "firstname",
    "age"
  ],
  "properties": {
    "firstname": {
      "type": "string",
      "minLength": 3,
      "maxLength": 7,
      "examples": [
        "Foo",
        "Bar",
        "Abc",
        "Def",
        "Ghi",
        "Jkl",
        "Mnop",
        "Qrstuvw",
        "xyz"
      ]
    },
    "age": {
      "type": "integer",
      "minimum": 18,
      "maximum": 56,
      "examples": [
        18,
        29,
        31,
        44,
        23,
        33,
        34,
        50,
        56
      ]
    },
    "country": {
      "type": "string",
      "enum": [
        "DE",
        "AT",
        "FR"
      ]
    }
  },
  "additionalProperties": false
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-08-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固