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
1
vote
0 answers

Unresolved reference for some library classes of Cerberus in PyCharm

I started running into an import issue where PyCharm is unable to resolve references to classes for some libraries I have installed. I haven't run into this issue for any standard libraries. from cerberus import Validator used to work but stopped…
Kenton Parton
  • 70
  • 1
  • 5
1
vote
1 answer

Cerberus schema with single dict or list of dicts

I'm trying to build a schema where the statement can be a single dict or a list of dicts. Ex: {'Document': {'key': 'value'}} Or multiple keys: {'Document': [ {'key1': 'value1'}, {'key2': 'value2'}, {'key3': 'value3'}]} Following the documentation I…
1
vote
1 answer

Dependencies validation based on conditions using Cerberus

There are two fields "field1" and "field2". The condition is either "field1" or "field2" can take 'ANY' value, but both fields cannot have 'ANY' value. How to add either dependencies or oneof or excludes based on the above condition? from cerberus…
Misha
  • 49
  • 7
1
vote
0 answers

Use cerberus coercers to encrypt / decrypt data

I am planning to use python eve & cerberus to save some documents into a mongodb database. Some of this documents will have some sensible data (pseudo-passwords) which I do want to store encrypted. I have thought that cerberus custom coercers is the…
Mayday
  • 4,680
  • 5
  • 24
  • 58
1
vote
1 answer

How can I produce a warning with ceberus if a key is missing?

Cerberus allows for required fields but I'd like to have a "preferred" class of fields such that a warning message is logged if they're missing. Some ideas I have that don't seem great are the following: I could extend the validator with a custom…
Jessica
  • 330
  • 1
  • 2
  • 10
1
vote
2 answers

Python: cerberus check_with function

I would like to validate a dict, where the values adhere to the following rules: value must be either a single float or List(float) if it is a single float, the value must be 1 if it's a List(float), each float must be positive Here's my code and…
Andi
  • 3,196
  • 2
  • 24
  • 44
1
vote
0 answers

Permit one of the given schemas

I am using Cerberus to validate some YAML files that might look like this: fleet.yml fleet_city: "New York" vehicles: vehicle_1: car: # License plate required for car license_plate: "ABC123" cargo: 10 vehicle_2: # All…
Donentolon
  • 1,363
  • 1
  • 10
  • 17
1
vote
1 answer

How can I validate a list which contains floats in a specific range with Cerberus?

I would like to validate JSON objects that were parsed into Python dictionaries like the following: # example with 2 elements { 'coordinates': [-20.3, 30.6] } # example with 3 elements { 'coordinates': [-20.3, 30.6, 0] } So far I was able…
udo
  • 4,832
  • 4
  • 54
  • 82
1
vote
1 answer

RuntimeError: There's no handler for [insert_filed_name] in the 'validate' domain

I am using cerberus v1.3.2 with python-3.8 stable to validate json data that will be used to send http requests. I am having an issue using the dependencies rule. My objects have a field request_type and an optional field payload that contains more…
1
vote
1 answer

How to validate nested dictionary object in Cerberus

Here is a sample data that needs to be validated. The keys in the employee_eligibility nested dictionary are number string "[0-9]+". { "client_id": 1, "subdomain": "Acme", "shifts": [ 20047, 20048, 20049 ], "employee_eligibility": { …
Adam
  • 473
  • 5
  • 21
1
vote
1 answer

How do i do an type OR Type. String OR Int?

I would like to be able to allow a string or an integer in a field. How do I do this? This is my current schema: 'minSize': {'type': 'any'},
Jack
  • 2,891
  • 11
  • 48
  • 65
1
vote
2 answers

Python cerberus – choices of strings

How can I validate a schema for a list of string choices? Say I want the following animal strings to be valid: ['dog', 'cat', 'lion'] What would the schema look like for checking whether the key of animal contains any of these? I can't quite figure…
OhMad
  • 6,871
  • 20
  • 56
  • 85
1
vote
0 answers

How to add conditional schema for cerberus

{ "offers": { "type": "list", "schema": { "type": "dict", "oneof":[{ "schema": { "random1": { "type": "float", "empty": False }, "random2": { "type": "float", …
rocky
  • 11
  • 1
1
vote
2 answers

How to validate by cerberus the data the field of which can be a dict, or list of dict?

I need to validate dictionary received from a user the problem is that a field can be both dictionary and list of dictionaries. How i can validate that with cerberus? Like a example i try this schemas: v = Validator( { 'v': { …
Night Str
  • 127
  • 2
  • 10
1
vote
1 answer

Cerberus META validation rule access

Python cerberus has a validation rule called meta where I assign a dict to meta rule. How am i supposed to access it? I am writing a custom error_handler using this to customise the error messages. My target is schema = {'a': {'type': 'integer',…
yugantar
  • 1,970
  • 1
  • 11
  • 17
1 2 3
8 9