Custom Error Messages¶
You can add custom error message to validators via the err_msg parameter.
If you would like to interpolate the argument names and values into your
message, you can do that using the following syntax:
${arg_name}: To interpolate the name of the argument${arg_value}: To interpolate the value of the argument
If you are using the DependsOn validator, you can further customize your
error message using two extra interpolated values as follows:
${dep_arg_name}: To interpolate the name of the dependent argument${dep_arg_value}: To interpolate the value of the dependent argument
You can also provide extra interpolated arguments via the extra_msg_args
parameter using a Python dictionary where the keys are the names used in the
err_msg (${key}) and values are the interpolated values.
Example Custom Message¶
In [11]:
Copied!
# Config to have a single line output of Exception error message
import sys; sys.tracebacklimit = 0;
%xmode plain
# Config to have a single line output of Exception error message
import sys; sys.tracebacklimit = 0;
%xmode plain
Exception reporting mode: Plain
Imports¶
In [12]:
Copied!
from typing import Annotated
from func_validator import validate_params, DependsOn, MustBePositive, MustBeLessThan
from typing import Annotated
from func_validator import validate_params, DependsOn, MustBePositive, MustBeLessThan
Custom Message Without Interpolation¶
In [13]:
Copied!
@validate_params
def example_func(
arg_1: Annotated[float, MustBePositive(err_msg="Must be greater than zero")]
): ...
@validate_params
def example_func(
arg_1: Annotated[float, MustBePositive(err_msg="Must be greater than zero")]
): ...
In [14]:
Copied!
example_func(-10)
example_func(-10)
ValidationError: Must be greater than zero
Custom Message With Interpolation¶
In [15]:
Copied!
@validate_params
def example_func_2(
arg_2: Annotated[float, MustBePositive(err_msg="${arg_name} should be greater than zero not ${arg_value}")]
):...
@validate_params
def example_func_2(
arg_2: Annotated[float, MustBePositive(err_msg="${arg_name} should be greater than zero not ${arg_value}")]
):...
In [16]:
Copied!
example_func_2(-10)
example_func_2(-10)
ValidationError: arg_2 should be greater than zero not -10
In [17]:
Copied!
@validate_params
def example_func_3(
age: int,
height: Annotated[int,
DependsOn("age", args_strategy=MustBeLessThan, err_msg="${arg_name}: ${arg_value} should be less than ${dep_arg_name}: ${dep_arg_value}")
],
): ...
@validate_params
def example_func_3(
age: int,
height: Annotated[int,
DependsOn("age", args_strategy=MustBeLessThan, err_msg="${arg_name}: ${arg_value} should be less than ${dep_arg_name}: ${dep_arg_value}")
],
): ...
In [18]:
Copied!
example_func_3(10, 14)
example_func_3(10, 14)
ValidationError: height: 14 must be < 10.
Custom Message With Extra Interpolated Arguments¶
In [22]:
Copied!
@validate_params
def example_func_4(age: Annotated[int,
MustBePositive(
err_msg="${name} ${arg_name} should be greater than zero not ${arg_value}",
extra_msg_args={"name": "Patrick's"}
)
]
): ...
@validate_params
def example_func_4(age: Annotated[int,
MustBePositive(
err_msg="${name} ${arg_name} should be greater than zero not ${arg_value}",
extra_msg_args={"name": "Patrick's"}
)
]
): ...
In [23]:
Copied!
example_func_4(-10)
example_func_4(-10)
ValidationError: Patrick's age should be greater than zero not -10