API Reference¶
-
class
password_validator.
PasswordValidator
[source]¶ Class to generate schema of password definitions
Example
>>> schema = PasswordValidator() >>> schema.has().letters().has().digits().no().spaces() <src.password_validator.password_validator.PasswordValidator object at ...> >>> schema.validate('testPassword123') True
- Returns
Schema object
- Return type
-
validate
(pwd)[source]¶ Validates pwd against the schema and returns the result
Example
>>> PasswordValidator().letters().validate('123') False >>> PasswordValidator().letters().validate('abc') True
- Parameters
pwd (str) – Password to validate against the schema
- Returns
Result of the validation
- Return type
boolean
-
has
(regexp=None)[source]¶ - Inverts the effect of
no()
and applies aregex
if provided to the schema. Works with:
letters()
,digits()
,uppercase()
,lowercase()
,symbols()
andspaces()
.
Example
>>> PasswordValidator().no().letters().has().digits().validate('123') True >>> PasswordValidator().has(r'[a-z]+').validate('test') True
- Parameters
regexp (str, optional) – The regular expression or string to mandate on the password
- Returns
Updated schema object
- Return type
- Inverts the effect of
-
no
(regexp=None)[source]¶ - Inverts the effect of all the next rules unless countered by a
has()
and applies a negative check of the regular expression provided. Works with:letters()
,digits()
,uppercase()
,lowercase()
,symbols()
andspaces()
.
Example
>>> PasswordValidator().no().letters().digits().validate('123') False >>> PasswordValidator().no().letters().has().digits().validate('123') True >>> PasswordValidator().no(r'[a-z]+').validate('test') False
- Parameters
regexp (str, optional) – The regex or str the password should not match to
- Returns
Updated schema object
- Return type
-
uppercase
()[source]¶ Mandates the presence/absense of uppercase letters.
Example
>>> PasswordValidator().uppercase().validate('Test') True >>> PasswordValidator().uppercase().validate('test') False
- Returns
Updated schema object
- Return type
-
lowercase
()[source]¶ Mandates the presence/absense of lowercase letters.
Example
>>> PasswordValidator().lowercase().validate('Test') True >>> PasswordValidator().lowercase().validate('TEST') False
- Returns
Updated schema object
- Return type
-
letters
()[source]¶ Mandates the presence/absense of letters.
Example
>>> PasswordValidator().letters().validate('test') True >>> PasswordValidator().no().letters().validate('test') False
- Returns
Updated schema object
- Return type
-
digits
()[source]¶ Mandates the presence/absense of digits.
Example
>>> PasswordValidator().digits().validate('test') False >>> PasswordValidator().no().digits().validate('test123') False
- Returns
Updated schema object
- Return type
-
min
(length)[source]¶ Sets the minimum length allowed.
Example
>>> PasswordValidator().min(8).validate('testPassword') True >>> PasswordValidator().min(8).validate('test') False
- Parameters
length (int) – Minimum length allowed
- Returns
Updated schema object
- Return type
-
max
(length)[source]¶ Sets the maximum length allowed.
Example
>>> PasswordValidator().max(8).validate('testPassword') False >>> PasswordValidator().max(8).validate('test') True
- Parameters
length (int) – Maximum length allowed
- Returns
Updated schema object
- Return type
-
spaces
()[source]¶ Mandates the presence/absense of whitespace.
Example
>>> PasswordValidator().spaces().validate('a bc') True >>> PasswordValidator().no().spaces().validate('a bc') False
- Returns
Updated schema object
- Return type