DontTrust

$ pip install donttrust

Form validation for python

Read the docs Github

Easy to use API

from donttrust import DontTrust, Schema

trust = DontTrust(username=Schema().string().required().strip().min(4).max(16).regex(r"[\w_]+"),
                  password=Schema().string().required().min(8),
                  email=Schema().email().required().allow_mail_providers("gmail", "outlook"),
                  promotions=Schema().boolean().default(False))

print(trust.validate_and_return_json_object({"username": "  test    ", "password": "password1", "email": "test@gmail.com"))
            
# Output
{
    "username": "test",
    "password": "password1",
    "email": "test@gmail.com"
}

Use with any framework

# Flask in this example

from flask import Flask, request, make_response, jsonify
from donttrust import DontTrust, Schema, ValidationError

app = Flask(__name__)


@app.route("/login", methods=["GET"])
def login_route():
    trust = DontTrust(username=Schema().string().required().strip().alphanum().min(4).max(32),
                      password=Schema().string().required().min(8),
                      remember_me=Schema().boolean().default(True))
    
    # Assuming request.form has the fields username, password and optionally, remember_me:
    try:
        return make_response(jsonify(trust.validate(request.form)))
    except ValidationError as e:
        return make_response(jsonify({"field": e.field, "message": e.message}), 400)

if __name__ == "__main__":
    app.run()