View Source PasswordValidator (Password Validator v0.5.1)
Primary interface to PasswordValidator. The two main methods are validate/3
and validate_password/2
.
Examples
iex> opts = [
...> length: [max: 6],
...> ]
iex> PasswordValidator.validate_password("too_long", opts)
{:error, ["String is too long. 8 but maximum is 6"]}
iex> opts = [
...> length: [min: 5, max: 30],
...> character_set: [
...> lower_case: 1, # at least one lower case letter
...> upper_case: [3, :infinity], # at least three upper case letters
...> numbers: [0, 4], # at most 4 numbers
...> special: [0, 0], # no special characters allowed
...> ]
...> ]
iex> changeset = Ecto.Changeset.change({%{password: "Simple_pass12345"}, %{}}, %{})
iex> changeset = PasswordValidator.validate(changeset, :password, opts)
iex> changeset.errors
[password: {"Too many special (1 but maximum is 0)", [
{:validator, PasswordValidator.Validators.CharacterSetValidator},
{:error_type, :too_many_special}
]},
password: {"Too many numbers (5 but maximum is 4)", [
{:validator, PasswordValidator.Validators.CharacterSetValidator},
{:error_type, :too_many_numbers}
]},
password: {"Not enough upper_case characters (only 1 instead of at least 3)", [
{:validator, PasswordValidator.Validators.CharacterSetValidator},
{:error_type, :too_few_upper_case}
]}]
Summary
Functions
@spec validate(Ecto.Changeset.t(), atom(), list()) :: Ecto.Changeset.t()