Skip to main content

Set of tools that makes input data validation easier

Project description

Schematec

https://travis-ci.org/mylokin/redisext.svg?branch=master

Schematec is a set of tools that makes input data validation easier. The purpose of this code is attempt to bring simplicity to applications logics using separation of data validation and actual data processing.

Quickstart

import schematec as s

schema = s.dictionary(
   id=[s.integer, s.required],
   name=s.string,
   tags=s.array(s.string),
)
>>> data = {
...     'id': '1',
...     'name': 'Red Hot Chili Peppers',
...     'tags': ['funk', 'rock'],
...     'rank': '1',
... }
>>> schema(data)
{'id': 1, 'name': u'Red Hot Chili Peppers', 'tags': [u'funk', u'rock']}

Concepts

Schematec module is based on three basic concepts:

  • Schema

  • Validator

  • Converter

Schema

Term “schema” is used to describe complex data struct such as dictionary(hashmap) or array(list). Schemas has two different types of validation (it is not related to array schemas):

  • Strict - requires all values

  • Non-strict - tolerate to missed values

schematec.exc.SchemaError is raised in case provided data is incorrect.

Order of schema validations:

  1. Unbound Validators

  2. Schemas(inner)

  3. Converters

  4. Bound Validators

Validator

Term “validator” describes callable objects that perform different types of checks. There are two types of validators in schematec:

  • Bound - type related, for example “max length” validator is bound to sized type.

  • Unbound - universal, for example “required” validator.

Raises schematec.exc.ValidationError.

Converter

Term “converter” is used to describe cast functions. Schematec supports subset of JSON data types.

Basic types:

  • integer(int)

  • string(str)

  • boolean(bool)

Containers:

  • array(list)

  • dictionary(dict)

Raises schematec.exc.ConvertationError.

Examples

Recursive schema

import schematec as s

schema = s.dictionary(
    id=[s.integer, s.required],
    entity=s.dictionary(
        name=[s.string, s.required],
        value=s.string,
    )
)
>>> data = {
...     'id': 1,
...     'entity': {
...         'name': 'song',
...         'value': 'californication',
...     }
... }
>>> schema(data)
{'id': 1, 'entity': {'name': u'song', 'value': u'californication'}}

Errors handling

import schematec as s

schema = s.dictionary(
    id=[s.integer, s.required],
    entity=s.dictionary(
        name=[s.string, s.required],
        value=s.string,
    )
)
>>> data = {
...     'id': 1,
...     'entity': {
...         'value': 'californication',
...     }
... }
>>> schema(data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "schematec/schema.py", line 44, in __call__
    value = schema(value, strict=strict)
  File "schematec/schema.py", line 32, in __call__
    validator(name, data)
  File "schematec/validators.py", line 12, in __call__
    raise exc.ValidationError(name)
schematec.exc.ValidationError: name

Sugar descriptors

import schematec as s

schema = s.dictionary(
    id=s.integer & s.required,
    entity=s.dictionary(
        name=s.string & s.required,
        value=s.string,
    )
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

schematec-0.3.4.tar.gz (4.1 kB view hashes)

Uploaded Source

Built Distribution

schematec-0.3.4-py2.py3-none-any.whl (5.3 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page