
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to validate data using Cerberus in python
Introduction
The Cerberus module in python provides powerful yet lightweight data validation functions. It is designed in such a way that you can extend it to various applications and custom validations.
We first define a schema and then validate the data against the scheme and check if it matches the provided conditions or not. If not, accurate errors are thrown to display where it went wrong.
Various conditions can be applied at once to the data field for validation.
Getting started
In order to use Cerberus, you must first install it, as it does not come packaged with Python.
In order to download and install it, we use the pip package manager.
Launch your terminal and use the command below.
pip install Cerberus
Once you have successfully downloaded and installed the Cerberus library, you must import it’s validator module into your Python script.
from cerberus import Validator
And that’s it. You are all set to start validating data.
Validating data present in a dictionary
First, we must create a schema.
schema = {'numbers': {'type': 'integer'}} v = Validator(schema)
This means, in a python dictionary, the numbers field must contain only integers.
data = {'numbers': 5}
The above is the data that we need to validate.
if v.validate(data): print("Data is valid") else: print("Data is invalid")
This validates the data against the scheme we had created earlier.
Example
from cerberus import Validator schema = {'numbers': {'type': 'integer'}} v = Validator(schema) data = {'numbers': 5} if v.validate(data): print("Data is valid") else: print("Data is invalid")
Output
Data is valid .
Validating with various rules and printing errors
from cerberus import Validator v = Validator() v.schema = {'ID': {'required': True, 'type': 'number'}, 'age': {'type': 'integer'}} if v.validate({'age': 60}): print('Data is valid') else: print('Data is invalid') print(v.errors)
Output
Data is invalid, {'ID': ['required field']}
Setting minimum and maximum value ranges
from cerberus import Validator v = Validator() v.schema = {'name': { 'type': 'string', 'minlength': 5}, 'age': {'type': 'integer', 'min': 18, 'max': 65}} if v.validate({'name': 'VJ', 'age': 16}): print('Data is valid') else: print('Data is invalid') print(v.errors)
Output
Data is invalid {'age': ['min value is 18'], 'name': ['min length is 5']}
And that’s it, you can now validate dictionaries, json files, etc in a similar way using the Cerberus library.
Conclusion
You have learnt to validate data using the Cerberus library with your own custom made schema.
Using this method, you can validate json files, data extracted from an API, etc.
We validated data mainly in order to avoid errors while building a database or while working on data analysis.
You can even automate validating data and build a dynamic website based on the same.
- Related Articles
- How to validate an email id using regular expression in Python?
- How to validate email using jQuery?
- How to validate a form using jQuery?
- How to validate email address using RegExp in JavaScript?
- How to validate the IP address using PowerShell?
- How to validate a URL using regular expression in C#?
- How to validate Email Address in Android on EditText using Kotlin?
- Validate IP Address in Python
- How to write a Python Regular Expression to validate numbers?
- Python program to validate email address
- How to validate an email address using Java regular expressions.
- Validate Binary Search Tree in Python
- Python program to validate postal address format
- Program to validate a user using JSP
- How can I validate EditText input in Android using Kotlin?
