YAML 文件的接口/默认定义

发布于 2025-01-16 15:30:17 字数 229 浏览 6 评论 0原文

我有一个配置文件,想要标准化密钥。 因此,当其他开发人员创建自己的配置时,它们将得到验证。

当使用编程语言时,你使用的是接口。

我还知道,某些基于 YAML 的工具(例如 Ansible)也有默认值/定义,以确保您正确编写内容。但据我所知,它们还可以与插件等一起使用...

那么有没有办法在 YAML 文件中实现这种效果?

我的意思是我可以以某种方式以编程方式验证它,但也许有更好的东西。

I have a configuration file and want to standardize the keys.
So when other developers create their own configurations, they will be validated.

When using programming languages you use interfaces.

I also know that somehow some YAML-based Tools like Ansible also have defaults/definitions to ensure you are writing your stuff correctly. But as far as I know, they also work with addons and such...

So is there are a way to achieve this kind of effect in YAML files?

I mean I could programmatically validate it somehow, but maybe there is something better.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

无敌元气妹 2025-01-23 15:30:17

您有两个选择:

  • 使用 JSON 架构。一些编辑器和验证器提供直接的 YAML 支持,而对于其他编辑器和验证器,您需要先将 YAML 转换为 JSON 才能进行验证(这可以自动完成)。请参阅实现。这将限制您只能使用 JSON 中可用的功能(例如,没有复杂的键、锚点和正在解析的别名等)。
  • 使用目标类型作为架构。 YAML 是一种序列化语言,这意味着您选择的编程语言中的本机类型充当架构。这显然不适用于具有动态类型的语言,例如 Python 或 JavaScript。它可以在 Java(通过 SnakeYAML)、C#(通过 YamlDotNet)和其他语言中工作。您无法获得自动编辑器支持,但您可以编写一个小型验证器工具,甚至现在使用 LSP 为您最喜欢的编辑器编写验证器工具也很简单。

Ansible(以及许多其他基于 YAML 的框架)实际上并不是一个很好的例子,因为它在 YAML 之上使用 Jinja,因此验证起来要复杂得多。

You have two options:

  • Use JSON Schema. Some editors and validators offer direct YAML support, for others you need to transform the YAML to JSON first to validate (which can be done automatically). See implementations. This will restrict you to use only features available in JSON (e.g. no complex keys, anchors & aliases being resolved etc).
  • Use the target type as schema. YAML is a serialization language, this means that the native type in the programming language of your choice serves as schema. This obviously doesn't work well in languages with dynamic typing, like Python or JavaScript. It does work in Java (via SnakeYAML), C# (via YamlDotNet) and others. You don't get automatic editor support but you can write a small validator tool and even writing a validator tool for your favorite editor is simple today with LSP.

Ansible (and lots of other YAML-based frameworks) isn't really a good example because it uses Jinja on top of YAML and therefore is far more complex to validate.

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