使用 python 中的 dynaconf 或 schema 验证 yaml 文件
我有一个与嵌套字典相对应的 yaml 文件,我想用 dynaconf 对其进行验证。我正在遵循这里的指南: https://www.dynaconf.com/validation/ 但是他们的例子非常简单,我找不到一种方法来验证与嵌套字典相对应的数据,就像下面的例子一样:
---
item1:
- prod1: val1
- prod2
- proda: vala
- prodb: |
valb1
valb2
version: 1
---
item2:
prodx: valx
例如,我想确保 vala、valb2、valx 都应该存在。
settings = Dynaconf(
settings_files=['file.yml'],
validators=[
Validator("proda", must_exist=True, eq='vala'),
Validator("version", must_exist=True, is_type_of=int),
...
]
或者,如果您知道其他东西,例如“模式”库,它可能更适合这项工作,请告诉我。 ps 我还想将多个 yaml 配置保留在一个文件中,用 --- 分隔
I have a fairly yaml file corresponding to nested dict, which I want to validate with dynaconf. I'm following the guide here: https://www.dynaconf.com/validation/ but their example is pretty simple, and I can't find a way to validate data which corresponds to nested dictionaries, like in my example below:
---
item1:
- prod1: val1
- prod2
- proda: vala
- prodb: |
valb1
valb2
version: 1
---
item2:
prodx: valx
For instance I would like to make sure that vala, valb2, valx all should exist.
settings = Dynaconf(
settings_files=['file.yml'],
validators=[
Validator("proda", must_exist=True, eq='vala'),
Validator("version", must_exist=True, is_type_of=int),
...
]
Or, if you know something else, like "schema" library, which could be more suitable for this job let me know.
p.s. I also want to keep multiple yaml configs in one file, separated by ---
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据经验,当涉及开源工具时,请仔细阅读文档和测试套件。
来自他们众多测试之一的示例:
As a rule of thumb, when it comes to open source tools, go through both the docs and the tests suit.
Ex from one of their many tests: