Class: IsValid
Overview
Ruby Gem to make a validation of a variable or a hash, based on regular expressions or own pre-defined validation templates in an easy way. See full documentation on GitHub.
Constant Summary collapse
- TYPES =
%w[string array hash class module symbol].freeze
- STRICT_TYPES =
%w[integer float].freeze
Instance Method Summary collapse
- #check(val, rules) ⇒ Object
- #check_hash(hash_var, template, errors = []) ⇒ Object
- #check_one_of_rules(val, rules, key = 'variable') ⇒ Object
- #check_required(hash_var, template_rules) ⇒ Object
- #check_with_types(val, rule_name) ⇒ Object
-
#initialize(settings = {}) ⇒ IsValid
constructor
A new instance of IsValid.
- #rule_exists(rule) ⇒ Object
Constructor Details
#initialize(settings = {}) ⇒ IsValid
Returns a new instance of IsValid.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/is_valid.rb', line 21 def initialize(settings = {}) @templates = settings[:templates] || {} @errors = settings[:errors] || {} @all_keys = settings[:all_keys] || false @strict_types = settings[:strict_types] || false rules = settings[:rules] || {} types_hash = {} TYPES.each do |type| types_hash[type.to_sym] = type end validators = init_rules @rules = validators.merge(rules) @rules = @rules.merge(types_hash) end |
Instance Method Details
#check(val, rules) ⇒ Object
36 37 38 39 |
# File 'lib/is_valid.rb', line 36 def check(val, rules) validated = rules.is_a?(String) ? check_with_types(val, rules) : check_one_of_rules(val, rules) validated.is_a?(Array) ? false : validated end |
#check_hash(hash_var, template, errors = []) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/is_valid.rb', line 68 def check_hash(hash_var, template, errors = []) template_rules = @templates[template.to_sym] return [error_no_template(template)] if template_rules.nil? req_errors = check_required(hash_var, template_rules) return req_errors unless req_errors.empty? hash_var.each do |key, val| rules = template_rules[key.to_sym] if @all_keys && rules.nil? errors << error_no_key(key, template) elsif !rules.nil? rules = [rules] if rules.is_a?(String) validated = check_one_of_rules(val, rules, key) errors += validated if validated != true inner = check_hash(val, template, errors) if val.is_a?(Hash) errors += inner if !inner.nil? && inner != true end end errors.empty? ? true : errors.uniq end |
#check_one_of_rules(val, rules, key = 'variable') ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/is_valid.rb', line 56 def check_one_of_rules(val, rules, key = 'variable') errors = [] rules.each do |rule| if rule_exists(rule) errors << error_no_rule(rule) elsif !check(val, rule) errors << error_not_valid(key, rule, @errors) end end !errors.empty? && errors.length == rules.length ? errors : true end |
#check_required(hash_var, template_rules) ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/is_valid.rb', line 90 def check_required(hash_var, template_rules) errors = [] template_rules.each_key do |key| if key.match?('_required$') key = key.to_s.gsub(/_required$/, '') errors << error_required_key(key) if hash_var[key.to_sym].nil? end end errors end |
#check_with_types(val, rule_name) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/is_valid.rb', line 41 def check_with_types(val, rule_name) return true if rule_name == 'any' return val.nil? if rule_name == 'nil' return val.is_a?(Object.const_get(rule_name.capitalize)) if TYPES.include?(rule_name) return val.is_a?(Object.const_get(rule_name.capitalize)) if @strict_types && STRICT_TYPES.include?(rule_name) return [true, false].include?(val) if @strict_types && rule_name == 'boolean' rule = @rules[rule_name.to_sym] val.to_s.match?(rule) end |
#rule_exists(rule) ⇒ Object
52 53 54 |
# File 'lib/is_valid.rb', line 52 def rule_exists(rule) !rule.nil? && @rules[rule.to_sym].nil? end |