Class: PasswordValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/workarea/validators/password_validator.rb

Overview

TODO: Extract out into separate gem for v3

Constant Summary collapse

MIN_LENGTHS =
{
  weak: 7,
  medium: 7,
  strong: 8
}
REGEXES =
{
  weak:   %r[(?=.{#{MIN_LENGTHS[:weak]},}).*], # 7 characters
  medium: %r[^(?=.{#{MIN_LENGTHS[:medium]},})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$], # len=7 chars and numbers
  strong: %r[^.*(?=.{#{MIN_LENGTHS[:strong]},})(?=.*[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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/workarea/validators/password_validator.rb', line 15

def validate_each(record, attribute, value)
  return if value.blank?

  required_strength = options.fetch(:strength, :weak)

  if !required_strength.in?(REGEXES.keys) && record.respond_to?(required_strength)
    required_strength = record.send(required_strength)
  end

  if REGEXES[required_strength] !~ value
    record.errors.add(
      attribute,
      "password_#{required_strength}_requirements".to_sym,
      min: MIN_LENGTHS[required_strength]
    )
  end
end