Sha256: 408687395ff63605fff3b7a84bc166126caa880f3bd588375f0a4f78bfc8042d

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

# Password complexity validator
# Options:
# - digit:  minimum number of digits in the validated string
# - digits: 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
# - symbols: minimum number of punctuation characters or symbols in the validated string
# - upper:  minimum number of upper-case letters in the validated string
class DeviseSecurity::PasswordComplexityValidator < ActiveModel::EachValidator
  PATTERNS = {
    digit: /\p{Digit}/,
    digits: /\p{Digit}/,
    lower: /\p{Lower}/,
    symbol: /\p{Punct}|\p{S}/,
    symbols: /\p{Punct}|\p{S}/,
    upper: /\p{Upper}/
  }.freeze

  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

  def active_pattern_keys
    options.keys & PATTERNS.keys
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
devise-security-0.16.0 lib/devise-security/validators/password_complexity_validator.rb
devise-security-0.15.0 lib/devise-security/validators/password_complexity_validator.rb