Skip to main content

UserAttributeSimilarityValidator

Validate that the password is sufficiently different from the user's attributes.

Attributes

AttributeTypeDescription
DEFAULT_USER_ATTRIBUTEStuple = ("username", "first_name", "last_name", "email")A tuple of field names on the user model to be checked for similarity against the password if no specific attributes are provided during initialization.

Constructor

Signature

def UserAttributeSimilarityValidator(
user_attributes: tuple = ('username', 'first_name', 'last_name', 'email'),
max_similarity: float = 0.7
)

Parameters

NameTypeDescription
user_attributestuple = ('username', 'first_name', 'last_name', 'email')A sequence of attribute names on the user object to compare against the password.
max_similarityfloat = 0.7The maximum allowed similarity ratio (0.1 to 1.0) between the password and user attributes.

Signature

def UserAttributeSimilarityValidator(
user_attributes: tuple = ('username', 'first_name', 'last_name', 'email'),
max_similarity: float = 0.7
) - > null

Parameters

NameTypeDescription
user_attributestuple = ('username', 'first_name', 'last_name', 'email')An iterable of attribute names on the user object to compare against the password.
max_similarityfloat = 0.7The maximum allowed similarity ratio (0.1 to 1.0) between the password and an attribute before a ValidationError is raised.

Methods


validate()

@classmethod
def validate(
password: string,
user: object = null
) - > null

Validate that the password is sufficiently different from the user's attributes.

Parameters

NameTypeDescription
passwordstringThe raw password string to be evaluated for similarity.
userobject = nullThe user instance whose attributes will be compared against the password; if None, validation is skipped.

Returns

TypeDescription
nullNone if validation passes.

get_error_message()

@classmethod
def get_error_message() - > string

Returns the localized error message string used when a password is found to be too similar to a user attribute.

Returns

TypeDescription
stringThe translated error message template containing a placeholder for the attribute name.

get_help_text()

@classmethod
def get_help_text() - > string

Returns the localized help text explaining the password similarity requirement to the user.

Returns

TypeDescription
stringA user-friendly string describing the password restriction.