Skip to main content

Validators for fields in Flask-Restless

Project description

OSIRIS VALIDATOR

Osiris Validator is a set of decorators for validation with SQLAlchemy

(Readme is under construction...)

Getting Started

Installing

pip install osirisvalidator

Usage

To use the decorators, the validates() decorator from SQLAlchemy must be used before, and the pattern must be followed.

The parameter "field" is mandatory and you can set a custom message.

from sqlalchemy.orm import validates
from osirisvalidator.string import *
from osirisvalidator.internet import valid_email

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(200), nullable=False)
    email = db.Column(db.String(60), nullable=False)

    @validates('name')
    @not_blank(field='name', message='Custom message')
    @is_alpha_space(field='name')
    @string_len(field='name', min=3, max=100)
    def validate_name(self, key, name):
        return name

    @validates('email')
    @not_blank(field='email')
    @valid_email(field='email')
    def validate_email(self, key, email):
        return email

When a validation error occurs, a ValidationException is thrown.

Flask example

@app.route('/saveuser', methods=['POST'])
def saveuser():
    try:
        user = User()
        user.name = request.form['name']
        user.email = request.form['email']
        db.session.add(user)
        db.session.commit()
    except ValidationException as ve:
        flash(ve.errors)
        return redirect(url_for('newuser'))

    return redirect(url_for('index'))

Flask-restless example

The parameter validation_exceptions in APIManager.create_api() from Flask-restless must be set to use osiris' ValidationException.

from osirisvalidator.exceptions import ValidationException

[...]

manager = APIManager(app, flask_sqlalchemy_db=db)
manager.create_api(User, validation_exceptions=[ValidationException], methods=['GET', 'POST'])

See about in: https://flask-restless.readthedocs.io/en/stable/customizing.html#capturing-validation-errors

List of validators

osirisvalidator.string

  • not_empty
  • not_blank
  • is_alpha
  • is_alpha_space (alpha characters and space)
  • is_alnum
  • is_alnum_space (alphanumeric characters and space)
  • is_digit
  • string_len (mandatory parameters: min and max)
  • match_regex (mandatory parameter: regex)

osirisvalidator.number

  • min_max (mandatory parameters: min and max)

osirisvalidator.internet

  • valid_email

osiris.intl.br

  • valid_cpf
  • valid_cnpj

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

osirisvalidator-0.1.1.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

osirisvalidator-0.1.1-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

Details for the file osirisvalidator-0.1.1.tar.gz.

File metadata

  • Download URL: osirisvalidator-0.1.1.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for osirisvalidator-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a80f4b541461360406908fe41a0082fbc5afbbb17e37e86ba0ceddcda8fe1133
MD5 d3f27020d4088f9efedec4366c00a1c9
BLAKE2b-256 a6bc0b70b5ffc27e632a083c79076011fc002593938597ff263f0c71ece34c53

See more details on using hashes here.

File details

Details for the file osirisvalidator-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: osirisvalidator-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 20.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for osirisvalidator-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 82aa4d90f9dea426c34b938e004e86bfe50ce4364ba92103ae18ac7f56851334
MD5 d5c3391ac7c9e8a4c01540dcc57379d2
BLAKE2b-256 0796a408c5453ebd1cb5bb11081ab57b513a2b2a54b9002503d337b48b78d9de

See more details on using hashes here.

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