Class: ActiveModel::Validations::PasswordValidator

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

Constant Summary collapse

REGEXES =
{
  :weak   => /(?=.{6,}).*/, # 6 characters
  :medium => /^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$/, #len=7 chars and numbers
  :strong => /^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[\d\W]).*$/#len=8 chars and numbers and special chars
}

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



10
11
12
13
14
15
# File 'lib/active_validators/active_model/validations/password_validator.rb', line 10

def validate_each(record, attribute, value)
  required_strength = options.fetch(:strength, :weak)
  if (REGEXES[required_strength] !~ value)
    record.errors.add(attribute)
  end
end