Class: ActiveModel::Validations::PasswordStrengthValidator

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

Instance Method Summary collapse

Instance Method Details

#extra_words_for_object(extra_words, object) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_model/validations/password_strength_validator.rb', line 19

def extra_words_for_object(extra_words, object)
  return [] unless extra_words.present?
  if object
    if extra_words.respond_to?(:call)
      extra_words = extra_words.call(object)
    elsif extra_words.kind_of? Symbol
      extra_words = object.send(extra_words)
    end
  end
  extra_words || []
end

#strength_options(options, object) ⇒ Object



13
14
15
16
17
# File 'lib/active_model/validations/password_strength_validator.rb', line 13

def strength_options(options, object)
  strength_options = options.dup
  strength_options[:extra_dictionary_words] = extra_words_for_object(strength_options[:extra_dictionary_words], object)
  strength_options.slice(:min_entropy, :use_dictionary, :min_word_length, :extra_dictionary_words)
end

#validate_each(object, attribute, value) ⇒ Object



6
7
8
9
10
11
# File 'lib/active_model/validations/password_strength_validator.rb', line 6

def validate_each(object, attribute, value)
  ps = ::StrongPassword::StrengthChecker.new(strength_options(options, object))
  unless ps.is_strong?(value.to_s)
    object.errors.add(attribute, :'password.password_strength', options.merge(:value => value.to_s))
  end
end