Condition

Conditions are used to validate the input data based on rules between fields. They ensure that the relationships between multiple fields satisfy specific criteria before further processing.

Overview

Conditions are added using the add_condition method. They evaluate the combined input data, ensuring that inter-field dependencies and relationships (such as equality, ordering, or presence) meet predefined rules.

Example

class TestInputFilter(InputFilter):
    def __init__(self):
        super().__init__()

        self.add(
            'username',
            filters=[StringTrimFilter()],
            validators=[IsStringValidator()]
        )

        self.add(
            'name',
            filters=[StringTrimFilter()],
            validators=[IsStringValidator()]
        )

        self.add_condition(
            OneOfCondition(['id', 'name'])
        )

Available Conditions

Base Condition

class flask_inputfilter.conditions.BaseCondition

Bases: object

Base class for defining conditions.

Each condition should implement the check method.

check(data)

Check if the condition is met based on the provided data.

Parameters:

data – Dictionary containing the data to validate against.

Returns:

True if the condition is satisfied, False otherwise.

Raises:

NotImplementedError – If the method is not implemented by subclasses.

Detailed Description

class flask_inputfilter.conditions.ArrayLengthEqualCondition(first_array_field, second_array_field)

Bases: BaseCondition

Checks if two array fields have equal length.

Parameters:

  • first_array_field (str): The first field containing an array.

  • second_array_field (str): The second field containing an array.

Expected Behavior:

Validates that the length of the array from first_array_field is equal to the length of the array from second_array_field. If not, the condition fails.

Example Usage:

class ArrayLengthFilter(InputFilter):
    def __init__(self):
        super().__init__()

        self.add(
            'list1',
            validators=[IsArrayValidator()]
        )

        self.add(
            'list2',
            validators=[IsArrayValidator()]
        )

        self.add_condition(
            ArrayLengthEqualCondition(
                first_array_field='list1',
                second_array_field='list2'
            )
        )
Parameters:
  • first_array_field (str)

  • second_array_field (str)

first_array_field
second_array_field
check(data)

Check if the condition is met based on the provided data.

Parameters:

data (dict[str, Any]) – Dictionary containing the data to validate against.

Returns:

True if the condition is satisfied, False otherwise.

Raises:

NotImplementedError – If the method is not implemented by subclasses.

Return type:

bool

class flask_inputfilter.conditions.ArrayLongerThanCondition(longer_field, shorter_field)

Bases: BaseCondition

Checks if the array in one field is longer than the array in another field.

Parameters:

  • longer_field (str): The field expected to have a longer array.

  • shorter_field (str): The field expected to have a shorter array.

Expected Behavior:

Validates that the array in longer_field has more elements than the array in shorter_field.

Example Usage:

class ArrayComparisonFilter(InputFilter):
    def __init__(self):
        super().__init__()

        self.add(
            'list1',
            validators=[IsArrayValidator()]
        )

        self.add(
            'list2',
            validators=[IsArrayValidator()]
        )

        self.add_condition(
            ArrayLongerThanCondition(
                longer_field='list1',
                shorter_field='list2'
            )
        )
Parameters:
  • longer_field (str)

  • shorter_field (str)

longer_field
shorter_field
check(data)

Check if the condition is met based on the provided data.

Parameters:

data (dict[str, Any]) – Dictionary containing the data to validate against.

Returns:

True if the condition is satisfied, False otherwise.

Raises:

NotImplementedError – If the method is not implemented by subclasses.

Return type:

bool

class flask_inputfilter.conditions.CustomCondition(condition)

Bases: BaseCondition

Allows defining a custom condition using a user-provided callable.

Parameters:

  • condition (Callable[[dict[str, Any]], bool]): A function that takes the input data and returns a boolean indicating whether the condition is met.

Expected Behavior:

Executes the provided callable with the input data. The condition passes if the callable returns True, and fails otherwise.

Example Usage:

