Validator
Validators are used to validate the data after filters have been applied.
They ensure that the input data meets the required conditions before further processing.
Overview
Validators can be added to specific fields using the decorator syntax or as global validators using global_validator().
The global validation will be executed before the specific field validation.
Example
class UpdatePointsInputFilter(InputFilter):
id: int = field(required=True)
points: int = field(
required=True,
validators=[RangeValidator(min_value=0, max_value=10)]
)
global_validator(IsIntegerValidator())
Available Validators
Special Validators
Following are the special validators that are used to combine or mutate the normal validators:
Base Validator
- class flask_inputfilter.validators.BaseValidator
Bases:
objectBaseValidator-Class.
Every validator should inherit from it.
- validate(value)
Validate the given value.
- Parameters:
value – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
Detailed Description
- class flask_inputfilter.validators.ArrayElementValidator(element_filter, error_message=None)
Bases:
BaseValidatorValidates each element within an array by applying an inner
InputFilterto every element. It ensures that all array items conform to the expected structure.Parameters:
elementFilter (InputFilter | BaseValidator | list[BaseValidator]): An instance used to validate each element.
error_message (Optional[str]): Custom error message for validation failure.
Expected Behavior:
Verifies that the input is a list and then applies the provided filter to each element. If any element fails validation, a
ValidationErroris raised.Example Usage:
This example demonstrates how to use the ArrayElementValidator with a custom InputFilter for validating elements in an array.
from my_filters import UserInputFilter class UsersInputFilter(InputFilter): users: list = field(validators=[ ArrayElementValidator(element_filter=UserInputFilter()) ])
Additionally, you can use a validator directly on your elements:
class TagInputFilter(InputFilter): tags: list[str] = field(validators=[ ArrayElementValidator(element_filter=IsStringValidator()) ])
- Parameters:
element_filter (Union[InputFilter, BaseValidator, list[BaseValidator]])
error_message (Optional[str])
- element_filter
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.ArrayLengthValidator(min_length=0, max_length=inf, error_message=None)
Bases:
BaseValidatorChecks whether the length of an array falls within a specified range.
Parameters:
min_length (int, default: 0): The minimum number of elements required.
max_length (int, default: infinity): The maximum number of allowed elements.
error_message (Optional[str]): Custom error message if the length check fails.
Expected Behavior:
Ensures that the input is a list and that its length is between the specified minimum and maximum. If not, a
ValidationErroris raised.Example Usage:
class ListInputFilter(InputFilter): items: list = field(validators=[ ArrayLengthValidator(min_length=1, max_length=5) ])
- Parameters:
min_length (int)
max_length (int)
error_message (Optional[str])
- error_message
- max_length
- min_length
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.CustomJsonValidator(required_fields=None, schema=None, error_message=None)
Bases:
BaseValidatorValidates that the provided value is valid JSON. It also checks for the presence of required fields and optionally verifies field types against a provided schema.
Parameters:
required_fields (list, default: []): Fields that must exist in the JSON.
schema (dict, default: {}): A dictionary specifying expected types for certain fields.
error_message (Optional[str]): Custom error message if validation fails.
Expected Behavior:
If the input is a string, it attempts to parse it as JSON. It then confirms that the result is a dictionary, contains all required fields, and that each field adheres to the defined type in the schema.
Example Usage:
class JsonInputFilter(InputFilter): data: dict = field(validators=[ CustomJsonValidator( required_fields=['id', 'name'], schema={'id': int, 'name': str} ) ])
- Parameters:
required_fields (Optional[list[str]])
schema (Optional[dict])
error_message (Optional[str])
- error_message
- required_fields
- schema
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.DateAfterValidator(reference_date, error_message=None)
Bases:
BaseValidatorEnsures that a given date is after a specified reference date. It supports both datetime objects and ISO 8601 formatted strings.
Parameters:
reference_date (Union[str, date, datetime]): The date that the input must be later than.
error_message (Optional[str]): Custom error message if the validation fails.
Expected Behavior:
Converts both the input and the reference date to datetime objects and verifies that the input date is later. If the check fails, a
ValidationErroris raised.Example Usage:
class EventInputFilter(InputFilter): event_date: str = field(validators=[ DateAfterValidator(reference_date="2023-01-01") ])
- Parameters:
reference_date (Union[str, date, datetime])
error_message (Optional[str])
- error_message
- reference_date
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.DateBeforeValidator(reference_date, error_message=None)
Bases:
BaseValidatorValidates that a given date is before a specified reference date. It supports datetime objects and ISO 8601 formatted strings.
Parameters:
reference_date (Union[str, date, datetime]): The date that the input must be earlier than.
error_message (Optional[str]): Custom error message if validation fails.
Expected Behavior:
Parses the input and reference date into datetime objects and checks that the input date is earlier. Raises a
ValidationErroron failure.Example Usage:
class RegistrationInputFilter(InputFilter): birth_date: str = field(validators=[ DateBeforeValidator(reference_date="2005-01-01") ])
- Parameters:
reference_date (Union[str, date, datetime])
error_message (Optional[str])
- error_message
- reference_date
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.DateRangeValidator(min_date=None, max_date=None, error_message=None)
Bases:
BaseValidatorChecks if a date falls within a specified range.
Parameters:
min_date (Optional[Union[str, date, datetime]]): The lower bound of the date range.
max_date (Optional[Union[str, date, datetime]]): The upper bound of the date range.
error_message (Optional[str]): Custom error message if the date is outside the range.
Expected Behavior:
Ensures the input date is not earlier than
min_dateand not later thanmax_date. AValidationErroris raised if the check fails.Example Usage:
class BookingInputFilter(InputFilter): booking_date: str = field(validators=[ DateRangeValidator( min_date="2023-01-01", max_date="2023-01-31" ) ])
- Parameters:
min_date (Optional[Union[str, date, datetime]])
max_date (Optional[Union[str, date, datetime]])
error_message (Optional[str])
- error_message
- max_date
- min_date
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.FloatPrecisionValidator(precision, scale, error_message=None)
Bases:
BaseValidatorEnsures that a numeric value conforms to a specific precision and scale. This is useful for validating monetary values or measurements.
Parameters:
precision (int): The maximum total number of digits allowed.
scale (int): The maximum number of digits allowed after the decimal point.
error_message (Optional[str]): Custom error message if validation fails.
Expected Behavior:
Converts the number to a string and checks the total number of digits and the digits after the decimal point. A
ValidationErroris raised if these limits are exceeded.Example Usage:
class PriceInputFilter(InputFilter): price: float = field(validators=[ FloatPrecisionValidator(precision=5, scale=2) ])
- Parameters:
precision (int)
scale (int)
error_message (Optional[str])
- error_message
- precision
- scale
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.InArrayValidator(haystack, strict=False, error_message=None)
Bases:
BaseValidatorChecks that the provided value exists within a predefined list of allowed values.
Parameters:
haystack (list[Any]): The list of allowed values.
strict (bool, default: False): When
True, also checks that the type of the value matches the types in the allowed list.error_message (Optional[str]): Custom error message if validation fails.
Expected Behavior:
Verifies that the value is present in the list. In strict mode, type compatibility is also enforced. If the check fails, a
ValidationErroris raised.Example Usage:
class StatusInputFilter(InputFilter): status: str = field(validators=[ InArrayValidator(haystack=["active", "inactive"]) ])
- Parameters:
haystack (list[Any])
strict (bool)
error_message (Optional[str])
- error_message
- haystack
- strict
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.InEnumValidator(enum_class, error_message=None)
Bases:
BaseValidatorVerifies that a given value is a valid member of a specified Enum class.
Parameters:
enumClass (Type[Enum]): The Enum to validate against.
error_message (Optional[str]): Custom error message if validation fails.
Expected Behavior:
Performs a case-insensitive comparison to ensure that the value matches one of the Enum’s member names. Raises a
ValidationErrorif the value is not a valid Enum member.Example Usage:
from enum import Enum class ColorEnum(Enum): RED = "red" GREEN = "green" BLUE = "blue" class ColorInputFilter(InputFilter): color: str = field( validators=[InEnumValidator(enum_class=ColorEnum)] )
- Parameters:
enum_class (Type[Enum])
error_message (Optional[str])
- enum_class
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsArrayValidator(error_message=None)
Bases:
BaseValidatorChecks if the provided value is an array (i.e. a list).
Parameters:
error_message (Optional[str]): Custom error message if validation fails.
Expected Behavior:
Raises a
ValidationErrorif the input is not a list.Example Usage:
class ListInputFilter(InputFilter): items: list = field(validators=[ IsArrayValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsBase64ImageCorrectSizeValidator(min_size=None, max_size=None, error_message=None)
Bases:
BaseValidatorChecks whether a Base64 encoded image has a size within the allowed range. By default, the image size must be between 1 and 4MB.
Parameters:
min_size (int, default: 1): The minimum allowed size in bytes.
max_size (int, default: 4 * 1024 * 1024): The maximum allowed size in bytes.
error_message (Optional[str]): Custom error message if validation fails.
Expected Behavior:
Decodes the Base64 string to determine the image size and raises a
ValidationErrorif the image size is outside the permitted range.Example Usage:
class ImageInputFilter(InputFilter): image = field(validators=[ IsBase64ImageCorrectSizeValidator( min_size=1024, max_size=2 * 1024 * 1024 ) ])
- Parameters:
min_size (Optional[int])
max_size (Optional[int])
error_message (Optional[str])
- error_message
- max_size
- min_size
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsBase64ImageValidator(error_message=None)
Bases:
BaseValidatorValidates that a Base64 encoded string represents a valid image by decoding it and verifying its integrity.
Parameters:
error_message (Optional[str]): Custom error message if validation fails.
Expected Behavior:
Attempts to decode the Base64 string and open the image using the PIL library. If the image is invalid or corrupted, a
ValidationErroris raised.Example Usage:
class AvatarInputFilter(InputFilter): avatar: str = field(validators=[ IsBase64ImageValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsBooleanValidator(error_message=None)
Bases:
BaseValidatorChecks if the provided value is a boolean.
Parameters:
error_message (Optional[str]): Custom error message if the input is not a bool.
Expected Behavior:
Raises a
ValidationErrorif the input value is not of type bool.Example Usage:
class FlagInputFilter(InputFilter): is_active: bool = field(validators=[ IsBooleanValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsDataclassValidator(dataclass_type, error_message=None)
Bases:
BaseValidatorValidates that the provided value conforms to a specific dataclass type.
Parameters:
dataclass_type (Type[dict]): The expected dataclass type.
error_message (Optional[str]): Custom error message if validation fails.
Expected Behavior:
Ensures the input is a dictionary and, that all expected keys are present. Raises a
ValidationErrorif the structure does not match. All fields in the dataclass are validated against their types, including nested dataclasses, lists, and dictionaries.Example Usage:
from dataclasses import dataclass @dataclass class User: id: int name: str class UserInputFilter(InputFilter): user: dict = field(validators=[ IsDataclassValidator(dataclass_type=User) ])
- Parameters:
dataclass_type (Type[T])
error_message (Optional[str])
- dataclass_type
- error_message
- validate(value)
Validate that value conforms to the dataclass type.
- Parameters:
value (Any)
- Return type:
None
- class flask_inputfilter.validators.IsDateTimeValidator(error_message=None)
Bases:
BaseValidatorChecks if the provided value is a datetime object.
Parameters:
error_message (Optional[str]): Custom error message if the value is not a datetime.
Expected Behavior:
Raises a
ValidationErrorif the input value is not of type datetime.Example Usage:
class TimestampInputFilter(InputFilter): timestamp: datetime = field(validators=[ IsDateTimeValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsDateValidator(error_message=None)
Bases:
BaseValidatorChecks if the provided value is a date object.
Parameters:
error_message (Optional[str]): Custom error message if the value is not a date.
Expected Behavior:
Raises a
ValidationErrorif the input value is not of type date.Example Usage:
class DateInputFilter(InputFilter): event_date: date = field(validators=[ IsDateValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsFloatValidator(error_message=None)
Bases:
BaseValidatorChecks if the provided value is a float.
Parameters:
error_message (Optional[str]): Custom error message if the value is not a float.
Expected Behavior:
Raises a
ValidationErrorif the input value is not of type float.Example Usage:
class MeasurementInputFilter(InputFilter): temperature: float = field(validators=[ IsFloatValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsFutureDateValidator(tz=None, error_message=None)
Bases:
BaseValidatorEnsures that a given date is in the future. Supports datetime objects and ISO 8601 formatted strings.
Parameters:
tz (Optional[timezone], default:
timezone.utc): Timezone to use for comparison.error_message (Optional[str]): Custom error message if the date is not in the future.
Expected Behavior:
Parses the input date and compares it to the current date and time. If the input date is not later than the current time, a
ValidationErroris raised.Example Usage:
class AppointmentInputFilter(InputFilter): appointment_date: str = field(validators=[ IsFutureDateValidator() ])
- Parameters:
tz (Optional[timezone])
error_message (Optional[str])
- error_message
- tz
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsHexadecimalValidator(error_message=None)
Bases:
BaseValidatorChecks if a given value is a valid hexadecimal string. The input must be a string that can be converted to an integer using base 16.
Parameters:
error_message (Optional[str]): Custom error message if the value is not a valid hexadecimal string.
Expected Behavior:
Verifies that the input is a string and attempts to convert it to an integer using base 16. Raises a
ValidationErrorif the conversion fails.Example Usage:
class HexInputFilter(InputFilter): hex_value: str = field(validators=[ IsHexadecimalValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsHorizontalImageValidator(error_message=None)
Bases:
BaseValidatorEnsures that the provided image is horizontally oriented. This validator accepts either a Base64 encoded string or an image object.
Parameters:
error_message (Optional[str]): Custom error message if the image is not horizontally oriented.
Expected Behavior:
Decodes the image (if provided as a string) and checks that its width is greater than or equal to its height. Raises a
ValidationErrorif the image does not meet the horizontal orientation criteria.Example Usage:
class HorizontalImageInputFilter(InputFilter): image = field(validators=[ IsHorizontalImageValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsHtmlValidator(error_message=None)
Bases:
BaseValidatorChecks if a value contains valid HTML. The validator looks for the presence of HTML tags in the input string.
Parameters:
error_message (Optional[str]): Custom error message if the value does not contain valid HTML.
Expected Behavior:
Verifies that the input is a string and checks for HTML tags using a regular expression. Raises a
ValidationErrorif no HTML tags are found.Example Usage:
class HtmlInputFilter(InputFilter): html_content: str = field(validators=[ IsHtmlValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsImageValidator(error_message=None)
Bases:
BaseValidatorValidates that the provided value is a valid image. Supports various input formats including file paths, base64 encoded strings, bytes, or PIL Image objects.
Parameters:
error_message (Optional[str]): Custom error message if validation fails.
Expected Behavior:
Attempts to validate the input as an image by: - If input is a PIL Image object, it’s considered valid - If input is a string, tries to open it as a file path or decode as base64 - If input is bytes, tries to open as image data - Raises a
ValidationErrorif the input cannot be processed as an imageExample Usage:
class ImageInputFilter(InputFilter): image = field(validators=[ IsImageValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsInstanceValidator(class_type, error_message=None)
Bases:
BaseValidatorValidates that the provided value is an instance of a specified class.
Parameters:
classType (Type[Any]): The class against which the value is validated.
error_message (Optional[str]): Custom error message if the validation fails.
Expected Behavior:
Raises a
ValidationErrorif the input is not an instance of the specified class.Example Usage:
class MyClass: pass class InstanceInputFilter(InputFilter): object = field(validators=[ IsInstanceValidator(class_type=MyClass) ])
- Parameters:
class_type (Type[Any])
error_message (Optional[str])
- class_type
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsIntegerValidator(error_message=None)
Bases:
BaseValidatorChecks whether the provided value is an integer.
Parameters:
error_message (Optional[str]): Custom error message if the value is not an integer.
Expected Behavior:
Raises a
ValidationErrorif the input value is not of type int.Example Usage:
class NumberInputFilter(InputFilter): number: int = field(validators=[ IsIntegerValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsJsonValidator(error_message=None)
Bases:
BaseValidatorValidates that the provided value is a valid JSON string.
Parameters:
error_message (Optional[str]): Custom error message if the input is not a valid JSON string.
Expected Behavior:
Attempts to parse the input using JSON decoding. Raises a
ValidationErrorif parsing fails.Example Usage:
class JsonInputFilter(InputFilter): json_data: str = field(validators=[ IsJsonValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsLowercaseValidator(error_message=None)
Bases:
BaseValidatorChecks if a value is entirely lowercase. The validator ensures that the input string has no uppercase characters.
Parameters:
error_message (Optional[str]): Custom error message if the value is not entirely lowercase.
Expected Behavior:
Confirms that the input is a string and verifies that all characters are lowercase using the string method
islower(). Raises aValidationErrorif the check fails.Example Usage:
class LowercaseInputFilter(InputFilter): username: str = field(validators=[ IsLowercaseValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsMacAddressValidator(error_message=None)
Bases:
BaseValidatorChecks if a value is a valid MAC address. It verifies common MAC address formats, such as colon-separated or hyphen-separated pairs of hexadecimal digits.
Parameters:
error_message (Optional[str]): Custom error message if the value is not a valid MAC address.
Expected Behavior:
Ensures the input is a string and matches a regular expression pattern for MAC addresses. Raises a
ValidationErrorif the value does not conform to the expected MAC address format.Example Usage:
class NetworkInputFilter(InputFilter): mac_address = field(validators=[ IsMacAddressValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsPastDateValidator(tz=None, error_message=None)
Bases:
BaseValidatorChecks whether a given date is in the past. Supports datetime objects, date objects, and ISO 8601 formatted strings.
Parameters:
tz (Optional[timezone], default:
timezone.utc): Timezone to use for comparison.error_message (Optional[str]): Custom error message if the date is not in the past.
Expected Behavior:
Parses the input date and verifies that it is earlier than the current date and time. Raises a
ValidationErrorif the input date is not in the past.Example Usage:
class HistoryInputFilter(InputFilter): past_date = field(validators=[ IsPastDateValidator() ])
- Parameters:
tz (Optional[timezone])
error_message (Optional[str])
- error_message
- tz
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsPortValidator(error_message=None)
Bases:
BaseValidatorChecks if a value is a valid network port. Valid port numbers range from 1 to 65535.
Parameters:
error_message (Optional[str]): Custom error message if the value is not a valid port number.
Expected Behavior:
Ensures that the input is an integer and that it lies within the valid range for port numbers. Raises a
ValidationErrorif the value is outside this range.Example Usage:
class PortInputFilter(InputFilter): port: int = field(validators=[ IsPortValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsRgbColorValidator(error_message=None)
Bases:
BaseValidatorChecks if a value is a valid RGB color string. The expected format is
rgb(r, g, b)where r, g, and b are integers between 0 and 255.Parameters:
error_message (Optional[str]): Custom error message if the value is not a valid RGB color.
Expected Behavior:
Verifies that the input is a string, matches the RGB color format using a regular expression, and that the extracted numeric values are within the range 0 to 255. Raises a
ValidationErrorif the check fails.Example Usage:
class ColorInputFilter(InputFilter): color = field(validators=[ IsRgbColorValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsStringValidator(error_message=None)
Bases:
BaseValidatorValidates that the provided value is a string.
Parameters:
error_message (Optional[str]): Custom error message if the value is not a string.
Expected Behavior:
Raises a
ValidationErrorif the input is not of type str.Example Usage:
class TextInputFilter(InputFilter): text: str = field(validators=[ IsStringValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsTypedDictValidator(typed_dict_type, error_message=None)
Bases:
BaseValidatorValidates that the provided value conforms to a specified TypedDict structure.
Parameters:
typed_dict_type (Type[TypedDict]): The TypedDict class that defines the expected structure.
error_message (Optional[str]): Custom error message if the validation fails.
Expected Behavior:
Ensures the input is a dictionary and, that all expected keys are present. Raises a
ValidationErrorif the structure does not match.Example Usage:
from typing import TypedDict class PersonDict(TypedDict): name: str age: int class PersonInputFilter(InputFilter): person = field(validators=[ IsTypedDictValidator(typed_dict_type=PersonDict) ])
- Parameters:
typed_dict_type (Type[TypedDict]) – The TypedDict class to validate against.
error_message (Optional[str]) – Custom error message to use if validation fails.
- error_message
- typed_dict_expected_keys
- typed_dict_name
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsUUIDValidator(error_message=None)
Bases:
BaseValidatorChecks if the provided value is a valid UUID string.
Parameters:
error_message (Optional[str]): Custom error message if the input is not a valid UUID.
Expected Behavior:
Verifies that the input is a string and attempts to parse it as a UUID. Raises a
ValidationErrorif parsing fails.Example Usage:
class UUIDInputFilter(InputFilter): uuid: str = field(validators=[ IsUUIDValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsUppercaseValidator(error_message=None)
Bases:
BaseValidatorChecks if a value is entirely uppercase. It verifies that the input string has no lowercase characters.
Parameters:
error_message (Optional[str]): Custom error message if the value is not entirely uppercase.
Expected Behavior:
Ensures that the input is a string and that all characters are uppercase using the string method
isupper(). Raises aValidationErrorif the check fails.Example Usage:
class UppercaseInputFilter(InputFilter): code = field(validators=[ IsUppercaseValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsUrlValidator(error_message=None)
Bases:
BaseValidatorChecks if a value is a valid URL. The validator uses URL parsing to ensure that the input string contains a valid scheme and network location.
Parameters:
error_message (Optional[str]): Custom error message if the value is not a valid URL.
Expected Behavior:
Verifies that the input is a string and uses URL parsing (via
urllib.parse.urlparse) to confirm that both the scheme and network location are present. Raises aValidationErrorif the URL is invalid.Example Usage:
class UrlInputFilter(InputFilter): website: str = field(validators=[ IsUrlValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsVerticalImageValidator(error_message=None)
Bases:
BaseValidatorValidates that the provided image is vertically oriented. Accepts either a Base64 encoded string or an image object.
Parameters:
error_message (Optional[str]): Custom error message if the image is not vertically oriented.
Expected Behavior:
Decodes the image (if provided as a string) and checks that its height is greater than or equal to its width. Raises a
ValidationErrorif the image is horizontally oriented.Example Usage:
class VerticalImageInputFilter(InputFilter): image = field(validators=[ IsVerticalImageValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsWeekdayValidator(error_message=None)
Bases:
BaseValidatorChecks whether a given date falls on a weekday (Monday to Friday). Supports datetime objects, date objects, and ISO 8601 formatted strings.
Parameters:
error_message (Optional[str]): Custom error message if the date is not a weekday.
Expected Behavior:
Parses the input date and verifies that it corresponds to a weekday. Raises a
ValidationErrorif the date falls on a weekend.Example Usage:
class WorkdayInputFilter(InputFilter): date = field(validators=[ IsWeekdayValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.IsWeekendValidator(error_message=None)
Bases:
BaseValidatorValidates that a given date falls on a weekend (Saturday or Sunday). Supports datetime objects, date objects, and ISO 8601 formatted strings.
Parameters:
error_message (Optional[str]): Custom error message if the date is not on a weekend.
Expected Behavior:
Parses the input date and confirms that it corresponds to a weekend day. Raises a
ValidationErrorif the date is on a weekday.Example Usage:
class WeekendInputFilter(InputFilter): date = field(validators=[ IsWeekendValidator() ])
- Parameters:
error_message (Optional[str])
- error_message
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.LengthValidator(min_length=None, max_length=None, error_message=None)
Bases:
BaseValidatorValidates the length of a string, ensuring it falls within a specified range.
Parameters:
min_length (Optional[int]): The minimum allowed length.
max_length (Optional[int]): The maximum allowed length.
error_message (Optional[str]): Custom error message if the validation fails.
Expected Behavior:
Checks the length of the input string and raises a
ValidationErrorif it is shorter thanmin_lengthor longer thanmax_length.Example Usage:
class TextLengthInputFilter(InputFilter): username: str = field(validators=[ LengthValidator(min_length=3, max_length=15) ])
- Parameters:
min_length (Optional[int])
max_length (Optional[int])
error_message (Optional[str])
- error_message
- max_length
- min_length
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.NotInArrayValidator(haystack, strict=False, error_message=None)
Bases:
BaseValidatorEnsures that the provided value is not present in a specified list of disallowed values.
Parameters:
haystack (list[Any]): A list of disallowed values.
strict (bool, default: False): If
True, the type of the value is also validated against the disallowed list.error_message (Optional[str]): Custom error message if the validation fails.
Expected Behavior:
Raises a
ValidationErrorif the value is found in the disallowed list, or if strict type checking is enabled and the value’s type does not match any allowed type.Example Usage:
class UsernameInputFilter(InputFilter): username: str = field(validators=[ NotInArrayValidator(haystack=["admin", "root"]) ])
- Parameters:
haystack (list[Any])
strict (bool)
error_message (Optional[str])
- error_message
- haystack
- strict
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.RangeValidator(min_value=None, max_value=None, error_message=None)
Bases:
BaseValidatorChecks whether a numeric value falls within a specified range.
Parameters:
min_value (Optional[float]): The minimum allowed value.
max_value (Optional[float]): The maximum allowed value.
error_message (Optional[str]): Custom error message if the validation fails.
Expected Behavior:
Verifies that the numeric input is not less than
min_valueand not greater thanmax_value. Raises aValidationErrorif the value is outside this range.Example Usage:
class ScoreInputFilter(InputFilter): score: float = field(validators=[ RangeValidator(min_value=0, max_value=100) ])
- Parameters:
min_value (Optional[Union[int, float]])
max_value (Optional[Union[int, float]])
error_message (Optional[str])
- error_message
- max_value
- min_value
- validate(value)
Validate the given value.
- Parameters:
value (Any) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None
- class flask_inputfilter.validators.RegexValidator(pattern, error_message=None)
Bases:
BaseValidatorValidates that the input string matches a specified regular expression pattern.
Parameters:
pattern (str | RegexEnum): The regular expression pattern or value of RegexEnum the input must match.
error_message (Optional[str]): Custom error message if the input does not match the pattern.
Expected Behavior:
Uses the Python
remodule to compare the input string against the provided pattern. Raises aValidationErrorif there is no match.Example Usage:
class EmailInputFilter(InputFilter): email: str = field(validators=[ RegexValidator(pattern=r'[a-cA-C]+') ])
- Parameters:
pattern (Union[str, RegexEnum])
error_message (Optional[str])
- error_message
- pattern
- validate(value)
Validate the given value.
- Parameters:
value (str) – The value to validate.
- Raises:
ValidationError – If the value is invalid.
NotImplementedError – If the method is not implemented by subclasses.
- Return type:
None