Data Validation
Data Validation
Usage Example
Declaring your schema
from vibora.schemas import Schema, fields
from vibora.schemas.exceptions import ValidationError
from vibora.schemas.validators import Length, Email
from vibora.context import get_component
from .database import Database
class AddUserSchema(Schema):
@staticmethod
async def unique_email(email: str):
# You can get any existent component by using "vibora.context"
database = get_component(Database)
if await database.exists_user(email):
raise ValidationError(
'There is already a registered user with this e-mail'
)
# Custom validations can be done by passing a list of functions
# to the validators keyword param.
email: str = fields.Email(pattern='.*@vibora.io',
validators=[unique_email]
)
# There are many builtin validation helpers as Length().
password: str = fields.String(validators=[Length(min=6, max=20)])
# In case you just want to enforce the type of a given field,
# a type hint is enough.
name: strUsing your schema
Last updated