validator for JSON/YAML/dict data
Project description
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)
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
dictspec-0.2.1.tar.gz
(6.3 kB
view details)
File details
Details for the file dictspec-0.2.1.tar.gz
.
File metadata
- Download URL: dictspec-0.2.1.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 305568ce191759b410047cecdcec16534f0580e5f33a740eb8965ad56557d14d |
|
MD5 | dd9f1e4b3e0d2cb9bcb5e3530e823d8e |
|
BLAKE2b-256 | 434bbfa11631ea2d26ccf0b244f4a1a0583e92c6586f5c8df3d051c880238dca |