Skip to main content

Validate Python dictionaries like JSON schema

Project description

Latest PyPI version Documentation Status License Build status Coverage

logo

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)
    ...
ValueError: 'age' too small: expected >= 0, but was -12
>>>

>>> testdict = {'name': 'Neil', 'age': '55'}
>>> schema.validate(testdict)
    ...
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)
    ...
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

  • Adding custom validator functions

Features currently in progress

  • Metaschema validation

  • Validation of subschemas in list or tuples

  • Regex support for strings

  • Lazy validation and summary of all errors

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


Download files

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

Source Distribution

schemadict-0.0.3.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

schemadict-0.0.3-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file schemadict-0.0.3.tar.gz.

File metadata

  • Download URL: schemadict-0.0.3.tar.gz
  • Upload date:
  • Size: 11.6 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

Hashes for schemadict-0.0.3.tar.gz
Algorithm Hash digest
SHA256 901e9464c6ca74c7ac2399bcd0194f43abacdbb008fffbdca060b3cd8ea95f66
MD5 1755886bb3f6509e01ba7bd198264089
BLAKE2b-256 68baf5b022c5e087ed43014b9a2f58f2da7fc578a2f3587b14d85d6ea41f57ca

See more details on using hashes here.

Provenance

File details

Details for the file schemadict-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: schemadict-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 10.8 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

Hashes for schemadict-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ebf54874a038ecde3ac7b7c327cf4c43ea4b4db8870bf0c5727fac58809eb56f
MD5 13d599b8feee274a7329ade720e33186
BLAKE2b-256 3e42efac74e61507126aa5bef56d74f0ce09f36c2b43cab1ef981c2e8ac5fc9d

See more details on using hashes here.

Provenance

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