dictspec is a simple Python library that validates dictionary and list based data structures. You can use it to validate JSON/YAML documents against your own specification.
It is MIT licensed and uses relative imports, so you can just drop it into your project.
Examples
Validate against plain Python data objects or types:
>>> spec = {
... 'foo': 1,
... 'bar': [basestring],
... }
>>> data = {
... 'foo': 4,
... 'bar': ['hello', u'w\x00F6rld']}
... }
>>> from dictspec.validator import validate
>>> validate(spec, data)
Use more complex specs and get detailed errors:
>>> from dictspec.spec import number, required >>> spec = { ... required('foo'): number(), ... 'bar': bool(), ... } >>> data = { ... # missing 'foo' key ... 'bar': 4, # wrong type ... 'baz': True, # unknown key ... }>>> from dictspec.validator import ValidationError >>> try: ... validate(spec, data) ... except ValidationError, ex: ... print ex.errors ["missing 'foo' not in .", "unknown 'baz' in .", '4 in bar not of type bool']
Also with recursion and arbitrary keys:
>>> from dictspec.spec import anything, recursive
>>> spec = {
... 'hello': recursive({
... anything(): recursive(),
... })
... }
>>> data = {
... 'hello': {'any': {'thing': {'recursive':{}}}}
... }
>>> validate(spec, data)
Metadata
Release files for dictspec 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| dictspec-0.2.1.tar.gz | 6.3 kB | Details |
Release files / dictspec-0.2.1.tar.gz
| Download URL | dictspec-0.2.1.tar.gz |
|---|---|
| Size | 6.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
305568ce191759b410047cecdcec16534f0580e5f33a740eb8965ad56557d14d
|
|
BLAKE2b-256 checksum How to use checksums |
434bbfa11631ea2d26ccf0b244f4a1a0583e92c6586f5c8df3d051c880238dca
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |