Customize
Custom Error Message
We can custom error messages for all validators. Here is an example of how to customize error messages for the Password validator.
# common ways custom error messages
Numeric(message="Value should be a number").validate(5.2,'data')
# example Password
Password(messages={"numbers": "one number"})
Note
Only the password rule has different ways to customize error messages.
Custom Rule
- We can create custom rules by create a class that has a
validatemethod. - in the
validatemethod must has two parametersvalueandfield_name. - The
valueis the data that we want to validate and thefield_nameis the name of the field that we want to validate. - The
validatemethod must returnNoneif the data is valid, otherwise return the error message (string).
# create a custom rules
class UniqueRule:
def __init__(self, table, field):
self.table = table
self.field = field
def validate(self, value, field_name):
# validate data is unique or not
if not unique:
return f"{field_name} is not unique"
return None
see at example how to use the custom rule.