Questions tagged [cerberus]

Cerberus is a lightweight and extensible data validation library for Python

Cerberus provides powerful yet simple and lightweight data validation functionality out of the box and is designed to be easily extensible, allowing for custom validation. It has no dependencies and is thoroughly tested from Python 2.6 up to 3.5, PyPy and PyPy3.

At a Glance

You define a validation schema and pass it to an instance of the Validator class:

schema = {'name': {'type': 'string'}}
v = Validator(schema)

Then you simply invoke the validate() to validate a dictionary against the schema. If validation succeeds, True is returned:

document = {'name': 'john doe'}
v.validate(document)
True

Documentation

See the Cerberus website.

127 questions
2
votes
1 answer

strange validation behavior with `datetime` in sub object in python eve

I am seeing some very strange validation behavior while testing my python eve API. Eve 0.7.4 Mongod v3.2.10 Simplified summary: I have a domain endpoint test with a schema containing an object props which has two sub properties time and…
mlshapiro
  • 43
  • 6
2
votes
0 answers

Can I disable custom validation rules in Cerberus?

I have some custom validation rules I'm using for somewhat complex validation of in Cerberus (following http://docs.python-cerberus.org/en/stable/customize.html). They work fine, but occasionally when testing or working with files I know are…
Kyle Niemeyer
  • 301
  • 2
  • 12
2
votes
2 answers

cerberus. at least one of two keys should be present is json

I'm using Cerberus to validate the data posted as JSON to a Flask-based ReST-API. I want at least one of the two fields freight_id and tender_id to be present. These mappings would be considered as valid: {"freight_id": 1, "price" :…
Shahryar Saljoughi
  • 2,599
  • 22
  • 41
2
votes
2 answers

Using cerberus in Python to validate boolean values

I'm trying to use Cerberus in Python to validate some data. I found out that for 'boolean' type, the validator always return True, like this: import cerberus bool_schema = {'name': {'type': 'boolean', 'required':…
Hou Lu
  • 3,012
  • 2
  • 16
  • 23
2
votes
1 answer

Python-Eve. Declare an array of Object IDs on Cerberus schema

I am developing an API with Python-Eve and I need to create a MongoDB schema declaration using Cerberus to express a document like the one below: { name : 'John Smith', type: 'home', devices : [ ObjectID('1234'), …
gcw
  • 1,639
  • 1
  • 18
  • 37
1
vote
1 answer

Is it possible for cerberus to check nested recursive structure?

So there is a response, which has recursive list children : resp = [{'name': "child-1", 'cap': 3, 'children': [ { 'name': "child-11", 'cap': 3, 'children': [ { …
Lezakk
  • 21
  • 2
1
vote
1 answer

Specify list length range in Python Cerberus

I need to set the minimum and maximum length of a list between 2 - 5. Is there a way to specify this in Python Cerberus. Here's what I have currently but this allows lists of all sizes: { "levels": { "type": "list", "schema":…
1
vote
0 answers

Nested JSON Validation and Parsing

I cannot return the nested JSON file after I validate it using Cerberus in my api. Validation works fine but I don't know how to input the JSON file to the "add_argument" function of RequestParser. I have defined the nested Json structure in a…
1
vote
0 answers

Cerberus - how not to skip empty values from getting checked

I am using Cerberus with default and custom validation rules. My raw data can include empty values and this is causing them to be skipped from any validation, whereas I would like to be able to check them against other values and produce a custom…
Lev
  • 673
  • 1
  • 12
  • 29
1
vote
1 answer

Cerberus: Can the schema copy a value to multiple fields?

I want to take an input document like the below and copy the 'foo' key to multiple fields with different coercions and validations to get something like this: >>> input_doc = {'foo': 10} >>> coercions = {'foo': {'copy_to': 'bar', 'coerce': str},…
1
vote
0 answers

Python Cerberus: validating NAN and numbers using 'anyof_schema' rule

I'm trying to create a Cerberus validation schema to validate a set of data in a Pandas dataframe. One of the columns should only validate on the following data: numbers NaN (this is Numpy's floating-point representation of Not a Number, and how…
sam_ur_ai
  • 107
  • 10
1
vote
1 answer

In Cerberus (Python) is there a way to create a schema that allows any key name in a dictionary?

Given a dictionary where the top level keys can be any value, but there is a strict schema within the values of those keys: {"rand_value": ["key1": "val1", "key2": "val2"], "another_rand_value": ["key1": "val1", "key2": "val2"]} Can I create a…
Prime
  • 197
  • 2
  • 13
1
vote
0 answers

How to convert json schema in one form to other surported by cerberus?

How can we convert this schema to the below one: { "entityName": "Firm", "attributes": [ { "name": "FirmKey", "rules": [ { "type": "primaryKey", …
1
vote
1 answer

Validate list of dicts using cerberus

Started to use cerberus for contract testing purposes. It works perfectly in cases, when we got dict-based JSON structure, e.g: {'results': [{"key": "value"}, {"key": "value"}, {"key":"value}]} But everything goes bad when response is just a list of…
Shoo
  • 43
  • 8
1
vote
0 answers

How do I setup cerberus to validate against a model based on a SQL database schema?

How do I setup cerberus to validate against a model based on a SQL database schema? Specifically is there a way to generate the schema definition of what we are loading data into (e.g. SQL Server database table) without having to manually define it…
Bim Dave
  • 11
  • 1
1 2
3
8 9