Python Validator
Validator
The validator is a library available in the python programming language. The library in python consists of all the related modules which can be imported to perform the required operation. The library is the same as the library in a college or school, where a lot of books are stored in a room; from these books, we can collect the required book, and we can read the book in a similar way the library in the python programming language is the collection of the precompiled codes, templates and the documentation which can be used in a program based on the required operation.
The libraries are used such that the present code can be used in the program without re-writing the code. The validator library consists of the validate( ) function, which can be used to validate whether the given condition is correct; in return, the validate( ) function will return only True or False. Many libraries are imported from the python installation package( pip). The libraries are imported into the program by running the following syntax:
Syntax:
#import <library>
Parameters:
- Library: Different libraries have different names and can be used for different operations in the program.
Python Validator Library
The validator package is installed using the python installation package(pip ) then from this; we can import the validator library to perform necessary operations.
Command:
# installing the validator library using the python installation package( pip )
pip install validator
Working of validator library:
The validate function is imported from the validator, and then we need to create two dictionaries, one to hold the values and the other dictionary to hold the condition; then, these two libraries are passed into the validate function to check whether the values pass the condition or not.
Then finally, in the output, we can observe either True or False; if the values pass the condition, then we can observe the True in the output, and if the values do not pass the condition, we can observe the False in the output.
Example:
#importing the validate function from the validator library
from validator import validate
# input the values that are to be checked and the condition dictionary
values = {“ name”: “Nikitha”
“age”: 20
“height”: 5’1 }
condition : { “name” : “required”
“age” : “required”
“height” : “required” }
output = validate( values, condition)
# displaying the output
print(output)
Output:
True
Here we can observe that, as the values have passed all the conditions, we can observe True in the output. Now let us consider another example which does not pass all the conditions, and we will observe the output.
Example:
#importing the validate function from the validator library
from validator import validate
# input the values that are to be checked and the condition dictionary
values = {“ name”: “Nikitha”
“age”: 20}
condition : { “name” : “required”
“age” : “required”
“height” : “required” }
output = validate( values, condition)
# displaying the output
print(output)
Output:
False
Here we can observe that the user does not provide all the values; we can observe that the output is False. We can also observe the program's output with the help of the validator class; we can create a class and pass these values and condition dictionaries then we can observe the output.
Example:
#importing the validate function from the validator library
from validator import validate
# input the values that are to be checked and the condition dictionary
values = {“ name”: “Nikitha”
“age”: 20
“height”: 5’2 }
condition : { “name” : “required”
“age” : “required”
“height” : “required” }
# Creating a variable which must be passed into the validator class to observe the output.
vali = validator( values, condition)
output = vali.validate( )
# displaying the output
print(output)
Output:
True
Validating Arrays
The validator library also consists of the validate_many( ) function, which is imported to check for more than one value in the form of an array. Let us consider an example of more than one value passed into the condition with the help of the validate_many( ) function, and we will observe the output.
Example:
#importing the validate_many( )function from the validator library
from validator import validate
# input the values that are to be checked and the condition dictionary
values = {“ name”: “Nikitha”
“name”: “Nihaas”
“name”: “Kavlas” }
condition : { “name” : “required | min:3” }
output = validate( values, condition)
# displaying the output
print(output)
Output:
True