A small package for validating incoming HTTP requests
Project description
FlaskVel
A small package that provides a convenient method to validate incoming HTTP requests with a variety of powerful validation rules, highly customizable and heavily influenced by Laravel.
Jump straight to the documentation.
Instalation
To install FlaskVel run:
pip install flaskvel
FlaskVel is now installed, check out the Quickstart.
This package is only compatible with Python 3+.
Quickstart
Lets suppose we want an endpoint that is used to register a user. First of all, we have to instantiate Flask and to also initialize FlaskVel by calling the constructor with its appropriate parameters.
# main.py
from flask import Flask, jsonify
from flaskvel import Flaskvel, validate, BodyFormats
app = Flask(__name__)
Flaskvel(app)
Now we are ready to define our endpoint.
# main.py
@app.route("/register", methods=["POST"])
def register():
# some code for registering the user
# ...
return jsonify({"status": "ok"}), 200
To add validations let's create a class that derives from flaskvel.Validator
and contains the rules.
# MyValidator.py
from flaskvel import Validator, Rules
class MyValidator(Validator):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) # MUST always be called first
self.rules = {
"username": ["required", "string"],
"password": ["required", "string", "min:8", "max:32", "confimed"],
"email": [Rules.REQUIRED, Rules.EMAIL] # we can also use predefined constants instead of strings
}
For more info about writing rules see Rules syntax.
Now we can add our validator to the endpoint using one of the decorators provided by FlaskVel.
@app.route("/register", methods=["POST"])
@validate(MyValidator, BodyFormats.ANY)
def register():
return jsonify({"status": "ok"}), 200
The decorator used,
@validate(...)
or@validate_no_validator(...)
, must be positioned after@app.route(...)
.
You can find a list of all the rules here.
Jump straight to the documentation.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file flaskvel-2.1.tar.gz
.
File metadata
- Download URL: flaskvel-2.1.tar.gz
- Upload date:
- Size: 23.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b2ac638dd877cbcb99300a4bfe9315fd6e2b59b3937d85f8c709ba034ddecfe |
|
MD5 | 105b13869489df4d90cc777a94d1ae3d |
|
BLAKE2b-256 | 8ec76b6b339449616fe2aa190c34b0aeedf2f54a6eef3d6ae9d03b26e837aa01 |