Class: ActiveModel::Validations::PasswordValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/active_model/validations/password_validator.rb

Constant Summary collapse

DEFAULT_ERROR_MESSAGES =
{
  name_included_in_password: 'is too similar to your name',
  username_included_in_password: 'is too similar to your username',
  email_included_in_password: 'is too similar to your email',
  domain_included_in_password: 'is too similar to this domain name',
  password_too_short: 'is too short',
  password_too_long: 'is too long',
  not_enough_unique_characters: 'does not have enough unique characters',
  password_not_allowed: 'is not allowed',
  password_too_common: 'is too common',
  fallback: 'is not valid'
}

Instance Method Summary collapse

Instance Method Details

#error_messagesObject



27
28
29
# File 'lib/active_model/validations/password_validator.rb', line 27

def error_messages
  @error_messages ||= DEFAULT_ERROR_MESSAGES
end

#error_messages=(em) ⇒ Object



31
32
33
# File 'lib/active_model/validations/password_validator.rb', line 31

def error_messages=(em)
  @error_messages = em
end

#validate_each(record, attribute, value) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/active_model/validations/password_validator.rb', line 15

def validate_each(record, attribute, value)
  pc = NOBSPW::PasswordChecker.new password: record.send(attribute),
                                   email:    email_value(record),
                                   name:     name_value(record),
                                   username: username_value(record)

  pc.weak_password_reasons.each do |reason|
    record.errors[attribute] << get_message(reason)
  end
  pc.strong?
end