validation using python – JSON or dictionary

If you want to check whether the user has entered an integer or whether the input has 15 characters or whether it is a valid email, then this article is definitely for you.

Usually, validations are done in the front end in case of web applications. But, if you are developing any web application with python using Flask or Django and if we need to perform those validations at the back-end then this library comes in handy.

The validators library is a perfect solution for python validation or the back-end validation. Let us get started with the installation.

Installation

The package can be easily installed using pip.

pip install validator

or we can clone the repo from GitHub and install it from there. The link to their repo is here.

Get started

This library requires the input to be in the form of dictionary. As we all know the body of a POST or a PUT request will usually be in the form of JSON. We can get that data convert it into dictionary and then provide as input to the validator class.

If our input is dictionary it is even straight forward.

Simple validation

The user should have two dictionaries.

  1. The data.
  2. The rules or validations dictionary.

Suppose our data may look like this.

And the rules dictionary is,

What we basically define by the rules is that the name field is required, age field should be an integer and it should have a minimum value of 18 and the mail field is also required and the type is mail.

Putting it all together our code to perform validation will look like this.

Validations

The following validations are possible with this library.

validationssyntax Example
between {“key”: “between: value1, value2”} {“age” : “between:6,18”}
integer{“key”: “integer”}{“num” : “integer”}
IP address{“key”: “ip”}{“ip_addr” : “127.0.0.1” }
IPv4{“key”: “ipv4”}{“ipv4_addr” : “127.0.0.1”}
IPv6{“key”: “ipv6”}{“ipv6_addr” : “2001:0db8:85a3:0000:0000:8a2e:0370:7334”}
list {“key” : “list”}{“arr” : [1, 2, 3]}
mail{“key” : “mail”}{“email_addr” : “[email protected]”}
max{“key” : “max:value”}{“age”: 18}
min{“key”: “min:value”}{“age”: 18}
regex{“key” : “regex:^[0-9a-zA-Z]*$”}{“id”: “abc123”}
required{“key” : “required”}{“name”: “should not be empty”}
same{“new_pass”: “same:old_pass”}{“old_pass”: “password”, “new_pass”: “password”}
size{“key” : “size:6”}{“zip”: “123456”}

These are the various possible validations available in this library.

Error messages

We can also get the error messages if the validation fails. We should just add “return_errors=True” option to the validate class.

This will return a tuple. One is a boolean value indicating whether the validation is true or false and the other one is the errors.

If we print result the output is,

True

If we print errors the output is,

{'name': {'Required': 'Field was empty'},
mail': {'Mail': 'Expected a Mail, Got: jon_doe'}}

Conclusion

This article will be useful and come in handy if you are doing backend validations in your web applications. If you want to know more methods and usages, take a look at their Github repo.

Happy coding!