How to validate data using Cerberus in python

PythonServer Side ProgrammingProgramming

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.

raja
Updated on 11-Feb-2021 06:44:52

Advertisements