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: .*$'
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:
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
JSON schema in YAML is considered as the common denominator of most modern computer languages. It allows parsing JSON files. It is strongly recommended in YAML that other schemas should be considered on JSON schema. The primary reason for this is that it includes key value combination which are user friendly.
YAML Schema is a small extension to JSON Schema Draft 4 that adds some features specific to YAML. Understanding JSON Schema provides a good resource for understanding how to use JSON Schema, and further resources are available at json-schema.org.
Try this:
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
A map can contain any string as a key, whereas a rec can only contain the keys specified in 'required' and 'optional'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With