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
3
votes
1 answer

Have required set to True by default in cerberus

Is there a way to tell cerberus to have required set to True by default for all keys in the schema? This would save me some time, because most often I want to assert the existence of a key.
lue3Seba
  • 65
  • 5
2
votes
1 answer

Validating JSON Schema with Cerberus throws error when using correct data type

I am trying to validate a JSON schema. When specifying the correct data type date for released Cerberus throws an error. def test_validate_books_schema(): schema = { "url" : {'type': 'string'}, "name" : {'type':…
Armz
  • 85
  • 1
  • 7
2
votes
0 answers

Use Cerberus Schemas to Automate Documentation

We are using cerberus extensively to validate json configuration files. We therefore have a wide range of schemas, which define how these json documents should be formatted. We would like to be able to use these schemas to auto-generate some…
OliverBurke
  • 51
  • 1
  • 5
2
votes
1 answer

Python Cerberus - One is required if another one does not exist

What I want to achieve: >>> from cerberus import Validator >>> schema = {"x": {"type": "integer", "required": False}, "y": {"type": "integer", "required": False}} >>> v = Validator(schema) >>> v.validate({"x": 5}) True >>> v.validate({"y":…
AnnieFromTaiwan
  • 3,845
  • 3
  • 22
  • 38
2
votes
3 answers

N elements list of string validation in cerberus

How can I validate that certain type is a list and it contains only e.g string elements, of unknown number? My current solution is 'categories_id' : {'required' : False, 'type' : ['string','list']}, but it doesn't do the trick, is also returns True…
konradk
  • 161
  • 1
  • 9
2
votes
1 answer

cerberus - how to validate arbitrary dict keys?

I have read issues here and here using keysrules and valuesrules but I've only seen them validate nested not root. I'd like to valid the top level root dict keys. schema = { 'any_arbitrary_str': { 'type': 'dict', 'keysrules':…
nobody
  • 270
  • 2
  • 14
2
votes
1 answer

Python: validation using cerberus

I would like to validate a dict, where a field may contain either an int or a List[int]. Also, all int must be positive. I need some help with setting up the schema. The schema below are not working properly. They are not checking for negative…
Andi
  • 3,196
  • 2
  • 24
  • 44
2
votes
1 answer

cerberus.schema.SchemaError: {'uuid': [{'query_objectid_as_string': ['unknown rule']}]}

Eve API. I need to filter records by where={"uuid": "my_uuid"}. So my 'settings.py' for the Eve application contains: 'schema': { 'uuid': { 'type': 'string', 'required': True, …
Alex
  • 98
  • 7
2
votes
0 answers

Cerberus is very slow for valdiation, what have I done wrong?

(Disclaimer: Every performance comparison on stackoverflow I read gets slammed for not being comprehensive/correct/well written/relevant etc. etc. - I'm not pretending this is a real comparison or perfectly setup, I would just like to know if I can…
SColvin
  • 11,584
  • 6
  • 57
  • 71
2
votes
1 answer

How do I return a custom rule-name/error-code using Cerberus?

Am validating .csv file and I want to give the results of the validation in a format the user is accustomed to. To make use of Cerberus, I've let the user to define the validation rules in a .yaml file. schema.yaml Rules: Rule1: maxlength: 10 …
lukik
  • 3,919
  • 6
  • 46
  • 89
2
votes
2 answers

Dependencies validation using Cerberus

Am validating a CSV file with Cerberus but am struggling with what I'd assume is some basic logic Scenario: A CSV file has 2 columns. Column 2 requires to have a value only if Column 1 has a value. If Column 1 is empty then Column 2 should also be…
lukik
  • 3,919
  • 6
  • 46
  • 89
2
votes
2 answers

deeply nested json validator using cerberus

i have a deeply nested json that i'm trying to validate with cerberus. So, I have the following structure: (NOTE: assume even a deeper nest) i created my schema for the first level but I cant figure out how to go deeper in the json. I can't manually…
hashguard
  • 403
  • 4
  • 22
2
votes
2 answers

How to check referential integrity in Cerberus?

Consider the following Cerberus schema: { 'employee': { 'type': 'list', 'schema': { 'type': 'dict', 'schema': { 'id': {'required': True, 'type': 'integer'}, 'name': {'required': True, 'type': 'string'} } …
Bruno Rijsman
  • 3,715
  • 4
  • 31
  • 61
2
votes
1 answer

eve 0.8 `allow_unknown` in nested dict field

We developed a API using Eve 0.7 that used allow_unknown on a nested dict field. This field worked as expected according to the cerberus documentation. We are now upgrading to Eve 0.8 and our endpoints no longer respect the allow_unknown parameter…
mlshapiro
  • 43
  • 6
2
votes
3 answers

How can I remove the fields from a document that failed validation with Cerberus?

I am using the open source Python data validation library Cerberus to validate the structure of a dictionary. I want it to take a partly invalid document and output it without the invalid keys. For example, for this script: from cerberus import…
OhadBasan
  • 797
  • 6
  • 15
1
2
3
8 9