krak/schema

用于定义和处理的声明式API的Schema

v0.2.0 2020-04-13 18:54 UTC

This package is not auto-updated.

Last update: 2024-09-10 15:22:19 UTC


README

该Schema库提供使用声明式API定义Schema的能力,并进而使用任意处理器对该Schema进行操作。

我们将Schema定义的概念和处理器分开,以便我们可以构建描述Schema的AST,然后允许不同的处理器处理该结构,例如验证、生成symfony配置树、构建有效的JSON Schema等。

安装

使用composer安装 krak/schema

使用方法

定义Schema

<?php
use function Krak\Schema\{struct, listOf, dict, string, bool, int};

$schema = struct([
    'name' => string(),
    'isAdmin' => bool(),
    'age' => int(),
    'tags' => listOf(string()),
    'photos' => dict(struct([
        'url' => string(),
        'width' => int(),
        'height' => int(),
    ]))
]);
/* would match a structure like: 
{
  "name": "Bob",
  "isAdmin": true,
  "age": 26,
  "tags": ["tall", "dark", "handsome"],
  "photos": {
    "small": {
      "url": "https://mydomain.com/images/bob/small",
      "width": 100,
      "height": 200
    },
    "large": {
      "url": "https://mydomain.com/images/bob/large",
      "width": 600,
      "height": 1200
    },
  }
}
*/

验证(即将推出)

最终,我们将支持对数组结构进行验证的能力。

symfony配置树处理器

使用configTree Schema处理器声明性地声明和构建symfony配置树生成器。

<?php

use Symfony\Component\Config\Definition\{ConfigurationInterface, TreeBuilder};
use function Krak\Schema\ProcessSchema\SymfonyConfig\configTree;
use function Krak\Schema\{struct, string};

final class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder() {
        return configTree('aws', struct([
            'version' => string(),
            'region' => string(),
            'credentials' => struct([
                'key' => string(),
                'secret' => string(),
            ])
        ]));
    }
}

注意: 目前支持Symfony 4和5配置。

查看功能测试套件以了解所有支持API的示例。

声明式语法与构建语法的比较

这是一个看似简单的配置文件,我们想要验证其Schema

my_package:
  string_key: 'abc'
  int_key: 1
  struct_key:
    a: 1
    b: 2
  list_key: [1, 2, 3]
  list_of_struct_key:
    - a: 1
      b: 2
  struct_of_list:
    a: ['', '']
    b: [0, 0]

这是构建语法

return (new TreeBuilder('my_package'))->getRootNode();
    ->children()
        ->scalarNode('string_key')->end()
        ->integerNode('int_key')->end()
        ->arrayNode('struct_key')
            ->children()
                ->scalarNode('a')->end()
                ->integerNode('b')->end()
            ->end()
        ->end()
        ->arrayNode('list_key')
            ->integerPrototype()->end()
        ->end()
        ->arrayNode('list_of_struct_key')
            ->arrayPrototype()
                ->children()
                    ->integerNode('a')->end()
                    ->integerNode('b')->end()
                ->end()
            ->end()
        ->end()
        ->arrayNode('struct_of_list_key')
            ->children()
                ->arrayNode('a')
                    ->scalarPrototype()->end()
                ->end()
                ->arrayNode('b')
                    ->integerPrototype()->end()
                ->end()
            ->end()
        ->end()
    ->end()
->end();

这是相同定义的声明式语法

return configTree('my_package', struct([
    'string_key' => string(),
    'int_key' => int(),
    'struct_key' => struct([
        'a' => int(),
        'b' => int(),
    ]),
    'list_key' => listOf(int()),
    'list_of_struct_key' => listOf(struct([
        'a' => int(),
        'b' => int(),
    ])),
    'struct_of_list_key' => struct([
        'a' => listOf(string()),
        'b' => listOf(int()),
    ])
]));

参考

原始RFC Pull Request到Symfony: symfony/symfony#35127

文档

尚未设置正式的API文档,但src目录目前不到200loc。此外,测试目录也提供了各种功能的良好概述。

测试

运行composer test以运行测试套件。

路线图

  • API文档
  • 支持更多字符串/数字约束(正则表达式、最小值、最大值等)的附加schema函数
  • JSON Schema ProcessSchema
    • 创建将schema定义导出为有效JSON Schema JSON的能力
  • Validation ProcessSchema
    • 创建用于基本schema的功能验证库
    • 支持自定义验证器和schema函数
  • symfony Validation ProcessSchema
    • 导出到symfony约束以用于SF验证器。使用一个API,我们可以定义导出到SF配置和SF验证的schema!