UserAttributeSimilarityValidator
Validate that the password is sufficiently different from the user's attributes.
Attributes
| Attribute | Type | Description |
|---|---|---|
| DEFAULT_USER_ATTRIBUTES | tuple = ("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
| Name | Type | Description |
|---|---|---|
| user_attributes | tuple = ('username', 'first_name', 'last_name', 'email') | A sequence of attribute names on the user object to compare against the password. |
| max_similarity | float = 0.7 | The 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
| Name | Type | Description |
|---|---|---|
| user_attributes | tuple = ('username', 'first_name', 'last_name', 'email') | An iterable of attribute names on the user object to compare against the password. |
| max_similarity | float = 0.7 | The 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
| Name | Type | Description |
|---|---|---|
| password | string | The raw password string to be evaluated for similarity. |
| user | object = null | The user instance whose attributes will be compared against the password; if None, validation is skipped. |
Returns
| Type | Description |
|---|---|
null | None 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
| Type | Description |
|---|---|
string | The 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
| Type | Description |
|---|---|
string | A user-friendly string describing the password restriction. |