Voluptuous, *despite* the name, is a Python data validation library.
Project description
Voluptuous, despite the name, is a Python data validation library.
Voluptuous has two goals:
Support complex data structures.
Provide useful error messages.
Schemas are defined as simple Python nested data structures consisting of dictionaries, lists and scalars. Each node in the input schema is pattern matched against corresponding nodes in the input data.
Here’s an example schema:
>>> import voluptuous as V >>> settings = { ... 'snmp_community': V.coerce(str), ... 'retries': V.coerce(int), ... 'snmp_version': V.all(V.coerce(str), V.any('3', '2c', '1')), ... } >>> features = ['Ping', 'Uptime', 'Http'] >>> schema = V.Schema({ ... 'exclude': features, ... 'include': features, ... 'set': settings, ... 'targets': { ... 'exclude': features, ... 'include': features, ... 'features': { ... str: settings, ... }, ... }, ... })
And the data to be validated (with invalid data at set/retries):
>>> data = { ... 'set': { ... 'snmp_community': 'public', ... 'snmp_version': '2c', ... 'retries': 'one', ... }, ... 'targets': { ... 'exclude': ['Ping'], ... 'features': { ... 'Uptime': {'retries': 3}, ... 'Users': {'snmp_community': 'monkey'}, ... }, ... }, ... }
And finally, validation:
>>> schema(data) # doctest: +NORMALIZE_WHITESPACE Traceback (most recent call last): ... Invalid: expected int for dictionary value @ data['set']['retries']
Correct the invalid data and revalidate:
>>> data['set']['retries'] = 1 >>> schema(data) # doctest: +NORMALIZE_WHITESPACE {'set': {'snmp_version': '2c', 'snmp_community': 'public', 'retries': 1}, 'targets': {'exclude': ['Ping'], 'features': {'Uptime': {'retries': 3}, 'Users': {'snmp_community': 'monkey'}}}}
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
voluptuous-0.2.tar.gz
(6.0 kB
view details)
File details
Details for the file voluptuous-0.2.tar.gz
.
File metadata
- Download URL: voluptuous-0.2.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d77fe845df6750b6bae5fd012d4b4fe05d4a0785e838ce96c1caf90b7b6e5dc6 |
|
MD5 | 2c5763ecb9515b5e7c73d130b9d53f1d |
|
BLAKE2b-256 | f9295d863699daa890a9a83c02e0c1bb853041d086eb50b8a7b3ae7e7c009a1c |