Skip to main content

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.

normalization inside of *of-rules

The primary important difference is that you can use sureberus if you want to use transforming directives, such as default or coerce, while also validating the document. Cerberus only allows you to do one or the other. Most often, this limitation becomes a problem when you want to use an *of-rule.

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, which should be used in preference to anyof or oneof, when possible, as they provide much better error messages.

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"}}
            },
        }
    }
}

You can also specify a default_choice inside of the when_key_is directive, to specify which choice to use if the (e.g.) type key is elided from the value being validated.

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"}}
        },
    }
}

In-line schema registries

Small, reusable "chunks" of schema can be defined in-line in the schema specification, instead of requiring Python code to be written which sets up registries. This allows for easy use of recursive schemas at any point in your schema, or just a way to conveniently reuse some subschema in multiple places. For example, here is a schema that validates any nested list of strings:

{
    "registry": {
        "nested_list": {
            "type": "list",
            "schema": {
                "anyof": [
                    {"type": "string"},
                    "nested_list",
                ],
            }
        }
    },
    "type": "dict",
    "schema": {"things": "nested_list"},
}

This will validate data like {"things": ["one", ["two", ["three"]]]}.

Typically any place you can specify a schema, you can instead specify a string which will be used to find a previously registered schema (references to registered schemas are resolved lexically).

When you need to "merge in" a registered schema, you can use the schema_ref directive. This can be useful if you want to register a schema and use it at exactly the same level, for example:

{
    "registry": {
        "nested_list": {
            "type": "list",
            "schema": {"anyof": [{"type": "integer"}, "nested_list"]}
        }
    },
    "schema_ref": "nested_list",
}

This will validate data like ["one", ["two", ["three"]]].

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))
    ]
)

Download files

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

Source Distribution

sureberus-0.7.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sureberus-0.7.0-py2.py3-none-any.whl (8.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file sureberus-0.7.0.tar.gz.

File metadata

  • Download URL: sureberus-0.7.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for sureberus-0.7.0.tar.gz
Algorithm Hash digest
SHA256 c924bb98cf91945e2b0e0da767c158adc52cac9265d5cd2c2eb4007f993e6ad6
MD5 339d3cdab237d3c5c7811cf57e7aaf77
BLAKE2b-256 9f0c94116429ed9d6e485cce57513b39b6581692236a00054959e705d1bf9c81

See more details on using hashes here.

File details

Details for the file sureberus-0.7.0-py2.py3-none-any.whl.

File metadata

  • Download URL: sureberus-0.7.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for sureberus-0.7.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 75b2a850275b00286ea146116330bd72b0361f6b28061e0160e80557d96b2ee8
MD5 4d2e2ee7d14ea2a0fa1df43c6073a5a4
BLAKE2b-256 a1795ceada7ff26f73dc91cb6584c3153e1578da752be3b1651b90474019d850

See more details on using hashes here.

Release history Release notifications | RSS feed

0.15.0

2 files

0.14.0

2 files

0.13.0

2 files

0.12.0

2 files

0.11.2

2 files

0.11.1

2 files

0.11.0

2 files

0.10.3

2 files

0.10.2

2 files

0.10.1

2 files

0.10.0

2 files

0.9.1

2 files

0.9.0

2 files

0.8.0

2 files

This release

0.7.0 This release

2 files

0.6.1

2 files

0.6

2 files

0.5

2 files

0.4

2 files

0.3

2 files

0.2

2 files

0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page