def my_custom_condition(data):
    return data.get('age', 0) >= 18

class CustomFilter(InputFilter):
    def __init__(self):
        super().__init__()

        self.add(
            'age',
            validators=[IsIntegerValidator()]
        )

        self.add_condition(
            CustomCondition(
                condition=my_custom_condition
            )
        )
Parameters:

condition (Callable[[dict[str, Any]], bool])

condition
check(data)

Check if the condition is met based on the provided data.

Parameters:

data (dict[str, Any]) – Dictionary containing the data to validate against.

Returns:

True if the condition is satisfied, False otherwise.

Raises:

NotImplementedError – If the method is not implemented by subclasses.

Return type:

bool

class flask_inputfilter.conditions.EqualCondition(first_field, second_field)

Bases: BaseCondition

Checks if two specified fields are equal.

Parameters:

  • first_field (str): The first field to compare.

  • second_field (str): The second field to compare.

Expected Behavior:

Validates that the values of first_field and second_field are equal. Fails if they differ.

Example Usage:

class EqualFieldsFilter(InputFilter):
    def __init__(self):
        super().__init__()

        self.add(
            'password'
        )

        self.add(
            'confirm_password'
        )

        self.add_condition(
            EqualCondition(
                first_field='password',
                second_field='confirm_password'
            )
        )
Parameters:
  • first_field (str)

  • second_field (str)

first_field
second_field
check(data)

Check if the condition is met based on the provided data.

Parameters:

data (dict[str, Any]) – Dictionary containing the data to validate against.

Returns:

True if the condition is satisfied, False otherwise.

Raises:

NotImplementedError – If the method is not implemented by subclasses.

Return type:

bool

class flask_inputfilter.conditions.ExactlyNOfCondition(fields, n)

Bases: BaseCondition

Checks that exactly n of the specified fields are present in the input data.

Parameters:

  • fields (list[str]): A list of fields to check.

  • n (int): The exact number of fields that must be present.

Expected Behavior:

Counts the number of specified fields present in the data and validates that the count equals n.

Example Usage:

class ExactFieldsFilter(InputFilter):
    def __init__(self):
        super().__init__()

        self.add(
            'field1'
        )

        self.add(
            'field2'
        )

        self.add(
            'field3'
        )

        self.add_condition(
            ExactlyNOfCondition(
                fields=['field1', 'field2', 'field3'],
                n=2
            )
        )
Parameters:
  • fields (list[str])

  • n (int)

fields
n
check(data)

Check if the condition is met based on the provided data.

Parameters:

data (dict[str, Any]) – Dictionary containing the data to validate against.

Returns:

True if the condition is satisfied, False otherwise.

Raises:

NotImplementedError – If the method is not implemented by subclasses.

Return type:

bool

class flask_inputfilter.conditions.ExactlyNOfMatchesCondition(fields, n, value)

Bases: BaseCondition

Checks that exactly n of the specified fields match a given value.

Parameters:

  • fields (list[str]): A list of fields to check.

  • n (int): The exact number of fields that must match the value.

  • value (Any): The value to match against.

Expected Behavior:

Validates that exactly n fields among the specified ones have the given value.

Example Usage:

class MatchFieldsFilter(InputFilter):
    def __init__(self):
        super().__init__()

        self.add(
            'field1'
        )

        self.add(
            'field2'
        )

        self.add_condition(
            ExactlyNOfMatchesCondition(
                fields=['field1', 'field2'],
                n=1,
                value='expected_value'
            )
        )
Parameters:
  • fields (list[str])

  • n (int)

  • value (Any)

fields
n
value
check(data)

Check if the condition is met based on the provided data.

Parameters:

data (dict[str, Any]) – Dictionary containing the data to validate against.

Returns:

True if the condition is satisfied, False otherwise.

Raises:

NotImplementedError – If the method is not implemented by subclasses.

Return type:

bool

class flask_inputfilter.conditions.ExactlyOneOfCondition(fields)

