The robust validation library for flask
pip install flask-validityfrom flask import Flask
from validity.Validation import Validation 
app = Flask(__name__)
validation = Validation()
@app.route('/')
@validation .validate(
[
 {
 "field": "title",
 "required": True,
 "type": "str",
 "min": 10,
 "max": 30,
 "regex": None
 }
]
)
def index():
    return 'Web App with Python Flask!'
app.run(host='0.0.0.0', port=81)This class used to validate any incoming json request to your flask route, it validate the request after validation passes then the request enters your route function.
It amins to allow you to write once and used a lot instead of validation request on every route it is better to do it once at top of your function.
It support number of parameter are as follow:
fieldThis is required parameter which indicate the field name you want to accpet.requiredThis is not required parameter but if this not present any of below validation will not considered, its value eitherTrueorFalsetypeWhich type of request value should be it supportint,float,list,bool,dictandstrminandmaxThese are optional parameter but if one of them present other is required its used to indicate restriction to min and max length or value.regexIt is optional parameter but It is used to validate request base on regex.emailIt is optional parameter it validate email.phoneIt is optional parameter it validate phone.fileIf validat file set this to true.sizeFile size in bytes.extFile extension it accpet python list.mimeFile mime types, it accpet python list.
Validity support three method for returning the errors
- As json
- Return as json array.
 
 - As String
- Return as string.
 
 - As query string
- Return redirect with query parameters.
 
 - None
- None mean it will do nothing but you can get the error with 
validator.Errors 
 - None mean it will do nothing but you can get the error with 
 
You can pass these flags to validate as second argument.
@app.route('/process', methods=['POST', 'GET'])
@validator.validate(
[{
"field": "name",
"required": True,
"type": "str",
}],
None
)
def  process():
	 errors = validator.Errors
	 # rest of code..Currently it support two languages out of the box that includes:
- English
 - Urdu
 
You can set the language as below:
# ... 
validator  =  Validator("en")
# ...You can add your langauge by sending PR and/or you can pass validator constructor language string as below, assume it will be your own language:
# ...
validator  =  Validator("en", {
"required": "The %s is required.",
"extension": "The %s extension is not allowed.",
"mime": "The %s mimetype is not allowed.",
"size": "The %s size is too large.",
"empty": "The %s must not be empty.",
"type": "The %s must be %s .",
"between": "The %s must be between %s and %s .",
"regx": "The %s must be in correct format.",
"phone": "The phone number should be valid.",
"email": "The email should be valid.",
"error": "Unable to decode the data."
})
# ...Thank you for considering contributing to the validity! Feel free to create a pull request.
- GNU GPL3
 
If you discover a security vulnerability within Validity, please send an e-mail to our team via security@alphasofthub.com. All security vulnerabilities will be promptly addressed.