Utility functions for Pydantic-based data validation.

This module provides reusable validator functions designed to be used with Pydantic models to enforce data consistency, formatting, and business rules. These validators are particularly useful in API request/response handling where user input requires strict sanitization and meaningful error reporting.

Features: - String normalization (whitespace stripping, empty value handling) - Email format validation using Pydantic's EmailStr - Geographic coordinate validation (latitude and longitude ranges) - Support for custom error messages via error_types dictionary - Integration with Pydantic's PydanticCustomError for consistent error codes

strip_validator

strip_validator(value: str) -> str

Strip whitespace from both ends of a string.

Parameters:
  • value (str) –

    Input string to be stripped.

Returns:
  • str –

    The stripped string.

Example
print(PydanticValidator.strip_validator("  test  "))
#> "test"

optional_string_validator

optional_string_validator(
    value: Any | None, *, error_types: dict | None = None
) -> str | None

Validate and strips an optional string value.

Parameters:
  • value (Any | None) –

    Input value to be validated.

  • error_types (dict | None, default: None ) –

    Optional dict mapping error codes to custom messages. If None, defaults to PYDANTIC_ERROR_TYPES.

Returns:
  • str | None –

    A stripped string or None if the value is empty.

Raises:
  • PydanticCustomError –

    If the value is not a string.

Example
print(PydanticValidator.optional_string_validator("  test  "))
#> "test"

print(PydanticValidator.optional_string_validator(""))
#> None

print(PydanticValidator.optional_string_validator(123))
#> Traceback (most recent call last):
#> ...
#> PydanticCustomError: 'string_type'

email_validator

email_validator(
    value: str, *, error_types: dict | None = None
) -> EmailStr | None

Validate an email address format.

Parameters:
  • value (str) –

    Input string to be validated as an email.

  • error_types (dict | None, default: None ) –

    Optional dict mapping error codes to custom messages. If None, defaults to PYDANTIC_ERROR_TYPES.

Returns:
  • EmailStr | None –

    Validated EmailStr object or None if input is invalid.

Raises:
  • PydanticCustomError –

    If the email format is incorrect.

Example
print(PydanticValidator.email_validator("test@example.com"))
#> EmailStr('test@example.com')

print(PydanticValidator.email_validator("invalid-email"))
#> Traceback (most recent call last):
#> ...
#> PydanticCustomError: 'incorrect_email'

latitude_validator

latitude_validator(
    value: Decimal, *, error_types: dict | None = None
) -> Decimal

Validate that a value is a valid geographic latitude.

Parameters:
  • value (Decimal) –

    Input Decimal to be validated.

  • error_types (dict | None, default: None ) –

    Optional dict mapping error codes to custom messages. If None, defaults to PYDANTIC_ERROR_TYPES.

Returns:
  • Decimal –

    The validated Decimal value.

Raises:
  • PydanticCustomError –

    If the value is not in the range [-90.0, 90.0].

Example
print(PydanticValidator.latitude_validator(Decimal("45.0")))
#> Decimal('45.0')

print(PydanticValidator.latitude_validator(Decimal("100.0")))
#> Traceback (most recent call last):
#> ...
#> PydanticCustomError: 'incorrect_latitude'

longitude_validator

longitude_validator(
    value: Decimal, *, error_types: dict | None = None
) -> Decimal

Validate that a value is a valid geographic longitude.

Parameters:
  • value (Decimal) –

    Input Decimal to be validated.

  • error_types (dict | None, default: None ) –

    Optional dict mapping error codes to custom messages. If None, defaults to PYDANTIC_ERROR_TYPES.

Returns:
  • Decimal –

    The validated Decimal value.

Raises:
  • PydanticCustomError –

    If the value is not in the range [-180.0, 180.0].

Example
print(PydanticValidator.longitude_validator(Decimal("90.0")))
#> Decimal('90.0')

print(PydanticValidator.longitude_validator(Decimal("200.0")))
#> Traceback (most recent call last):
#> ...
#> PydanticCustomError: 'incorrect_longitude'

validate_required_field

validate_required_field(
    value: Any | None, *, error_types: dict | None = None
) -> Any

Validate that a field is not missing in update models.

Parameters:
  • value (Any | None) –

    Input value to be validated.

  • error_types (dict | None, default: None ) –

    Optional dict mapping error codes to custom messages. If None, defaults to PYDANTIC_ERROR_TYPES.

Returns:
  • Any –

    The original value if it's valid.

Raises:
  • PydanticCustomError –

    If the value is None or empty string.

Example
print(PydanticValidator.validate_required_field("test"))
#> "test"

print(PydanticValidator.validate_required_field(None))
#> Traceback (most recent call last):
#> ...
#> PydanticCustomError: 'missing'