6

我使用 YAML 作为 Python 项目的配置文件格式。

最近我发现Rx是唯一可用于 Python 和 YAML 的模式验证器。:-/ Kwalify适用于 YAML,但仅适用于 Ruby 和 Java。:(

我整天都在阅读他们缺少的文档,但似乎无法编写有效的模式来表示我的文件结构。帮助?

我有以下 YAML 配置文件:

cmd:
  exec: mycmd
  aliases: [my, cmd]
  filter:
    sms: 'regex .*'

load:
  exec: load
  filter:
    sms: 'load: .*$'

echo:
  exec: echo %

我无法表示嵌套结构。我想要的是最外面的项目(在这种情况下是 cmd、load 和 echo)是一个任意字符串,而该字符串又包含其他项目。'exec' 是一个固定字符串和必填项;'aliases' 和 'filter' 也是固定的,但应该是可选的。过滤器又具有另一组必需项和可选项。我应该如何用 Rx 来表示这个?

到目前为止,我有以下架构(在 YAML 中),Rx 无法编译:

type: //rec
required:
  type: //rec
  required:
    exec: //str
  optional:
    aliases:
      type: //arr
      contents: //str
      length: {min: 1, max: 10}
    filter:
      type: //rec
      optional:
        sms: //str
        email: //str
        all: //str

在 IPython 中测试这个给了我这个:

/Rx.py in make_schema(self, schema)
     68       raise Error('invalid schema argument to make_schema')
     69
---> 70     uri = self.expand_uri(schema["type"])
     71
     72     if not self.type_registry.get(uri): raise "unknown type %s" % uri

KeyError: 'type'

这让我相信我没有在某处指定“类型”。:-S

有任何想法吗?

我已经厌倦了与这件事作斗争......还有其他方法可以编写模式并使用它来验证我的配置文件吗?

提前致谢,

伊万

4

1 回答 1

4

尝试这个:

type: //map
values:
  type: //rec
  required:
    exec: //str
  optional:
    aliases:
      type: //arr
      contents: //str
      length: {min: 1, max: 10}
    filter:
      type: //rec
      optional:
        sms: //str
        email: //str
        all: //str

map 可以包含任何字符串作为键,而 rec 只能包含在 'required' 和 'optional' 中指定的键。

于 2009-06-30T14:06:17.030 回答