Thursday, November 28, 2024
Google search engine
HomeLanguagesBasic Validation using flask-gladiator module in Python

Basic Validation using flask-gladiator module in Python

All kinds of validation are essential nowadays, considering all the unstructured data flowing in and out of systems. Apart from client-side validation, server-side validations are equally essential in web development. This article discusses a way in which validation can be injected in various frameworks such as Flask or Django.

Python’s flask-gladiator is a module that provides the following features:

  • Allowed server-side validation of any form.
  • Can be extended to any .py framework.
  • Allows functionalities like checking for range, required, type, etc.

Installation 

This module doesn’t come inbuilt with python and thus has to be installed explicitly

pip install flask-gladiator

From this module, validate() function is used to get the job done.

Syntax:

validate(data, validators) 

Parameters:

  • data: data in consideration
  • validators: tells on what basis validation has to be done
    • required :  The required validator to check for presence of field always.
    • format_email : Validator for checking email.
    • length_max : Checks for maximum length of text.
    • length_min : Checks for minimum text length.
    • length : Checks for particular length.
    • type_ : Checks for particular type.
    • regex_ : Checks for regex.
    • _value : Checks for particular value.
    • in_ : Checks in particular list.
    • lt : Checks for less than integer value.
    • gt : Checks for greater than integer value.
    • eq : Checks for equal integer value.
    • ne : Checks for not equal integer value.
    • gte : Checks for greater than equal integer value.
    • lte : Checks for less than equal to integer value.
    • true_if_empty : If empty, this validator returns true.
    • skip_on_fail : If the validation failed, this can be used to skip testing for validation.

Approach : 

  • In the initial step, the input data is formulated in dictionary format.
  • The validations are initialized as tuples of tuples, where 1st element of the tuple is key to check and the next values are validations that should apply to the corresponding key.
  • validate() function takes validations and input dictionary and returns True if all validations hold true, else returns False.

Given below are its various implementations that perform validations based on different validators.

Example 1 : 

Python3




import gladiator as gl
  
# input test data
valid_data = {
    'email': 'manjeet@gfg.com',
    'password': '123456',
    'name': 'Manjeet',
    'age': 24,
    'access': "User"
}
  
# assigning validations
field_validations = (
    ('email', gl.required, gl.format_email),
    ('password', gl.required, gl.length_min(5)),
    ('name', gl.required, gl.type_(str)),
    ('age', gl.required, gl.type_(int), gl.eq(24)),
    ('access', gl.required, gl.in_(['User', 'Admin']))
)
  
# checking data using validate()
print("Validating data : ")
result = gl.validate(field_validations, valid_data)
print("Is data valid ? : " + str(bool(result)))


Output : 

Validating data : 

Is data valid ? : True

Example 2 :

Python3




# Using regex, gt and length validators
import gladiator as gl
  
# input test data
valid_data = {
    'email': 'manjeet@gfg.com',
    'password': '123456',
    'name': 'Manjeet',
    'Gender': 'M',
    'age': 24,
  
}
  
# assigning validations
# checks name by regex, gender using length range, age greater than 18.
field_validations = (
    ('email', gl.required, gl.format_email),
    ('password', gl.required, gl.length_min(5)),
    ('name', gl.required, gl.type_(str), gl.regex_('[a-zA-Z][a-zA-Z ]+')),
    ('age', gl.required, gl.type_(int), gl.gt(18)),
    ('Gender', gl.length(1, 4)),
)
  
# checking data using validate()
print("Validating data : ")
result = gl.validate(field_validations, valid_data)
print("Is data valid ? : " + str(bool(result)))


Output : 

Validating data : 

Is data valid ? : True

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments