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
| Attribute | Type | Description |
|---|---|---|
| message | string = 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. |
| code | string = max_length | The 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
| Name | Type | Description |
|---|---|---|
| limit_value | Any | The maximum allowed length for the value being validated. |
| message | str | An 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
| Name | Type | Description |
|---|---|---|
| a | int | The current length of the value being validated |
| b | int | The maximum allowed length limit to compare against |
Returns
| Type | Description |
|---|---|
boolean | True 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
| Name | Type | Description |
|---|---|---|
| x | Any | The input value, typically a string or collection, whose length needs to be measured |
Returns
| Type | Description |
|---|---|
int | The total number of characters or items contained in the input value |