yamlious

Build voluptuous schema from yaml files


Keywords
yaml, voluptuous, schema, validation
License
Apache-2.0
Install
pip install yamlious==0.2.0

Documentation

Yamlious Python module

Yamlious is a Python module that allows you to declare voluptous schemas in YAML format.

This module is at a very early stage, only tested according to our use-cases.

Installation

$ pip install yamlious

Usage

import voluptuous
import yamlious

# build voluptuous schema
with open('my-schema.yaml') as istr:
  _schema, options = yamlious.from_yaml(istr)
  schema = voluptuous.schema(_schema, **options)
# check your data
schema({
  'firstname': 'John',
  'lastname': 'Doe',
})

Syntax

a yamlious schema is a dict of 2 keys:

  • content: a mandatory dict describing the first argument given to the voluptuous.Schema function.
  • options: an optional dict passed as **kwargs to the voluptuous.Schema function.

Show me an example

The following examples are extracted from the voluptuous documentation.

Twitter user search API schema

>>> from voluptuous import Schema
>>> schema = Schema({
...   'q': str,
...   'per_page': int,
...   'page': int,
... })

Yamlious can create the very same schema with the following YAML input:

content:
    q: str
    per_page: str
    page: int

Twitter user search API schema with semantic

>>> from voluptuous import Required, All, Length, Range
>>> schema = Schema({
...   Required('q'): All(str, Length(min=1)),
...   Required('per_page', default=5): All(int, Range(min=1, max=20)),
...   'page': All(int, Range(min=0)),
... })

the corresponding Yamlious file is:

content:
    q:
        key: Required
        All:
            - str
            - Length:
                min: 1
    per_page:
        key:
            Required:
                default: 5
        All:
          - int
          - Range:
              min: 1
              max: 20
    page:
        All:
            - int
            - Range:
                min: 0

Regarding the q key description:

  • key specifies the key part of Required('q'): All(str, Length(min=1)),
  • There is no need to repeat q to Required argument, because inside key, there is no need to do it.
  • The key function can be passed as string if there is no extra argument to pass, like q for instance.
  • There are possible variants:

    # key function is a dict without argument
    q:
        key:
          Required:
    # provides all arguments to function, even the key:
    q:
        key:
            Required:
                - q

Tests

You can use tox to run the test-suite on every supported platform:

# Install and load virtualenv
$ pip install virtualenv
$ virtualenv .env
$ .env/bin/activate
# Install tox
$ pip install tox
# Run the test suites
$ tox

Issues

Pull-requests are welcome. You can also submit your issues to the issues tracker

License

yamlious is licensed under the Apache License, Version 2.0. See LICENSE file for full license text.