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

Python - Cerberus, jsonschema, voluptous - Which one will be appropriate?

I am developing a small RESTful web application in python and using JSON as interface. The application accepts JSON data and needs to validate them against a schema. The schema may be very strict or flexible depending on the request. Also, the…
13
votes
1 answer

How to make cerberus required rule depends on condition

I have a big json document, where some fields should be required if other fields have exact values. E.g. document = {'is_realty_address': False, 'postcode': 111111} postcode have to be required if is_realty_address == False. All of the rules…
Dmitry Bozhenko
  • 251
  • 2
  • 9
8
votes
1 answer

What is the difference between Cerberus Custom Rules and Custom Validators?

From the documentation, it is not clear to me what the difference in use case for the Custom Rule and the Custom Validators are. In the examples given in the documentation, the only difference is an extra if statement that checks the value of is_odd…
darthbith
  • 18,484
  • 9
  • 60
  • 76
6
votes
1 answer

How can a Cerberus dependency reference a field higher up in the document?

I am trying to create a schema for documents that have dependencies that reference fields higher up in the document. For example: document = { 'packages': { 'some-package': {'version': 1} }, 'build-steps': { …
Dan Craig
  • 63
  • 5
5
votes
1 answer

How to coerce string to datetime in Python Cerberus?

I'm trying to coerce string as date so it can validate date data type, but it still returns False: from cerberus import Validator from datetime import datetime v = Validator() v.schema = {'start_date': {'type':…
Aryan087
  • 516
  • 6
  • 25
5
votes
1 answer

How to validate nested objects in cerberus?

I have the following schema: schema = { 'person': { 'name': {'type': 'string', 'required': True, 'minlength': 2}, 'gender': {'type': 'integer', 'required': True}, 'weight': {'type': 'integer', 'required': True}, …
user9377278
4
votes
2 answers

cerberus: forbid field existance

I want to use Cerberus to validate that a field is NOT present in the object. I would like to use something like: my_schema = { 'normal_field': { 'type': 'string', }, 'forbidden_field': { 'forbid': True, …
eLRuLL
  • 18,488
  • 9
  • 73
  • 99
4
votes
1 answer

Cerberus - Field required only when dependency is met

Consider the following schema schema = { "value_type":{ "type": "string", "required": True }, "units": { "type": "string", "dependencies": {"value_type": ["float", "integer"]}, "required": True …
helplessKirk
  • 326
  • 1
  • 6
  • 16
4
votes
2 answers

Detect empty string in numeric field using Cerberus

I am using the python library cerberus (http://docs.python-cerberus.org/en/stable/) and I want to check if a JSON field is a number (integer) or an empty string. I tried using the condition: {"empty": True, "type": "intenger"} But when the field is…
daloman
  • 309
  • 3
  • 10
4
votes
1 answer

Python Cerberus: multipe schemas for a single filed?

I am trying to use Cerberus to validate some data but I run into a problem. I defined several smaller schema such as: A = {"type": "dict", "required": False, "schema": {"name": {"type": "string"}}} B = {"type": "dict", "required": False, "schema":…
Fan Zhang
  • 139
  • 1
  • 2
  • 3
4
votes
1 answer

In Cerberus can you use 'valueschema' with a type of 'dict'?

So I'm using Cerberus for schema validation, but I'm running into a particular with validating a subdictionary of a dictionary whose key is unknown. So say I have the following document: dict = { 'things': { '0463': { 'foo':…
Sir Neuman
  • 1,385
  • 1
  • 14
  • 23
3
votes
1 answer

Cerberus: Use "required" fields with custom validator

I have validation rules in Cerberus that require a custom validator. When accessing fields in self.document, I have to also validate those fields are present, even if using the "required" flag. I am looking for a way for the "required" flag to…
zlipp
  • 790
  • 7
  • 16
3
votes
1 answer

Allow unknown keys but validated values in schema

What is the best way to validate a dict where the keys are unknown but the values have a definite schema. For example: data = { 'name': 'test', 'department': { 'unknown_key': { 'known_key': 'good', 'unknown_key': 'bad' …
3
votes
1 answer

Validation for custom type in Cerberus

I really enjoy Cerberus but I can't figure out a simple case in the docs. I want to have the type fudge which is a string with ~ prepended. I simply can't figure out how to do it.. fudge_type = cerberus.TypeDefinition('fudge_type', (str,),…
Pithikos
  • 18,827
  • 15
  • 113
  • 136
3
votes
1 answer

How can I customize error messages of Cerberus?

I want to localize the error messages Cerberus returns, e.g. I'd like to achieve the following: >>> validator.schema = {'animal': {'forbidden': ['Einhorn']}} >>> validator({'animal': 'Einhorn'}) False >>> validator.errors {'animal': ['VERBOTEN!']} …
funky-future
  • 3,716
  • 1
  • 30
  • 43
1
2 3
8 9