A requests validator for Flask inspired by Laravel.
This package provides an approach to validating incoming requests using powerful and composable rules.
To install and update using pip.
pip install -U flask-sieve
To learn about these powerful validation features, let's look at a complete example of validating a form and displaying the error messages back to the user.
Suppose you had a simple application with an endpoint to register a user. We are going to create validations for this endpoint.
# app.py
from flask import Flask
app = Flask(__name__)
@app.route('/', methods=('POST',))
def register():
return 'Registered!'
app.run()
To validate incoming requests to this endpoint, we create a class with validation rules of registering a user as follows:
# app_requests.py
from flask_sieve import FormRequest
class RegisterRequest(FormRequest):
def rules(self):
return {
'email': ['required', 'email'],
'username': ['required', 'string', 'min:6'],
'password': ['required', 'min:6', 'confirmed']
}
Now, using this class, we can guard our endpoint using a validate
decorator.
# app.py
from flask import Flask
from flask_sieve import Sieve, validate
from .app_requests import RegisterRequest
app = Flask(__name__)
Sieve(app)
@app.route('/', methods=('POST'))
@validate(RegisterRequest)
def register():
return 'Registered!'
app.run()
If the validation fails, the proper response is automatically generated.
Find the documentation here.
A Flask package for validating requests (Inspired by Laravel).
Copyright © 2019 Edward Njoroge
All rights reserved.
Find a copy of the License here.