Bases: BaseCondition

Ensures that exactly one of the specified fields is present.

Parameters:

  • fields (list[str]): A list of fields to check.

Expected Behavior:

Validates that only one field among the specified fields exists in the input data.

Example Usage:

class OneFieldFilter(InputFilter):
    def __init__(self):
        super().__init__()

        self.add(
            'email'
        )

        self.add(
            'phone'
        )

        self.add_condition(ExactlyOneOfCondition(['email', 'phone']))
Parameters:

fields (list[str])

fields
check(data)

Check if the condition is met based on the provided data.

Parameters:

data (dict[str, Any]) – Dictionary containing the data to validate against.

Returns:

True if the condition is satisfied, False otherwise.

Raises:

NotImplementedError – If the method is not implemented by subclasses.

Return type:

bool

class flask_inputfilter.conditions.ExactlyOneOfMatchesCondition(fields, value)

Bases: BaseCondition

Ensures that exactly one of the specified fields matches a given value.

Parameters:

  • fields (list[str]): A list of fields to check.

  • value (Any): The value to match against.

Expected Behavior:

Validates that exactly one of the specified fields has the given value.

Example Usage:

class OneMatchFilter(InputFilter):
    def __init__(self):
        super().__init__()

        self.add(
            'option1'
        )

        self.add(
            'option2'
        )

        self.add_condition(
            ExactlyOneOfMatchesCondition(
                fields=['option1', 'option2'],
                value='yes'
            )
        )
Parameters:
  • fields (list[str])

  • value (Any)

fields
value
check(data)

Check if the condition is met based on the provided data.

Parameters:

data (dict[str, Any]) – Dictionary containing the data to validate against.

Returns:

True if the condition is satisfied, False otherwise.

Raises:

NotImplementedError – If the method is not implemented by subclasses.

Return type:

bool

class flask_inputfilter.conditions.IntegerBiggerThanCondition(bigger_field, smaller_field)

Bases: BaseCondition

Checks if the integer value in one field is greater than that in another field.

Parameters:

  • bigger_field (str): The field expected to have a larger integer.

  • smaller_field (str): The field expected to have a smaller integer.

Expected Behavior:

Validates that the integer value from bigger_field is greater than the value from smaller_field.

Example Usage:

class NumberComparisonFilter(InputFilter):
    def __init__(self):
        super().__init__()

        self.add(
            'field_should_be_bigger',
            validators=[IsIntegerValidator()]
        )

        self.add(
            'field_should_be_smaller',
            validators=[IsIntegerValidator()]
        )

        self.add_condition(
            IntegerBiggerThanCondition(
                bigger_field='field_should_be_bigger',
                smaller_field='field_should_be_smaller'
            )
        )
Parameters:
  • bigger_field (str)

  • smaller_field (str)

bigger_field
smaller_field
check(data)

Check if the condition is met based on the provided data.

Parameters:

data (dict[str, int]) – Dictionary containing the data to validate against.

Returns:

True if the condition is satisfied, False otherwise.

Raises:

NotImplementedError – If the method is not implemented by subclasses.

Return type:

bool

class flask_inputfilter.conditions.NOfCondition(fields, n)

Bases: BaseCondition

Checks that at least n of the specified fields are present in the input data.

Parameters:

  • fields (list[str]): A list of fields to check.

  • n (int): The minimum number of fields that must be present.

Expected Behavior:

Validates that the count of the specified fields present is greater than or equal to n.

Example Usage:

class MinimumFieldsFilter(InputFilter):
    def __init__(self):
        super().__init__()

        self.add(
            'field1'
        )

        self.add(
            'field2'
        )

        self.add(
            'field3'
        )

        self.add_condition(
            NOfCondition(
                fields=['field1', 'field2', 'field3'],
                n=2
            )
        )
Parameters:
  • fields (list[str])

  • n (int)

fields
n
check(data)

