Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't this a valid schema for Rx?

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

like image 296
imiric Avatar asked Jun 30 '09 02:06

imiric


People also ask

Which schemas can be used in Yaml?

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.

Is there a YAML schema?

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.


1 Answers

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'.

like image 122
Daniel Lucraft Avatar answered Oct 23 '22 10:10

Daniel Lucraft