【问题标题】:Generating a json schemas that validate both root lists and mappings using pydantic使用 pydantic 生成验证根列表和映射的 json 模式
【发布时间】:2021-05-24 01:33:40
【问题描述】:

我正在尝试编写一个可用于验证两种不同需求格式的架构,一个 v1 曾经是根级别的列表,另一个是使用映射的新版本:

# requirements.yml v1
- {}  # the mapping inside being a RoleModel
...
# requirements.yml v2
roles:
  - {}  # the mapping inside being a RoleModel
  ...
collections:
  - {}  # the mapping inside being a CollectionModel
  ...

我能够为这两个版本中的任何一个生成不同的架构,但我不知道如何将这两个版本组合成一个架构。

出于实际原因,我不能使用不同的架构,因为文件名相同并且 Ansible 加载两者,因此在打开文件之前无法确定架构。

对于 v2 格式,我在 https://github.com/ansible-community/ansible-lint/blob/schemas/src/ansiblelint/schemas/requirements.py 确实有一个实现

我确实了解到,为了在根级别验证列表,我需要执行 https://github.com/ansible-community/ansible-lint/blob/schemas/src/ansiblelint/schemas/playbook.py#L35 之类的操作:

top_level_schema = schema([RoleModel], title='Requiremetns v1 Schema')

如何将两者结合在一个架构中,一个涵盖上面列出的两个示例的架构?

【问题讨论】:

标签: python jsonschema pydantic


【解决方案1】:

对于 JSON Schema,您希望使用具有一组模式值的应用程序。

根据您向我展示的代码,剧本架构实际上并没有生成有用的架构(只有定义,没有实际验证)。

根据 pydantic 的文档,您似乎想使用 Union 类型。

https://pydantic-docs.helpmanual.io/usage/schema/#json-schema-types

联合[str, int]
任意
{"anyOf": [{"type": "string"}, {"type": "integer"}]}

同样的方法可能适用于您定义的类,但我无法测试。

如果没有,我建议你在他们的 github repo 上提出问题。 虽然,我已经找到了类似的功能请求:https://github.com/samuelcolvin/pydantic/issues/2036

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-22
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多