Skip to main content

Cacheable schema description and validation

Project description

https://api.travis-ci.org/eventbrite/conformity.svg https://img.shields.io/pypi/v/conformity.svg https://img.shields.io/pypi/l/conformity.svg

A low-level, declarative schema validation library.

Declare a schema:

from conformity.fields import Dictionary, Float, Integer, List, UnicodeString

person = Dictionary({
    "name": UnicodeString(),
    "height": Float(gte=0),
    "event_ids": List(Integer(gt=0)),
})

Check to see if data is valid:

data = {"name": "Andrew", "height": 180.3, "event_ids": [1, "3"]}
errors = person.errors(data)

# Key event_ids: Index 1: Not an integer

And wrap functions to validate on the way in and out:

kwargs = Dictionary({
    "name": UnicodeString(),
    "score": Integer(),
}, optional_keys=["score"])

@validate_call(kwargs, UnicodeString())
def greet(name, score=0):
    if score > 10:
        return "So nice to meet you, {}!".format(name)
    else:
        return "Hello, {}.".format(name)

There’s support for basic string, numeric, geographic, temporal, networking, and other field types, with everything easily extensible (optionally via subclassing).

Errors are always instances of conformity.error:Error, and each error has a message, a code, and a pointer:

  • message is a plain-language (English) explanation of the problem.

  • code is a machine-readable code that, in most cases, is INVALID (using the constant conformity.error:ERROR_CODE_INVALID). In Dictionary, the error code is MISSING (ERROR_CODE_MISSING) for required keys that aren’t present and UNKNOWN for extra keys that aren’t allowed. In Constant, the error code is UNKNOWN for values that don’t match the allowed value or values. In Polymorph, the error code is UNKNOWN for unknown switch values when no __default__ is present.

  • pointer is None for errors in most field types. However, for data structure field types (List, Dictionary, SchemalessDictionary, Tuple), pointer is a string representing the dotted path to the offending value in the structure.

Interface

Anything can be a Conformity validator as long as it follows this interface:

  • An errors(value) method that returns a list of conformity.error:Error objects for each error or an empty list or None if the value is clean.

  • An introspect() method, that returns a dictionary describing the field. The format of this dictionary has to vary by field, but it should reflect the names of keyword arguments passed into the constructor, and provide enough information to entirely re-create the field as-is. Any sub-fields declared for structures should be represented using their own introspect() output. The dictionary must also contain a type key that contains the name of the type, but this should use lower case and underscores rather than the class name. It can also contain a description key which should be interpreted as the human-readable reason for the field.

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

conformity-1.24.2.tar.gz (23.0 kB view details)

Uploaded Source

Built Distribution

conformity-1.24.2-py27.py35.py36.py37-none-any.whl (30.0 kB view details)

Uploaded Python 2.7 Python 3.5 Python 3.6 Python 3.7

File details

Details for the file conformity-1.24.2.tar.gz.

File metadata

  • Download URL: conformity-1.24.2.tar.gz
  • Upload date:
  • Size: 23.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

File hashes

Hashes for conformity-1.24.2.tar.gz
Algorithm Hash digest
SHA256 b770a0b53c6171173dca0c7ed90de6ff882731a5e484c7c41f505c86232b0f3d
MD5 68b05df2698114ce49a42a40b5f70b9c
BLAKE2b-256 bdadf860dcc538e72cee8e9a7fb0feeb1be7a2e34b22a2858bb5e7fad8aa1575

See more details on using hashes here.

File details

Details for the file conformity-1.24.2-py27.py35.py36.py37-none-any.whl.

File metadata

  • Download URL: conformity-1.24.2-py27.py35.py36.py37-none-any.whl
  • Upload date:
  • Size: 30.0 kB
  • Tags: Python 2.7, Python 3.5, Python 3.6, Python 3.7
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

File hashes

Hashes for conformity-1.24.2-py27.py35.py36.py37-none-any.whl
Algorithm Hash digest
SHA256 1a1f99b5cc3e1aa6e279a0c7649f966b2a966b1980c38e373e5532e5fbf8323d
MD5 ca46d90b2c5c8fad088033767698a308
BLAKE2b-256 4f8a59cd5e69c480f3c04d95586f3e0a7d44170fc3818f4acece7374ce6d78cb

See more details on using hashes here.

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