Validate Python dictionaries like JSON schema
Project description
A schemadict is a regular Python dictionary which specifies the type and format of values for some given key. To check if a test dictionary is conform with the expected schema, schemadict provides the validate() method. If the test dictionary is ill-defined, an error will be thrown, otherwise None is returned.
Examples
Basic usage
>>> from schemadict import schemadict
>>> schema = schemadict({
... 'name': {
... 'type': str,
... 'min_len': 3,
... 'max_len': 12,
... },
... 'age': {
... 'type': int,
... '>=': 0,
... '<': 150,
... },
... })
>>>
>>> testdict = {'name': 'Neil', 'age': 55}
>>> schema.validate(testdict)
>>>
>>> testdict = {'name': 'Neil', 'age': -12}
>>> schema.validate(testdict)
Traceback (most recent call last):
...
ValueError: 'age' too small: expected >= 0, but was -12
>>>
>>> testdict = {'name': 'Neil', 'age': '55'}
>>> schema.validate(testdict)
Traceback (most recent call last):
...
TypeError: unexpected type for 'age': expected <class 'int'>, but was <class 'str'>
>>>
Nested schemadict
>>> schema_city = schemadict({
... 'name': {
... 'type': str
... },
... 'population': {
... 'type': int,
... '>=': 0,
... },
... })
>>>
>>> schema_country = schemadict({
... 'name': {'type': str},
... 'cities': {
... 'type': list,
... 'item_type': dict,
... 'item_schema': schema_city,
... },
... })
>>>
>>> test_country = {
... 'name': 'Neverland',
... 'cities': [
... {'name': 'Faketown', 'population': 3},
... {'name': 'Evergreen', 'population': True},
... ],
... }
>>>
>>> schema_country.validate(test_country)
Traceback (most recent call last):
...
TypeError: unexpected type for 'population': expected <class 'int'>, but was <class 'bool'>
>>>
Custom validation functions
TODO
Full documentation: https://schemadict.readthedocs.io/
Features
Schemadicts supports
Built-in support for Python’s primitive types
Specification of required and optional keys
Validation of nested schemas
Features currently in development
Adding custom validator functions
Metaschema validation
Validation of subschemas in list or tuples
Regex support for strings
Lazy validation and summary of all errors
Allow schema variations: schmea 1 OR schema 2
Add support for validation of type number.Number
Installation
Schemadict is available on PyPI and may simply be installed with
pip install schemadict
Idea
Schemadict is loosely inspired by JSON schema and jsonschema, a JSON schema validator for Python.
License
License: Apache-2.0
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 schemadict-0.0.4.tar.gz
.
File metadata
- Download URL: schemadict-0.0.4.tar.gz
- Upload date:
- Size: 9.3 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.2 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8dd97d14666b34accbfa5b072908b942ca4b34af3685a1815b7510550b310229 |
|
MD5 | 7015fb8ab8f8146757a02a5905210731 |
|
BLAKE2b-256 | 27d739c8866dece078ca0115200ddb2c28dbca4bf4bda41d1ce00b557f2da507 |
Provenance
File details
Details for the file schemadict-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: schemadict-0.0.4-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- 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.2 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b39171bb4f431699992c8c5b97294cbb4f6dbc525520de5e9162637e321197ba |
|
MD5 | 1467dbdd4bb09a925deb5f83ac37e055 |
|
BLAKE2b-256 | f379dd42eddca23e06d742dfa8956d459b00cfd62f1e4c66fc581e85d683fe8f |