Check if the condition is met based on the provided data.

Parameters:

data (Any) – Dictionary containing the data to validate against.

Returns:

True if the condition is satisfied, False otherwise.

Raises:

NotImplementedError – If the method is not implemented by subclasses.

Return type:

bool

class flask_inputfilter.conditions.NOfMatchesCondition(fields, n, value)

Bases: BaseCondition

Checks that at least n of the specified fields match a given value.

Parameters:

  • fields (list[str]): A list of fields to check.

  • n (int): The minimum number of fields that must match the value.

  • value (Any): The value to match against.

Expected Behavior:

Validates that the count of fields matching the given value is greater than or equal to n.

Example Usage:

class MinimumMatchFilter(InputFilter):
    def __init__(self):
        super().__init__()

        self.add(
            'field1'
        )

        self.add(
            'field2'
        )

        self.add_condition(
            NOfMatchesCondition(
                fields=['field1', 'field2'],
                n=1,
                value='value'
            )
        )
Parameters:
  • fields (list[str])

  • n (int)

  • value (Any)

fields
n
value
check(data)

Check if the condition is met based on the provided data.

Parameters:

data (dict[str, Any]) – Dictionary containing the data to validate against.

Returns:

True if the condition is satisfied, False otherwise.

Raises:

NotImplementedError – If the method is not implemented by subclasses.

Return type:

bool

class flask_inputfilter.conditions.NotEqualCondition(first_field, second_field)

Bases: BaseCondition

Checks if two specified fields are not equal.

Parameters:

  • first_field (str): The first field to compare.

  • second_field (str): The second field to compare.

Expected Behavior:

Validates that the values of first_field and second_field are not equal.

Example Usage:

class DifferenceFilter(InputFilter):
    def __init__(self):
        super().__init__()

        self.add(
            'field1'
        )

        self.add(
            'field2'
        )

        self.add_condition(NotEqualCondition('field1', 'field2'))
Parameters:
  • first_field (str)

  • second_field (str)

first_field
second_field
check(data)

Check if the condition is met based on the provided data.

Parameters:

data (dict[str, Any]) – Dictionary containing the data to validate against.

Returns:

True if the condition is satisfied, False otherwise.

Raises:

NotImplementedError – If the method is not implemented by subclasses.

Return type:

bool

class flask_inputfilter.conditions.OneOfCondition(fields)

Bases: BaseCondition

Ensures that at least one of the specified fields is present in the input data.

Parameters:

  • fields (list[str]): A list of fields to check.

Expected Behavior:

Validates that at least one field from the specified list is present. Fails if none are present.

Example Usage:

class OneFieldRequiredFilter(InputFilter):
    def __init__(self):
        super().__init__()

        self.add(
            'email'
        )

        self.add(
            'phone'
        )

        self.add_condition(
            OneOfCondition(
                fields=['email', 'phone']
            )
        )
Parameters:

fields (list[str])

fields
check(data)

Check if the condition is met based on the provided data.

Parameters:

data (dict[str, Any]) – Dictionary containing the data to validate against.

Returns:

True if the condition is satisfied, False otherwise.

Raises:

NotImplementedError – If the method is not implemented by subclasses.

Return type:

bool

class flask_inputfilter.conditions.OneOfMatchesCondition(fields, value)

Bases: BaseCondition

Ensures that at least one of the specified fields matches a given value.

Parameters:

  • fields (list[str]): A list of fields to check.

  • value (Any): The value to match against.

Expected Behavior:

Validates that at least one field from the specified list has the given value.

Example Usage:

class OneMatchRequiredFilter(InputFilter):
    def __init__(self):
        super().__init__()

        self.add(
            'option1'
        )

        self.add(
            'option2'
        )

        self.add_condition(
            OneOfMatchesCondition(
                fields=['option1', 'option2'],
                value='yes'
            )
        )
Parameters:
  • fields (list[str])

  • value (Any)

