Skip to main content

BaseValidator

This class serves as a base for creating validation logic that compares a given value against a predefined limit. It provides a standard interface for cleaning input data, performing comparison checks, and raising validation errors with customizable messages. Subclasses can override the comparison and cleaning methods to implement specific validation rules.

Attributes

AttributeTypeDescription
messagestring = Ensure this value is %(limit_value)s (it is %(show_value)s).The error message template used when validation fails, which can include placeholders for limit_value and show_value.
codestring = limit_valueThe specific error code identifier associated with the ValidationError raised when the comparison fails.

Constructor

Signature

def BaseValidator(
limit_value: any,
message: string = None
) - > null

Parameters

NameTypeDescription
limit_valueanyThe threshold or value to validate against.
messagestring = NoneAn optional custom error message to override the default.

Methods


compare()

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

Performs the core comparison logic between the cleaned input value and the defined limit value.

Parameters

NameTypeDescription
aanyThe cleaned input value being validated.
banyThe limit value or threshold to compare against.

Returns

TypeDescription
booleanA boolean indicating whether the validation condition is triggered (defaults to an identity check).

clean()

@classmethod
def clean(
x: any
) - > any

Normalizes or pre-processes the input value before it is passed to the comparison method.

Parameters

NameTypeDescription
xanyThe raw value that requires cleaning or type conversion.

Returns

TypeDescription
anyThe processed version of the input value, ready for validation.