All Projects → xeBuz → Flask-Validator

xeBuz / Flask-Validator

Licence: MPL-2.0 license
Validator for SQLAlchemy Models

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Flask-Validator

mock-alchemy
SQLAlchemy mock helpers.
Stars: ✭ 44 (+62.96%)
Mutual labels:  sqlalchemy
OpenAlchemy
Define SQLAlchemy models using the OpenAPI specification.
Stars: ✭ 39 (+44.44%)
Mutual labels:  sqlalchemy
sqlalchemy-adapter
SQLAlchemy Adapter for PyCasbin
Stars: ✭ 53 (+96.3%)
Mutual labels:  sqlalchemy
thanker
Don't be a wanker, be a thanker! Automatically give thanks to Pypi packages you use in your project.
Stars: ✭ 25 (-7.41%)
Mutual labels:  pypi
pycayennelpp
A Cayenne Low Power Payload (CayenneLPP) decoder and encoder for Python
Stars: ✭ 17 (-37.04%)
Mutual labels:  pypi
duckpy
A simple Python library for searching on DuckDuckGo.
Stars: ✭ 20 (-25.93%)
Mutual labels:  pypi
django-materializecss-form
Materializecss for Django Form
Stars: ✭ 83 (+207.41%)
Mutual labels:  pypi
flask-template
Template for creating Flask based projects
Stars: ✭ 60 (+122.22%)
Mutual labels:  sqlalchemy
VanillaConstraints
🍦 Simplified and chainable AutoLayout constraints for iOS.
Stars: ✭ 42 (+55.56%)
Mutual labels:  constraints
Driftwood
Driftwood is a DSL to make Auto Layout easy on iOS, tvOS and macOS.
Stars: ✭ 14 (-48.15%)
Mutual labels:  constraints
bag
A Python library for several purposes
Stars: ✭ 25 (-7.41%)
Mutual labels:  sqlalchemy
databricks-dbapi
DBAPI and SQLAlchemy dialect for Databricks Workspace and SQL Analytics clusters
Stars: ✭ 21 (-22.22%)
Mutual labels:  sqlalchemy
magql
The magical GraphQL framework that generates an API for your data.
Stars: ✭ 26 (-3.7%)
Mutual labels:  sqlalchemy
yosai alchemystore
SQLAlchemy-enabled Account Store for Yosai that features a flat Role-Based Access Control (RBAC) data model
Stars: ✭ 17 (-37.04%)
Mutual labels:  sqlalchemy
copulae
Multivariate data modelling with Copulas in Python
Stars: ✭ 96 (+255.56%)
Mutual labels:  pypi
CourseCake
By serving course 📚 data that is more "edible" 🍰 for developers, we hope CourseCake offers a smooth approach to build useful tools for students.
Stars: ✭ 21 (-22.22%)
Mutual labels:  sqlalchemy
lit-ncov-report
洛阳理工学院 "健康状况管控平台" , 非官方Python封装库兼CLI工具与拓展实现
Stars: ✭ 41 (+51.85%)
Mutual labels:  pypi
python-package-template
Easy to use template for great PyPi packages
Stars: ✭ 19 (-29.63%)
Mutual labels:  pypi
generate-word-cloud.py
🐍 A simple Python (2 or 3) script to generate a PNG word-cloud ☁️ image from a bunch of 📂 text files 🎉. Based on word_cloud by @amueller.
Stars: ✭ 19 (-29.63%)
Mutual labels:  pypi
hypothesis sqlalchemy
hypothesis strategies for generating SQLAlchemy objects
Stars: ✭ 24 (-11.11%)
Mutual labels:  sqlalchemy

Flask-Validator

PyPi status PyPI version Travis Documentation Status Requirements Status Coverage Status Code Climate Codacy Badge Code Climate technical debt PyPI - Downloads PyPI - Python Version GitHub license


Description

Data validator for Flask using SQL-Alchemy, working at Model component with events, preventing invalid data in the columns. The extension works with event listeners from SQLAlchemy.

Installation

pip install flask-validator

Documentation

For the online documentation, follow this link

Basic usage

The most performant way to set up your validations is during the SQLAlchemy special directive_ __declare_last__, it occurs after mappings are assumed to be completed and the 'configure' step has finished.

from flask_validator import ValidateInteger, ValidateString, ValidateEmail

class User(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))
    code = db.Column(db.Integer())
    email = db.Column(db.String(125))

    def __init__(self, string, integer):
        self.string = string
        self.integer = integer

    @classmethod
    def __declare_last__(cls):
        ValidateString(User.name)
        ValidateInteger(User.code)
        ValidateEmail(User.email)

user = User('Arthur Dent', 42, '[email protected]')

user.name = 666
print user.name
# 'Arthur Dent'
user.name = 'Zaphod Beeblebrox'
print user.name
# 'Zaphod Beeblebrox'

Exceptions

Every Constraint has a parameter to throw an exception everytime the validation fails, for example:

ValidateNumeric(Table.field, False, True, "Message")

The third parameter enables this feature and throw a ValidateError exception, otherwise it will fails silently.

Message Exception

The fourth parameter allow a custom message exception, with a few variables available

  • old_value: value previous to the modification
  • new_value: value provided (with the error)
  • key: the column name
  • field: object.column

Available Constraints

  • Types
    • ValidateInteger
    • ValidateNumeric
    • ValidateString
    • ValidateBoolean
  • Numeric
    • Validatelength
    • ValidateNaN
  • Comparision
    • ValidateLessThan
    • ValidateLessThanOrEqual
    • ValidateGreaterThan
    • ValidateGreaterThanOrEqual
  • Internet
    • ValidateEmail
    • ValidateIP
    • ValidateURL
  • Location
    • ValidateCountry
    • ValidateTimezone
    • ValidateLocale
  • Financial
    • ValidateCreditCard
    • ValidateCurrency
    • ValidateIBAN
    • ValidateBIC
  • Others
    • ValidateUUID
    • ValidateISBN'
    • ValidateRegex
    • ValidateRange

Custom Validators

You will be able to create customs validator implementing the class Validator.

You must define your own method check_value() and if you are receiving any argument, you also must call the parent __init__()

from flask_validator import Validator

class ValidateAorB(Validator)
    def __init__(self, field, useless, allow_null=True, throw_exception=False, message=None, interpolate_message):
        self.useless = useless

        Validator.__init__(self, field, allow_null, throw_exception, message, interpolate_message):

    def check_value(self, value):
        return if value in ['A', 'B']

class ValidateA(Validator)
    def check_value(self, value):
        return if value == 'A'

Pause the validation

The extension has two methods to stop and restart the listener.

class User(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))
    code = db.Column(db.Integer())
    email = db.Column(db.String(125))

    def __init__(self, string, integer):
        self.string = string
        self.integer = integer

# Initialize the validator
validate =  ValidateString(User.name)

# Do something validated
# ...

validate.stop()

# Assign values without being validated
# ...

validate.start()

# Re-enabled the listener
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].