Class: DeviseSecurity::PasswordComplexityValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/devise-security/validators/password_complexity_validator.rb

Overview

Password complexity validator Options:

  • digit: minimum number of digits in the validated string

  • lower: minimum number of lower-case letters in the validated string

  • symbol: minimum number of punctuation characters or symbols in the validated string

  • upper: minimum number of upper-case letters in the validated string

Constant Summary collapse

PATTERNS =
{
  digit: /\p{Digit}/,
  digits: /\p{Digit}/,
  lower: /\p{Lower}/,
  upper: /\p{Upper}/,
  symbol: /\p{Punct}|\p{S}/,
  symbols: /\p{Punct}|\p{S}/
}.freeze

Instance Method Summary collapse

Instance Method Details

#active_pattern_keysObject


30
31
32
# File 'lib/devise-security/validators/password_complexity_validator.rb', line 30

def active_pattern_keys
  options.keys & PATTERNS.keys
end

#validate_each(record, attribute, value) ⇒ Object


19
20
21
22
23
24
25
26
27
28
# File 'lib/devise-security/validators/password_complexity_validator.rb', line 19

def validate_each(record, attribute, value)
  active_pattern_keys.each do |key|
    minimum = [0, options[key].to_i].max
    pattern = Regexp.new PATTERNS[key]

    unless (value || '').scan(pattern).size >= minimum
      record.errors.add attribute, :"password_complexity.#{key}", count: minimum
    end
  end
end