wwwision/types-jsonschema

PHP类生成JSON Schema文件的工具,请参阅https://json-schema.fullstack.org.cn/

1.0.0 2024-02-29 16:17 UTC

This package is auto-updated.

Last update: 2024-08-29 18:00:40 UTC


README

wwwision/types包集成的工具,允许从PHP代码生成JSON Schema文件

用法

该包可以通过composer安装

composer require wwwision/types-jsonschema

使用它,您可以从PHP类创建JSON Schema

class Contact {
    public function __construct(public string $name, public int $age) {}
}

$schema = JSONSchemaGenerator::fromClass(Contact::class);

$expected = <<<JSON
{
    "type": "object",
    "properties": {
        "name": {
            "type": "string"
        },
        "age": {
            "type": "integer"
        }
    },
    "additionalProperties": false,
    "required": [
        "name",
        "age"
    ]
}
JSON;

assert(json_encode($schema, JSON_PRETTY_PRINT) === $expected);

高级模式

支持wwwision/types包的所有属性,以创建复杂的模式。

示例:复杂复合对象
#[StringBased]
final class GivenName {
    private function __construct(public readonly string $value) {}
}

#[StringBased]
final class FamilyName {
    private function __construct(public readonly string $value) {}
}

final class FullName {
    public function __construct(
        public readonly GivenName $givenName,
        public readonly FamilyName $familyName,
    ) {}
}

#[Description('honorific title of a person')]
enum HonorificTitle
{
    #[Description('for men, regardless of marital status, who do not have another professional or academic title')]
    case MR;
    #[Description('for married women who do not have another professional or academic title')]
    case MRS;
    #[Description('for girls, unmarried women and married women who continue to use their maiden name')]
    case MISS;
    #[Description('for women, regardless of marital status or when marital status is unknown')]
    case MS;
    #[Description('for any other title that does not match the above')]
    case OTHER;
}

#[Description('A contact in the system')]
final class Contact {
    public function __construct(
        public readonly HonorificTitle $title,
        public readonly FullName $name,
        #[Description('Whether the contact is registered or not')]
        public bool $isRegistered = false,
    ) {}
}

$schema = JSONSchemaGenerator::fromClass(Contact::class);

$expected = <<<JSON
{
    "type": "object",
    "description": "A contact in the system",
    "properties": {
        "title": {
            "type": "string",
            "description": "honorific title of a person",
            "enum": [
                "MR",
                "MRS",
                "MISS",
                "MS",
                "OTHER"
            ]
        },
        "name": {
            "type": "object",
            "properties": {
                "givenName": {
                    "type": "string"
                },
                "familyName": {
                    "type": "string"
                }
            },
            "additionalProperties": false,
            "required": [
                "givenName",
                "familyName"
            ]
        },
        "isRegistered": {
            "type": "boolean",
            "description": "Whether the contact is registered or not"
        }
    },
    "additionalProperties": false,
    "required": [
        "title",
        "name"
    ]
}
JSON;

assert(json_encode($schema, JSON_PRETTY_PRINT) === $expected);

贡献

问题拉取请求的形式进行的贡献将受到高度赞赏

许可证

请参阅LICENSE