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

PasswordValidator

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 a regex if provided

to the schema. Works with: letters(), digits(), uppercase(), lowercase(), symbols() and spaces().

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

PasswordValidator

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() and spaces().

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

PasswordValidator

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

PasswordValidator

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

PasswordValidator

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

PasswordValidator

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

PasswordValidator

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

PasswordValidator

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

PasswordValidator

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

PasswordValidator

symbols()[source]

Mandates the presence/absense of special characters like @, #, $, etc.

Example

>>> PasswordValidator().symbols().validate('@bc')
True
>>> PasswordValidator().no().symbols().validate('@bc')
False
Returns

Updated schema object

Return type

PasswordValidator