Cerberus alternative
Project description
Sureberus
This is an implementation of the Cerberus schema format. It doesn't implement all of the features of that library, and where it does implement a feature it doesn't always implement it in the exact same way.
The main reason it exists is to support some of the things that Cerberus doesn't do.
Schema selection based on dict keys
Often times when anyof
or oneof
are used, what we really want to do is
select a schema based on dict keys.
There are two options for this:
when_key_is
Use this when you have dictionaries that have a fixed key, such as "type"
,
which specifies some specific format to use. For example, if you have data that
can look like this:
{"type": "elephant", "trunk_length": 60}
{"type": "eagle", "wingspan": 50}
Then you would use when_key_is
, like this:
{
"type": "dict",
"when_key_is": {
"key": "type",
"choices": {
"elephant": {
"schema": {"trunk_length": {"type": "integer"}}
},
"eagle": {
"schema": {"wingspan": {"type": "integer"}}
},
}
}
}
when_key_exists
Use this when you have dictionaries where you must choose the schema based on keys that exist in the data exclusively for their type of data. For example, if you have data that can look like this:
{"image_url": "foo.jpg", "width": 30}
{"color": "red"}
Then you would use when_key_exists
, like this:
{
"type": "dict",
"when_key_exists": {
"image_url": {
"schema": {"image_url": {"type": "string"}, "width": {"type": "integer"}}
},
"color": {
"schema": {"color": {"type": "string"}}
},
}
}
normalization inside of *of-rules
The primary important difference is that you can use sureberus if you want to
use default
or coerce
inside of a
*of-rule.
Nullable in the face of *of-rules
Sureberus allows you to use nullable
even if you have *of-rules
that have
type
constraints. A nullable schema always allows None
.
A slightly nicer schema syntax
If you want to construct a schema from Python code instead of storing it as JSON, sureberus provides a more terse syntax for it:
Here's a standard dict-based schema, using an 80-character limit and strict newline/indent-based line wrapping:
myschema = {
'type': 'dict',
'anyof': [
{'schema': {'gradient': {'type': 'string'}}},
{
'schema': {
'image': {'type': 'string'},
'opacity': {'type': 'integer', 'default': 100},
}
},
],
}
And here is a sureberus.schema
-based schema, using the same line-wrapping
rules:
from sureberus.schema import Dict, SubSchema, String, Integer
myschema = Dict(
anyof=[
SubSchema(gradient=String()),
SubSchema(image=String(), opacity=Integer(default=100))
]
)
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 sureberus-0.4.tar.gz
.
File metadata
- Download URL: sureberus-0.4.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/2.7.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5523d40d64319377ccdb43978302b205381a748d69af07b5ab90d1eb9a59e9eb |
|
MD5 | bc109e14258a06fca79adc3e48170be0 |
|
BLAKE2b-256 | df0c0364d5797ca5c4fec2080022d8d448413685c96e6328425b67c100385238 |
Provenance
File details
Details for the file sureberus-0.4-py2.py3-none-any.whl
.
File metadata
- Download URL: sureberus-0.4-py2.py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/2.7.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c76ae70cf8915346b5ceb1a3ce9eea0ec85a8047ec5bd7266d34ac18c0db1ca1 |
|
MD5 | 3dbd4f4d47e02ca03bc4a1aa467055e6 |
|
BLAKE2b-256 | b120d020107f33efb6af12bd33cc19facf0ee55207ff75335c9aaf708acde3de |