×

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

Related Topics

Rank Based Percentile GUI Calculator using Tkinter in Python

GUI: The user is provided with information using manipulable visual widgets that don't require command-line input. These interface components respond to the user's interactions per the pre-programmed script, assisting each user's...

3 minutes read.

Find key from value in dictionary python

Python: Python programming language is one of the most used programming languages, as it is used widely in software and data analysis, web development, etc. It is said to be a...

5 minutes read.

Sort Dictionary in Python

Dictionaries In Python, a dictionary is an unordered collection or set of data types that enable of store data in an unordered key-value/pair. The key is stored alongside with value. Dictionary contains key:...

4 minutes read.

Merge Sort using Python

Merge Sort is a technique that is used for sorting elements in an array using a special method known as divide and conquer. It is the best example of the...

5 minutes read.

Python IDE names

Python is one of the most renowned programming languages. It has different execution conditions and a great many compilers to execute the python programs, e.g., PyCharm, PyDev, Jupyter Notebook, Visual...

6 minutes read.

Python abs() function

Python abs() function The abs() function returns the absolute value of a number. Syntax abs(x) Parameter x: The parameter ‘x’ can be an integer value, a floating point number or a complex number. Return This function returns...

1 minute read.

Anonymous/Lambda Function in Python

Lambda keyword is used to declare an Anonymous function, i.e. a function that does not have any name. It is also called Anonymous functions. In python, normal functions are defined...

3 minutes read.

Pointers in Python

In this tutorial, we will study what pointers are and if they have any utility in python. Now, let us understand what pointers are Pointers Pointers are special variables used to store the...

3 minutes read.

Python range() function

Python range() function The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. Syntax range(stop)        or range(start, stop[, step]) Parameter start: It is...

1 minute read.

Python JSON

In this tutorial, we will learn about JSON in the Python programming language. We will focus on its features, Guidelines to be kept in mind while writing its syntax, the...

3 minutes read.

Python Dictionary

Dictionary in Python is an unordered collection of data values, which is used to store data values like a map. Unlike different data types that hold just individual assets as...

4 minutes read.

Write Text files in Python

Let's discuss writing the text file in detail. Generally, writing text also follows the same procedure as like reading text. It varies only in some specific places. Steps followed for writing...

4 minutes read.

Python TypeError

What is TypeError in python? TypeError is one of the exceptions in the python programming language. This exception occurs when an operation is performed on an unsupported object type or can...

6 minutes read.

Nested for Loop in Python

"Loop" is one of the foundation concepts to learn programming in any language. Nested loops are significant steps in solving many types of problems, ranging from basic to complex scenarios....

9 minutes read.

Run exec python from PHP

PHP (which is a recursive abbreviation for PHP Hypertext Preprocessor) is one of the most broadly involved web improvement innovations on the planet. PHP code utilized for creating sites and...

4 minutes read.

How to uninstall python

To un-install Python, we need to follow different procedures in different operating systems. In this article, we will discuss the un-installation process of Python in Windows, Mac, and Linux operating...

4 minutes read.

Isreal() Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

4 minutes read.

STL in Python

Python has many libraries that are very useful and simplify many complex codes. In the same way, STL is also one of the python libraries used for reading and writing...

3 minutes read.

How to Define a Function in Python?

What is a Function? In programming, a piece of code that executes a specific task or a group of related operations is known as a function. What does Python Functions Do? If you...

6 minutes read.

How to Read html page in python

Html is a Hyper Text Markup language is a standard language used for creating webpages. HTML is the name of the language used to describe the construction of Web pages....

4 minutes read.