为什么这不是 Rx 的有效模式?
我使用 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”是一个固定字符串和必需的项目;也是固定的,但应该是可选的。过滤器又具有另一组必需和可选的项目,我应该如何用 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
有什么想法吗?
我已经厌倦了与这个东西战斗......还有其他方法可以编写模式并使用吗?它来验证我的配置文件吗?
提前致谢,
Ivan
I'm using YAML as a configuration file format for a Python project.
Recently I found Rx to be the only schema validator available for Python and YAML. :-/ Kwalify works with YAML, but it's only for Ruby and Java. :(
I've been reading their lacking documentation all day and just can't seem to write a valid schema to represent my file structure. Help?
I have the following YAML config file:
cmd:
exec: mycmd
aliases: [my, cmd]
filter:
sms: 'regex .*'
load:
exec: load
filter:
sms: 'load: .*
I'm failing at representing a nested structure. What I want is for the outer-most item (cmd, load and echo, in this case) to be an arbitrary string that in turn contains other items. 'exec' is a fixed string and required item; 'aliases' and 'filter' are also fixed, but should be optional. Filter in turn has another set of required and optional items. How should I represent this with Rx?
So far I have the following schema (in YAML), which Rx fails to compile:
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
Testing this in IPython gives me this:
/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'
Which leads me to believe I'm not specifying "type" somewhere. :-S
Any ideas?
I'm pretty tired fighting with this thing... Is there some other way I can write a schema and use it to validate my configuration files?
Thanks in advance,
Ivan
echo:
exec: echo %
I'm failing at representing a nested structure. What I want is for the outer-most item (cmd, load and echo, in this case) to be an arbitrary string that in turn contains other items. 'exec' is a fixed string and required item; 'aliases' and 'filter' are also fixed, but should be optional. Filter in turn has another set of required and optional items. How should I represent this with Rx?
So far I have the following schema (in YAML), which Rx fails to compile:
Testing this in IPython gives me this:
Which leads me to believe I'm not specifying "type" somewhere. :-S
Any ideas?
I'm pretty tired fighting with this thing... Is there some other way I can write a schema and use it to validate my configuration files?
Thanks in advance,
Ivan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
映射可以包含任何字符串作为键,而记录只能包含“必需”和“可选”中指定的键。
Try this:
A map can contain any string as a key, whereas a rec can only contain the keys specified in 'required' and 'optional'.