Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to do basic validation using flask-gladiator in Python

Njoku Ifeanyi Gerald

Flask is a popular and lightweight Python web framework, which means it is a third-party Python library that we can use to develop web applications. Different types of validation are essential when looking at all the unstructured data coming in and out of systems. This protects from malicious input and helps to ensure that the correct form of data is entered.

To perform form validation in Python, we can use the Flask-Gladiator framework. More precisely, we can use the validate method to validate different kinds of data and fields.

Flask-Gladiator features

This framework includes features such as:

  • Allows server-side validation of any form.

  • Allows validation attributes like checking for range, required, type, etc.

  • Can be imported and used in any .py file.

The validate function

The validate function makes use of a the dictionary format to input the data. The validations are initialized as tuples of tuples, where the first element of the tuple is the key to check and the associated values are the validations that should apply to the corresponding key.


The syntax for the validate method is as follows:

validate(data, validators) 


  • data: The data value to validate.
  • validator: The specified option for validation.

The validator parameters are as follows:

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

Return value

This framework returns true if the validation meets the requirements and false if it does not meet the requirements.


First, run the following commands to install the required dependencies:

pip install pipenv 
pipenv shell 
pipenv install Flask
pipenv install Flask-Gladiator
import gladiator as gl

valid_test_data = {
    'email': '',
    'pw': 'pd123',
    'name': 'TopBoy',
    'birth_year': 1999
# assigning validations
registration_form_validator = (
    ('email', gl.required, gl.format_email),
    ('pw', gl.required, gl.length_min(5)),
    ('name', gl.required, gl.type_(str)),
    ('birth_year', gl.required, gl.type_(int))
# checking data using validate()
print("Validating data : ")

result = gl.validate(registration_form_validator, valid_test_data)
print("Is data valid ? : " + str(bool(result)))


Validating data : 
Is data valid ? : True




Njoku Ifeanyi Gerald

View all Courses

Keep Exploring