【发布时间】: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')
如何将两者结合在一个架构中,一个涵盖上面列出的两个示例的架构?
【问题讨论】:
-
我可以告诉你如何在 JSON Schema 中做到这一点,但我对 pydantic 一无所知。看起来 JSON Schema 是从 Python 类型生成的?
-
你能分享两个脚本的结果架构吗?
-
当然,生成的模式在github.com/ansible-community/ansible-lint/tree/schemas/src/… 和源代码在github.com/ansible-community/ansible-lint/tree/schemas/src/… --- 棘手的是需求之一,作为唯一一个有两种形式的序列(v1 ) 或作为 (v2) 中的映射。
标签: python jsonschema pydantic