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 into the add
method for a specific field or as a global validator for all fields in add_global_validator
.
The global validation will be executed before the specific field validation.
Example
class UpdatePointsInputFilter(InputFilter):
def __init__(self):
super().__init__()
self.add(
'id',
required=True
)
self.add(
'points',
required=True,
validators=[RangeValidator(min_value=0, max_value=10)]
)
self.add_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:
object
BaseValidator-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:
BaseValidator
Validates each element within an array by applying an inner
InputFilter
to 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
ValidationError
is 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): def __init__(self): super().__init__() self.add('users', validators=[ ArrayElementValidator(element_filter=UserInputFilter()) ])
Additionally, you can use a validator directly on your elements:
class TagInputFilter(InputFilter): def __init__(self): super().__init__() self.add('tags', 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:
BaseValidator
Checks 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
ValidationError
is raised.Example Usage:
class ListInputFilter(InputFilter): def __init__(self): super().__init__() self.add('items', 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:
BaseValidator
Validates 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): def __init__(self): super().__init__() self.add('data', 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:
BaseValidator
Ensures 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
ValidationError
is raised.Example Usage:
class EventInputFilter(InputFilter): def __init__(self): super().__init__() self.add('event_date', 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:
BaseValidator
Validates 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
ValidationError
on failure.Example Usage:
class RegistrationInputFilter(InputFilter): def __init__(self): super().__init__() self.add('birth_date', 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:
BaseValidator
Checks 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_date
and not later thanmax_date
. AValidationError
is raised if the check fails.Example Usage:
class BookingInputFilter(InputFilter): def __init__(self): super().__init__() self.add('booking_date', 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:
BaseValidator
Ensures 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
ValidationError
is raised if these limits are exceeded.Example Usage:
class PriceInputFilter(InputFilter): def __init__(self): super().__init__() self.add('price', 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:
BaseValidator
Checks 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
ValidationError
is raised.Example Usage:
class StatusInputFilter(InputFilter): def __init__(self): super().__init__() self.add('status', 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:
BaseValidator
Verifies 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
ValidationError
if 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): def __init__(self): super().__init__() self.add('color', 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:
BaseValidator
Checks 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
ValidationError
if the input is not a list.Example Usage:
class ListInputFilter(InputFilter): def __init__(self): super().__init__() self.add('items', 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:
BaseValidator
Checks 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
ValidationError
if the image size is outside the permitted range.Example Usage:
class ImageInputFilter(InputFilter): def __init__(self): super().__init__() self.add('image', 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:
BaseValidator
Validates 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
ValidationError
is raised.Example Usage:
class AvatarInputFilter(InputFilter): def __init__(self): super().__init__() self.add('avatar', 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:
BaseValidator
Checks 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
ValidationError
if the input value is not of type bool.Example Usage:
class FlagInputFilter(InputFilter): def __init__(self): super().__init__() self.add('is_active', 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:
BaseValidator
Validates 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
ValidationError
if 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): def __init__(self): super().__init__() self.add('user', 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:
BaseValidator
Checks 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
ValidationError
if the input value is not of type datetime.Example Usage:
class TimestampInputFilter(InputFilter): def __init__(self): super().__init__() self.add('timestamp', 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:
BaseValidator
Checks 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
ValidationError
if the input value is not of type date.Example Usage:
class DateInputFilter(InputFilter): def __init__(self): super().__init__() self.add('event_date', 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:
BaseValidator
Checks 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
ValidationError
if the input value is not of type float.Example Usage:
class MeasurementInputFilter(InputFilter): def __init__(self): super().__init__() self.add('temperature', 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:
BaseValidator
Ensures 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
ValidationError
is raised.Example Usage:
class AppointmentInputFilter(InputFilter): def __init__(self): super().__init__() self.add('appointment_date', 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:
BaseValidator
Checks 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
ValidationError
if the conversion fails.Example Usage:
class HexInputFilter(InputFilter): def __init__(self): super().__init__() self.add('hex_value', 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:
BaseValidator
Ensures 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
ValidationError
if the image does not meet the horizontal orientation criteria.Example Usage:
class HorizontalImageInputFilter(InputFilter): def __init__(self): super().__init__() self.add('image', validators=[ IsHorizontalImageValidator() ])
- error_message
- 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.
- class flask_inputfilter.validators.IsHtmlValidator(error_message=None)
Bases:
BaseValidator
Checks 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
ValidationError
if no HTML tags are found.Example Usage:
class HtmlInputFilter(InputFilter): def __init__(self): super().__init__() self.add('html_content', 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.IsInstanceValidator(class_type, error_message=None)
Bases:
BaseValidator
Validates 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
ValidationError
if the input is not an instance of the specified class.Example Usage:
class MyClass: pass class InstanceInputFilter(InputFilter): def __init__(self): super().__init__() self.add('object', validators=[ IsInstanceValidator(classType=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:
BaseValidator
Checks 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
ValidationError
if the input value is not of type int.Example Usage:
class NumberInputFilter(InputFilter): def __init__(self): super().__init__() self.add('number', 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:
BaseValidator
Validates 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
ValidationError
if parsing fails.Example Usage:
class JsonInputFilter(InputFilter): def __init__(self): super().__init__() self.add('json_data', 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:
BaseValidator
Checks 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 aValidationError
if the check fails.Example Usage:
class LowercaseInputFilter(InputFilter): def __init__(self): super().__init__() self.add('username', 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:
BaseValidator
Checks 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
ValidationError
if the value does not conform to the expected MAC address format.Example Usage:
class NetworkInputFilter(InputFilter): def __init__(self): super().__init__() self.add('mac_address', 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:
BaseValidator
Checks 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
ValidationError
if the input date is not in the past.Example Usage:
class HistoryInputFilter(InputFilter): def __init__(self): super().__init__() self.add('past_date', 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:
BaseValidator
Checks 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
ValidationError
if the value is outside this range.Example Usage:
class PortInputFilter(InputFilter): def __init__(self): super().__init__() self.add('port', 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:
BaseValidator
Checks 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
ValidationError
if the check fails.Example Usage:
class ColorInputFilter(InputFilter): def __init__(self): super().__init__() self.add('color', 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:
BaseValidator
Validates 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
ValidationError
if the input is not of type str.Example Usage:
class TextInputFilter(InputFilter): def __init__(self): super().__init__() self.add('text', 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:
BaseValidator
Validates 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
ValidationError
if the structure does not match.Example Usage:
from typing import TypedDict class PersonDict(TypedDict): name: str age: int class PersonInputFilter(InputFilter): def __init__(self): super().__init__() self.add('person', 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:
BaseValidator
Checks 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
ValidationError
if parsing fails.Example Usage:
class UUIDInputFilter(InputFilter): def __init__(self): super().__init__() self.add('uuid', 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:
BaseValidator
Checks 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 aValidationError
if the check fails.Example Usage:
class UppercaseInputFilter(InputFilter): def __init__(self): super().__init__() self.add('code', 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:
BaseValidator
Checks 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 aValidationError
if the URL is invalid.Example Usage:
class UrlInputFilter(InputFilter): def __init__(self): super().__init__() self.add('website', 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:
BaseValidator
Validates 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
ValidationError
if the image is horizontally oriented.Example Usage:
class VerticalImageInputFilter(InputFilter): def __init__(self): super().__init__() self.add('image', 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:
BaseValidator
Checks 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
ValidationError
if the date falls on a weekend.Example Usage:
class WorkdayInputFilter(InputFilter): def __init__(self): super().__init__() self.add('date', 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:
BaseValidator
Validates 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
ValidationError
if the date is on a weekday.Example Usage:
class WeekendInputFilter(InputFilter): def __init__(self): super().__init__() self.add('date', 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:
BaseValidator
Validates 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
ValidationError
if it is shorter thanmin_length
or longer thanmax_length
.Example Usage:
class TextLengthInputFilter(InputFilter): def __init__(self): super().__init__() self.add('username', 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:
BaseValidator
Ensures 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
ValidationError
if 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): def __init__(self): super().__init__() self.add('username', 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:
BaseValidator
Checks 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_value
and not greater thanmax_value
. Raises aValidationError
if the value is outside this range.Example Usage:
class ScoreInputFilter(InputFilter): def __init__(self): super().__init__() self.add('score', 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:
BaseValidator
Validates that the input string matches a specified regular expression pattern.
Parameters:
pattern (str): The regular expression pattern the input must match.
error_message (Optional[str]): Custom error message if the input does not match the pattern.
Expected Behavior:
Uses the Python
re
module to compare the input string against the provided pattern. Raises aValidationError
if there is no match.Example Usage:
class EmailInputFilter(InputFilter): def __init__(self): super().__init__() self.add('email', validators=[ RegexValidator(pattern=r'[a-cA-C]+') ])
- Parameters:
pattern (str)
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