使用dynaconf或python中的模式验证yaml文件



我有一个相当yaml的文件,对应于嵌套的dict,我想用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),
...
]

或者,如果你知道其他事情,比如";模式";图书馆,哪个更适合这份工作,请告诉我。p.s.我还想把多个yaml配置保存在一个文件中,用---分隔

根据经验,当涉及到开源工具时,请同时查看文档和测试套件。

来自他们众多测试之一的Ex:

YAML = """
server:
hostname: "localhost"
port: 22
users:
- "Bruno"
- "Lula"
app:
name: "testname"
path: "/tmp/app_startup"
args:
arg1: "a"
arg2: "b"
arg3: "c"
hasemptyvalues:
key1:
key2:
key3: null
key4: "@empty"
"""
@pytest.fixture
def yaml_validators_good():
return [
Validator(
"server.hostname", "server.port", "server.users", must_exist=True
),
Validator(
"app.name",
"app.path",
"app.args.arg1",
"app.args.arg2",
"app.args.arg3",
must_exist=True,
),
]

@pytest.fixture
def yaml_validators_bad():
return [
Validator("missing.value", must_exist=True),
Validator("app.missing", must_exist=True),
Validator("app.args.missing", must_exist=True),
]

最新更新