Skip to main content

Cerburus based validation extended to support list schemas and list transposition to dictionary and python objects

Project description

PyPI version Build Status Code style: black

Cerberus List Schema is a Cerberus based validation library with extended methods to support list schemas as well as list transposition to dictionary and python objects.

  • List/array schema support
  • Transposition of lists to python objects via schemas
  • Support for missing values in document

Installation

Cerberus List Schema can be installed using pip.

$ pip install cerberus-list-schema

Extensions

Validation

>>> schema = {
>>>     "type": "list",
>>>     "items": [{"type": "string"}, {"type": "integer", "min": 20}],
>>> }
Simple validation

Lists can now be validated out of context of a dictionary

>>> document = ["Apples", 40]
>>> v = Validator(schema)
>>> v.validate(document)
True

... and usual cerberus validation rules still apply

>>> document = ["Apples", 15]
>>> v.validate(document)
False
>>> v.errors
{'_schema': [{1: ['min value is 20']}]}
Allow incomplete documents

In cerberus, documents that are missing information specified in a list schema will fail. Using Cerberus List Schema you can pass allow_list_missing=True to a Validator object to enable incomplete lists.

>>> document = ["Apples"]
>>> v.validate(document)
False
>>> v.errors
{'_schema': ['length of list should be 2, it is 1']}

>>> v = Validator(schema, allow_list_missing=True)
>>> v.validate(document)
True

Normalization

Normalization as dict

Lists can now be normalized as dict additional to the standard cerberus validation. By default they are given a key equalled to their list index.

>>> document = {"produce": ["Apple", 5, "High"]}
>>> schema = {
>>>     "produce": {
>>>         "type": "list",
>>>         "items": [
>>>             {"type": "string"},
>>>             {"type": "integer", "min": 0},
>>>             {"type": "string"},
>>>         ],
>>>     }
>>> }

>>> v = Validator(schema)
>>> v.normalized_as_dict(document)
{"fruits": {0: "Apple", 1: 5, 2: "High"}}
Naming indexes

However by using the name rule, lists can be assigned to a namable dict. Note that this is different to rename and should be preferred when using the dictionary normalization as rename can produce adverse effects.

>>> document = {"produce": ["Apple", 5, "High"]}
>>> schema = {
>>>     "produce": {
>>>         "type": "list",
>>>         "name": "fruits",
>>>         "items": [
>>>             {"name": "type", "type": "string"},
>>>             {"name": "count", "type": "integer", "min": 0},
>>>             {"name": "quality", "type": "string"},
>>>         ],
>>>     }
>>> }

>>> v = Validator(schema)
>>> v.normalized_as_dict(document)
{'fruits': {'type': 'Apple', 'count': 5, 'quality': 'High'}}
Allowing name conflicts

By default, conflicting names will throw an error. However, allow_name_conflicts can be specified to ignore the error. In this case, previous assignments will be overwritten without error

>>> document = {"produce": ["Apple", "Orange"]}
>>> schema = {
>>>     "produce": {
>>>         "type": "list",
>>>         "items": [
>>>             {"name": "fruit_type", "type": "string"},
>>>             {"name": "fruit_type", "type": "string"},
>>>         ],
>>>     }
>>> }

>>> v = Validator(schema)
>>> v.normalized_as_dict(document)
AttributeError: `name` rule (`fruit_type`) already in use by another field

>>> v.normalized_as_dict(document, allow_name_conflicts=True)
{'produce': {'type': 'Orange'}}

Object Mapping

Lists can now be normalized as dict additional to the standard cerberus validation. By default they are given a key equalled to their list index. However name may be used to rename object property to that provided. (ensuring the name is a valid python variable name)

>>> document = {"produce": ["Apple", 5, "High"], "supplier": ["Greg", "United Kingdom", 7.34]}
>>> schema = {
>>>     "produce": {
>>>         "type": "list",
>>>         "name": "fruits",
>>>         "items": [
>>>             {"name": "type", "type": "string"},
>>>             {"name": "count", "type": "integer", "min": 0},
>>>             {"name": "quality", "type": "string"},
>>>         ],
>>>     },
>>>     "supplier": {
>>>         "type": "list",
>>>         "items": [
>>>             {"type": "string"},
>>>             {"type": "string"},
>>>             {"type": "string", "coerce": int},
>>>         ],
>>>     },
>>> }

>>> v = Validator(schema)
>>> obj = v.normalized_as_object(document)

>>> obj.fruits.type  # note produce has been renamed to fruits
'Apple'
>>> obj.fruits.quality
'High'
>>> obj.supplier[0]
'Greg'
>>> obj.supplier[2]  # w/ coerce as int rule applied
7
Allowing callable properties for unassigned names

Array values without a name property can also be callable by using callable_numbers. This is places an underscore before the key index such that it can be called as a property of an object rather than by index.

>>> document = ["Greg", "United Kingdom", 7.34]
>>> schema = {
>>>    "type": "list",
>>>    "items": [
>>>        {"type": "string"},
>>>        {"type": "string", "name": "country"},
>>>        {"type": "string", "coerce": int},
>>>    ],
>>>}

>>> v = Validator(schema)
>>> obj = v.normalized_as_object(document, callable_numbers=True)

>>> obj._0
'Greg'
>>> obj._1  # value renamed to country

>>> obj.country
'United Kingdom'
>>> obj._2
7

Cerberus

More information about Cerberus and its validators can be found on their GitHub page @ https://github.com/pyeve/cerberus

Complete documentation for Cerberus is available at http://docs.python-cerberus.org

Project details


Download files

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

Source Distribution

cerberus-list-schema-1.0.2.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

cerberus_list_schema-1.0.2-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file cerberus-list-schema-1.0.2.tar.gz.

File metadata

  • Download URL: cerberus-list-schema-1.0.2.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

File hashes

Hashes for cerberus-list-schema-1.0.2.tar.gz
Algorithm Hash digest
SHA256 7497b5c2ca01e2c7ebdbad3436c625d2c2dcb38bc614946c341a0ff141d7a971
MD5 35d93e0487c82838cd96149666423e06
BLAKE2b-256 ff17c49fc73a87033db666405f205587324cd843e0152d2fe4dd817e550afa7f

See more details on using hashes here.

File details

Details for the file cerberus_list_schema-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: cerberus_list_schema-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

File hashes

Hashes for cerberus_list_schema-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e9e21a78d4ecd0245ff8503900a0d8528bc29fcd7e9e40f01848f9dc083a22a7
MD5 760244da47a5a55e08801e7674b5e07e
BLAKE2b-256 306792323e188082858897fc1e16d483d4019cb8a8548aa60b6e4195850f27e4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page