Sha256: 6e3f6640e76935bbadbc8470a2ee1ab64c29fcddccc163bb50bd089ca6747d46
Contents?: true
Size: 1.73 KB
Versions: 1
Compression:
Stored size: 1.73 KB
Contents
module ActiveModel # :nodoc: module Validations # :nodoc: class StrengthValidator < EachValidator # :nodoc: all def initialize(options) super(options.reverse_merge(:level => :good, :with => :username)) end def validate_each(record, attribute, value) strength = PasswordStrength.test(record.send(options[:with]), value, :exclude => options[:exclude]) record.errors.add(attribute, :too_weak, options) unless PasswordStrength.enabled && strength.valid?(options[:level]) end def check_validity! raise ArgumentError, "The :with option must be supplied" unless options.include?(:with) raise ArgumentError, "The :exclude options must be an array of string or regular expression" if options[:exclude] && !options[:exclude].kind_of?(Array) && !options[:exclude].kind_of?(Regexp) raise ArgumentError, "The :level option must be one of [:weak, :good, :strong]" unless [:weak, :good, :strong].include?(options[:level]) super end end module ClassMethods # Validates that the specified attributes are not weak (according to several rules). # # class Person < ActiveRecord::Base # validates_strength_of :password # end # # The default options are <tt>:level => :good, :with => :username</tt>. # # If you want to compare your password against other field, you have to set the <tt>:with</tt> option. # # validates_strength_of :password, :with => :email # # The available levels are: <tt>:weak</tt>, <tt>:good</tt> and <tt>:strong</tt> # def validates_strength_of(*attr_names) validates_with StrengthValidator, _merge_attributes(attr_names) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
password_strength-0.2.0 | lib/password_strength/active_record/ar3.rb |