Module: SchemaHelperMethods

Includes:
ClassSpecifier
Included in:
ActiveModel::Validations::WithSchemaValidator
Defined in:
lib/password_schema_validator/schema_helper_methods.rb

Constant Summary collapse

RESERVED_OPTIONS =
{
  min_len: :integer,
  max_len: :integer,
  lower_case: :boolean,
  upper_case: :boolean,
  digits: :boolean,
  special_charecters: :boolean,
  allowed_special_chareters: :array,
  discarded_words: :array,
  dictionary: :boolean
}

Instance Method Summary collapse

Methods included from ClassSpecifier

#array, #boolean, #integer

Instance Method Details

#allowed_special_charetersObject



50
51
# File 'lib/password_schema_validator/schema_helper_methods.rb', line 50

def allowed_special_chareters
end

#dictionaryObject



57
58
# File 'lib/password_schema_validator/schema_helper_methods.rb', line 57

def dictionary
end

#digits(validate, string) ⇒ Object



42
43
44
# File 'lib/password_schema_validator/schema_helper_methods.rb', line 42

def digits(validate, string)
  return "must contain digits" if validate && !(/\d+/ =~ string)
end

#discarded_words(black_listed_words, string) ⇒ Object



53
54
55
# File 'lib/password_schema_validator/schema_helper_methods.rb', line 53

def discarded_words(black_listed_words, string)
  return "must not be in the discarded words" if black_listed_words.map(&:downcase).include?(string.downcase)
end

#invalid_key_error(key) ⇒ Object



75
76
77
# File 'lib/password_schema_validator/schema_helper_methods.rb', line 75

def invalid_key_error(key)
  "Invalid key - '" + key.to_s + "' passed to the schema. Provide a valid permitted key."
end

#invalid_value_type(key, expected_type) ⇒ Object



79
80
81
# File 'lib/password_schema_validator/schema_helper_methods.rb', line 79

def invalid_value_type(key, expected_type)
  "'" + key.to_s + "' must be of type " + expected_type.to_s
end

#lower_case(validate, string) ⇒ Object



34
35
36
# File 'lib/password_schema_validator/schema_helper_methods.rb', line 34

def lower_case(validate, string)
  return "must contain lower-case letters" if validate && !(/[a-z]+/ =~ string)
end

#match_against_schema(record, attr_name, string) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/password_schema_validator/schema_helper_methods.rb', line 18

def match_against_schema(record, attr_name, string)
  options.each do |attribute|
    key, value = attribute
    error_msg = send(key, value, string)
    record.errors.add(attr_name, error_msg, options) if error_msg
  end
end

#max_len(maxValue, string) ⇒ Object



30
31
32
# File 'lib/password_schema_validator/schema_helper_methods.rb', line 30

def max_len(maxValue, string)
  return "must be maximum " + maxValue.to_s + " charecter long" if string.length > maxValue.to_i
end

#min_len(minValue, string) ⇒ Object



26
27
28
# File 'lib/password_schema_validator/schema_helper_methods.rb', line 26

def min_len(minValue, string)
  return "must be minimum " + minValue.to_s + " charecter long" if string.length < minValue.to_i
end

#special_charecters(validate, string) ⇒ Object



46
47
48
# File 'lib/password_schema_validator/schema_helper_methods.rb', line 46

def special_charecters(validate, string)
  return "must contain special charecters" if validate && !(/[`~\!@#\$%\^\&\*\(\)\-_\=\+\[\{\}\]\\\|;:'",<.>\/\?]+/ =~ string)
end

#upper_case(validate, string) ⇒ Object



38
39
40
# File 'lib/password_schema_validator/schema_helper_methods.rb', line 38

def upper_case(validate, string)
  return "must contain upper-case letters" if validate && !(/[A-Z]+/ =~ string)
end

#validate_attribute(attribute) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/password_schema_validator/schema_helper_methods.rb', line 66

def validate_attribute(attribute)
  key, value = attribute
  if RESERVED_OPTIONS[key.to_sym].nil?
    raise ArgumentError, invalid_key_error(key)
  elsif !send(RESERVED_OPTIONS[key.to_sym]).include?(value.class)
    raise ArgumentError, invalid_value_type(key, RESERVED_OPTIONS[key.to_sym])
  end
end

#validate_schema(options) ⇒ Object



60
61
62
63
64
# File 'lib/password_schema_validator/schema_helper_methods.rb', line 60

def validate_schema(options)
  options.each do |attribute|
    validate_attribute(attribute)
  end
end