pyhs3.exceptions.custom_error_msg

pyhs3.exceptions.custom_error_msg(custom_messages)[source]

Customize an error message for pydantic validation errors.

See https://github.com/pydantic/pydantic/discussions/8468.

Example:

>>> from typing import Annotated
>>> from pydantic import BaseModel
>>> from pydantic.types import StringConstraints
>>> NameString = Annotated[
...     str,
...     StringConstraints(pattern=r"^[a-zA-Z0-9]*$"),
...     custom_error_msg({"string_pattern_mismatch": "The field {field_name} can only contain letters and numbers."}),
... ]
>>> class Model(BaseModel):
...     name: NameString
>>> Model(name="dog@123")
Traceback (most recent call last):
...
pydantic_core._pydantic_core.ValidationError: 1 validation error for Model
name
  The field name can only contain letters and numbers. ...
Parameters:

custom_messages (dict[str, str])

Return type:

Any