Data validation through Cerberus

Data validation is a crucial task in software development because, without proper validation mechanisms, data can become corrupted, leading to erroneous results, security vulnerabilities, and potential system failures. Various libraries exist for data validation, and Cerberus is one of the powerful Python libraries that gives users data validation functionality out of the box. It’s easily extensible and allows for custom validations, all while being compatible with multiple Python versions.

Validating data through Cerberus

The key to validating data using this library is the Validator class. This is what we use to validate our defined schemas. We use the validator() function within this class for validation. Let’s look at a simple example:

from cerberus import Validator
# Define your schema
schema = {
'name': {'type': 'string', 'required': True},
'age': {'type': 'integer', 'min': 18, 'max': 99},
'email': {'type': 'string', 'required': True, 'regex': r'\S+@\S+\.\S+'},
'is_student': {'type': 'boolean', 'required': True}
}
# Sample data to be validated
data = {
'name': 'Robot A',
'age': 25,
'email': 'robot@example.com',
'is_student': True
}
wrongData = {
'name': 'Robot A',
'age': 17,
'email': 'robot@example.com',
'is_student': True
}
# Create a Validator instance with the defined schema
validator = Validator(schema)
# Validate the data against the schema
if validator.validate(data):
print("Validation successful!")
else:
print("Validation failed.")
print(validator.errors)
if validator.validate(wrongData):
print("Validation successful!")
else:
print("Validation failed.")
print(validator.errors)

Note: We’re using 3.11 version of the Python.

Here’s the code explanation:

  • Line 1: We import the Cerberus library.

  • Lines 3–8: We define the schema from which any input data will be compared and validated.

  • Lines 11–23: We define two dictionaries called data and wrongData. The data dictionary contains data that will be successfully validated by the schema while wrongData contains a dictionary that will be rejected by the schema.

  • Line 26: We create an instance of the Validator class and pass the schema to it.

  • Lines 29–39: We use the validate() method on the data and wrongData dictionaries to see if they match the schema that we defined on lines 3–8.

Additionally, Cerberus allows us to validate inputs that might contain additional fields not explicitly defined in our schema. It provides permission to handle such scenarios by either accepting any unknown fields (setting allow_unknown to True) or enforcing validation rules for those unknown fields.

from cerberus import Validator
# Define schema
schema = {
'name': {'type': 'string'}
}
# Create a Validator instance with the defined schema and allow_unknown = True
validator = Validator(schema, allow_unknown=True)
# Validate some data with an unknown field
data = {
'name': 'Robot',
'age': 30
}
if validator.validate(data):
print("Data is valid!")
else:
print("Data is invalid:", validator.errors)
# Define schema with allow_unknown set to a validation schema
schema_with_validation = {
'name': {'type': 'string'},
'details': {'allow_unknown': {'type': 'integer'}}
}
newvalidator = Validator(schema_with_validation)
# Validate the data with unknown fields validated against the schema
data_with_validation = {'name': 'Robot', 'details': {'phone': 1234567}}
if newvalidator.validate(data_with_validation):
print("Data with validation is valid!")
else:
print("Data with validation is invalid:", newvalidator.errors)

Conclusion

Using Cerberus with its Validator class for data validation allows us to ensure that the information our app receives is correct and safe to use. With Cerberus, we can easily set rules for how our data looks and behaves, ensuring it follows the right format and stays secure from any errors or attacks.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved