Orator Validator provides the best Model implement validation for Orator
Project description
This is an orator plugin that you can use to validate your model when the user is creating a new item or updating one on the database is easy to use and cleans the code a lot
Installation
You can install the plugin by using pip
$ pip install orator-validator
How to use it
this is an example of how to implement on your code
from orator import Model
from orator_validator import Validator
class User(Model, Validator):
__connection__ = 'local'
__fillable__ = [
'name', 'email', 'password', 'phone_number'
]
__guarded__ = ['id', 'password']
class UserValidation(object):
def saving(self, user):
user.validate('name', require=True, data_type=str)
user.validate(
'email', regex="(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"
)
user.validate(
'password', regex="^(?=.*[A-Za-z])(?=.*\\d)(?=.*[@$!%*#?&])[A-Za-z\\d@$!%*#?&]{6,}$"
)
user.process('phone_number', exist=self.__process_phone)
user.errors()
def updating(self, user):
user.validate_update('email', guarded=True)
user.validate_update(
'password', function_callback=self.__validate_new_password, user=user
)
user.errors()
def __validate_new_password(self, user):
'''
Validate that the new password is diferent than the old one
'''
User.find(user.id)
if user.password == User.find(user.id).password:
raise Exception("Can't update password with old one")
def __process_phone(self, user):
'''
This function process the phone if the user send one
'''
if user.phone[0] != "+":
user.phone = "+1 {}".format(user.phone)
User.observe(UserValidation())
the validate function accept this params
require: boolean when True checks if they send the value
data_type: object Verifies if the value is specific data type
regex: string pass a regex to verified
date_str: string witch you want to check the format of the date example ‘%H:%M’
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
orator_validator-0.4.0.tar.gz
(4.0 kB
view hashes)
Built Distribution
Close
Hashes for orator_validator-0.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf87cf786d09b20e32035e28e45b74e353f844ea5f7de88f7df92724af24f4ff |
|
MD5 | c83a649ec8b9e47cb0a04273f31ab9a7 |
|
BLAKE2b-256 | 1af318582d78e47bdf002d809f68d2409343930a60cc0acb48a0942171f12227 |