Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flaskvel-2.0.tar.gz (23.9 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page