fields
value
check(data)

Check if the condition is met based on the provided data.

Parameters:

data (dict[str, Any]) – Dictionary containing the data to validate against.

Returns:

True if the condition is satisfied, False otherwise.

Raises:

NotImplementedError – If the method is not implemented by subclasses.

Return type:

bool

class flask_inputfilter.conditions.RequiredIfCondition(condition_field, value, required_field)

Bases: BaseCondition

Ensures that a field is required if another field has a specific value.

Parameters:

  • condition_field (str): The field whose value is checked.

  • value (Optional[Union[Any, list[Any]]]): The value(s) that trigger the requirement.

  • required_field (str): The field that becomes required if the condition is met.

Expected Behavior:

If the value of condition_field matches the specified value (or is in the specified list), then required_field must be present. Otherwise, the condition passes.

Example Usage:

class ConditionalRequiredFilter(InputFilter):
    def __init__(self):
        super().__init__()

        self.add(
            'status'
        )

        self.add(
            'activation_date'
        )

        self.add_condition(
            RequiredIfCondition(
                condition_field='status',
                value='active',
                required_field='activation_date'
            )
        )
Parameters:
  • condition_field (str)

  • value (Optional[Union[Any, list[Any]]])

  • required_field (str)

condition_field
required_field
value
check(data)

Check if the condition is met based on the provided data.

Parameters:

data (dict[str, Any]) – Dictionary containing the data to validate against.

Returns:

True if the condition is satisfied, False otherwise.

Raises:

NotImplementedError – If the method is not implemented by subclasses.

Return type:

bool

class flask_inputfilter.conditions.StringLongerThanCondition(longer_field, shorter_field)

Bases: BaseCondition

Checks if the length of the string in one field is longer than the string in another field.

Parameters:

  • longer_field (str): The field expected to have a longer string.

  • shorter_field (str): The field expected to have a shorter string.

Expected Behavior:

Validates that the string in longer_field has a greater length than the string in shorter_field.

Example Usage:

class StringLengthFilter(InputFilter):
    def __init__(self):
        super().__init__()

        self.add(
            'description'
        )

        self.add(
            'summary'
        )

        self.add_condition(
            StringLongerThanCondition(
                longer_field='description',
                shorter_field='summary'
            )
        )
Parameters:
  • longer_field (str)

  • shorter_field (str)

longer_field
shorter_field
check(value)

Check if the condition is met based on the provided data.

Parameters:
  • data – Dictionary containing the data to validate against.

  • value (dict[str, str])

Returns:

True if the condition is satisfied, False otherwise.

Raises:

NotImplementedError – If the method is not implemented by subclasses.

Return type:

bool

class flask_inputfilter.conditions.TemporalOrderCondition(smaller_date_field, larger_date_field)

Bases: BaseCondition

Checks if one date is before another, ensuring the correct temporal order. Supports datetime objects, date objects, and ISO 8601 formatted strings.

Parameters:

  • smaller_date_field (str): The field containing the earlier date.

  • larger_date_field (str): The field containing the later date.

Expected Behavior:

Validates that the date in smaller_date_field is earlier than the date in larger_date_field. Raises a ValidationError if the dates are not in the correct order.

Example Usage:

class DateOrderFilter(InputFilter):
    def __init__(self):
        super().__init__()

        self.add(
            'start_date'
        )

        self.add(
            'end_date'
        )

        self.add_condition(
            TemporalOrderCondition(
                smaller_date_field='start_date',
                larger_date_field='end_date'
            )
        )
Parameters:
  • smaller_date_field (str)

  • larger_date_field (str)

larger_date_field
smaller_date_field
check(data)

Check if the condition is met based on the provided data.

Parameters:

data (dict[str, Any]) – Dictionary containing the data to validate against.

Returns:

True if the condition is satisfied, False otherwise.

Raises:

NotImplementedError – If the method is not implemented by subclasses.

Return type:

bool