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

发布于 2025-01-12 01:23:42 字数 752 浏览 7 评论 0原文

我有一个与嵌套字典相对应的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

或十年 2025-01-19 01:23:42

根据经验,当涉及开源工具时,请仔细阅读文档和测试套件。

来自他们众多测试之一的示例

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),
    ]

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:

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),
    ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文