Skip to main content

MaxLengthValidator

This class provides validation logic to ensure that a given value does not exceed a specified maximum length. It calculates the length of the input and compares it against a limit, raising a customizable error message if the validation fails.

Attributes

AttributeTypeDescription
messagestring = Ensure this value has at most %(limit_value)d character (it has %(show_value)d).The error message template used when the validated value exceeds the maximum length, supporting pluralization based on the limit_value.
codestring = max_lengthThe specific error code identifier used to categorize validation failures for the maximum length constraint.

Constructor

Signature

def MaxLengthValidator(
limit_value: Any,
message: str
) - > null

Parameters

NameTypeDescription
limit_valueAnyThe maximum allowed length for the value being validated.
messagestrAn optional custom error message to be used if validation fails.

Methods


compare()

@classmethod
def compare(
a: int,
b: int
) - > boolean

Determines if the provided value exceeds the maximum allowed length limit.

Parameters

NameTypeDescription
aintThe current length of the value being validated
bintThe maximum allowed length limit to compare against

Returns

TypeDescription
booleanTrue if the length of the value is greater than the limit, False otherwise

clean()

@classmethod
def clean(
x: Any
) - > int

Calculates the length of the input value to prepare it for comparison against the limit.

Parameters

NameTypeDescription
xAnyThe input value, typically a string or collection, whose length needs to be measured

Returns

TypeDescription
intThe total number of characters or items contained in the input value