vmagamedov / dictator

Structured data validation library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dictator

Structured data validation library

Why another validation library?

To exploit simple yet powerful idea in order to provide predictable validation with safety in mind.

Example

Schema = Mapping.of(String.named('foo'),
                    Sequence.named('bar').of(Integer))

schema = Schema({'foo': u'Some text',
                 'bar': [u'1', u'2', u'3']})

assert schema.value == {'foo': u'Some text', 'bar': [1, 2, 3]}

Syntax is heavily inspired by flatland.

Idea

Think about validation schema as a data type, which can contain other data types. This looks similar to algebraic data types. Validation - is an instantiation of this data type using input data structure. Instance of this data type is a validated data structure with easily introspectable errors and decoded pythonic values.

How it works

Classes in Python has two interesting methods: __new__ and __init__.

__new__ method of the schema classes is responsible in decoding of input values. Schema instances creation is done from the top level element to the all nested elements, while decoding is possible. Then, when all possible instances were created in top-down fashion, validation process is started using bottom-up strategy in the __init__ method for all those elements, which were successfully decoded.

Input data structure is considered valid, obviously, when all validated nested elements are also valid.

API docs

Please look for docstrings in the source code.

About

Structured data validation library

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Python 100.0%