Skip to content

validators

Between

Validator to ensure that a value falls within a particular range

Type Constraints

  • Supports Comparison (<, >, ==)
Source code in arc/types/validators/size.py
class Between:
    """Validator to ensure that a value falls within a particular range

    ## Type Constraints
    - Supports Comparison (<, >, ==)
    """

    def __init__(self, lower: SupportsComparison, upper: SupportsComparison):
        self.lower = lower
        self.upper = upper

    def __call__(self, value: SupportsComparison):
        if value <= self.lower or value >= self.upper:
            raise errors.ValidationError(
                f"must be between {self.lower} and {self.upper}"
            )

        return value

GreaterThan

Validator to limit the maximum size of a value

Type Constraints

  • Supports Comparison (<, >, ==)
Source code in arc/types/validators/size.py
class GreaterThan:
    """Validator to limit the maximum size of a value

    ## Type Constraints
    - Supports Comparison (<, >, ==)
    """

    def __init__(self, smallest: SupportsComparison):
        self.smallest = smallest

    def __call__(self, value: SupportsComparison):
        if value <= self.smallest:
            raise errors.ValidationError(f"must be greater than {self.smallest}")

        return value

Len

Validator for the length of a value.

  • Len(4) - Value must be length 4
  • Len(1, 4) - Value must be from length 1 to 4

Type Constraints

  • Supports len()
Source code in arc/types/validators/size.py
class Len:
    """Validator for the length of a value.

    - `Len(4)` - Value must be length 4
    - `Len(1, 4)` - Value must be from length 1 to 4

    ## Type Constraints
    - Supports `len()`
    """

    def __init__(self, min: int, max: int | None = None):
        self.min = min
        self.max = max

    def __call__(self, value: SupportsLen):
        length = len(value)

        if self.max:
            if length < self.min:
                raise errors.ValidationError(
                    f"must have a length between {self.min} and {self.max}"
                )
            if length > self.max:
                raise errors.ValidationError(
                    f"must have a length between {self.min} and {self.max}"
                )
        else:
            if length != self.min:
                raise errors.ValidationError(f"must have a length equal to {self.min}")

        return value

LessThan

Validator to limit the minimum size of a value

Type Constraints

  • Supports Comparison (<, >, ==)
Source code in arc/types/validators/size.py
class LessThan:
    """Validator to limit the minimum size of a value

    ## Type Constraints
    - Supports Comparison (<, >, ==)
    """

    def __init__(self, largest: SupportsComparison):
        self.largest = largest

    def __call__(self, value: SupportsComparison):
        if value >= self.largest:
            raise errors.ValidationError(f"must be less than {self.largest}")

        return value

Matches

Validator to match a regular expression.

Type Constraints

  • Matches against str(value), so the type must have a sensible string representation
Source code in arc/types/validators/misc.py
class Matches:
    """Validator to match a regular expression.

    ## Type Constraints
    - Matches against `str(value)`, so the type must have a sensible string representation
    """

    def __init__(self, pattern: str | re.Pattern[str], flags: int = 0):
        self.pattern = pattern
        self.flags = flags

    def __call__(self, value: t.Any):
        if not re.match(self.pattern, str(value), self.flags):
            raise errors.ValidationError(
                f"does not match expected format: {self.pattern}"
            )

        return value