Cacheable schema description and validation
Project description
A low-level, declarative schema validation library.
Declare a schema:
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``s, 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``s, the error code is ``UNKNOWN for values that don’t match the allowed value or values. In Polymorph``s, 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file conformity-1.12.0.tar.gz
.
File metadata
- Download URL: conformity-1.12.0.tar.gz
- Upload date:
- Size: 11.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3c58ccd4522005f84985bc0ce667586df3a97bdd8b7197af49903b238901e4b |
|
MD5 | dfe6ce352c8ffb4e17acd241ae42f22a |
|
BLAKE2b-256 | adab79a4fd4ca17bc912f71569c7d48888e7579acd2a5a8168f480e16fa5c214 |
File details
Details for the file conformity-1.12.0-py2.py3-none-any.whl
.
File metadata
- Download URL: conformity-1.12.0-py2.py3-none-any.whl
- Upload date:
- Size: 19.4 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b065183ff0c86f51e6dc3f68f0d9f2059ea903c65b52f7604821325d4f3ad6bd |
|
MD5 | 3898784217c6106da2976467cd87d50a |
|
BLAKE2b-256 | 99177c1457e62ed814b4e876bc8256010c9901ee9bd9b597757fa60046759e29 |