Sha256: a1a6478b2634d9727bac4e65e256d5e6a442a12af7d5e0262057547e39b9735f

Contents?: true

Size: 1.01 KB

Versions: 62

Compression:

Stored size: 1.01 KB

Contents

# TODO: Extract out into separate gem for v3
class PasswordValidator < ActiveModel::EachValidator
  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
  }

  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
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 lib/workarea/validators/password_validator.rb
workarea-core-3.5.26 lib/workarea/validators/password_validator.rb
workarea-core-3.4.45 lib/workarea/validators/password_validator.rb
workarea-core-3.5.25 lib/workarea/validators/password_validator.rb
workarea-core-3.5.23 lib/workarea/validators/password_validator.rb
workarea-core-3.4.44 lib/workarea/validators/password_validator.rb
workarea-core-3.5.22 lib/workarea/validators/password_validator.rb
workarea-core-3.4.43 lib/workarea/validators/password_validator.rb
workarea-core-3.5.21 lib/workarea/validators/password_validator.rb
workarea-core-3.4.42 lib/workarea/validators/password_validator.rb
workarea-core-3.5.20 lib/workarea/validators/password_validator.rb
workarea-core-3.4.41 lib/workarea/validators/password_validator.rb
workarea-core-3.5.19 lib/workarea/validators/password_validator.rb
workarea-core-3.4.40 lib/workarea/validators/password_validator.rb
workarea-core-3.5.18 lib/workarea/validators/password_validator.rb
workarea-core-3.4.39 lib/workarea/validators/password_validator.rb
workarea-core-3.5.17 lib/workarea/validators/password_validator.rb
workarea-core-3.4.38 lib/workarea/validators/password_validator.rb
workarea-core-3.5.16 lib/workarea/validators/password_validator.rb
workarea-core-3.4.37 lib/workarea/validators/password_validator.rb