Class: HashControl::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_control/validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash, opts = {}) ⇒ Validator

Returns a new instance of Validator.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/hash_control/validator.rb', line 7

def initialize(hash, opts = {})
  if hash.is_a? ::ActiveSupport::HashWithIndifferentAccess
    @hash = hash
  else
    @hash = ::ActiveSupport::HashWithIndifferentAccess.new(hash)
  end
  @error_class = opts[:raising] || ArgumentError
  @term = opts[:term] || 'param'
  @string_keys = opts[:string_keys] || false
  @permitted_keys = Set.new
end

Instance Method Details

#int(*keys) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/hash_control/validator.rb', line 64

def int(*keys)
  permitted_keys.merge keys
  keys.each do |key|
    next if hash[key].is_a? Integer
    error "#{term} #{key.inspect} must be integer but was #{hash[key].inspect}" + postscript
  end
  self
end

#int_or_nil(*keys) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/hash_control/validator.rb', line 73

def int_or_nil(*keys)
  permitted_keys.merge keys
  keys.each do |key|
    next if hash[key].nil? || hash[key].is_a?(Integer)
    error "#{term} #{key.inspect} must be integer but was #{hash[key].inspect}" + postscript
  end
  self
end

#not_nil(*keys) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/hash_control/validator.rb', line 82

def not_nil(*keys)
  permitted_keys.merge keys
  keys.each do |key|
    next unless hash[key].nil?
    error "#{term} #{key.inspect} is nil" + postscript
  end
  self
end

#onlyObject

Checks that only the the previously mentioned keys exist In Rails, ‘permit’ will do this as well, but having this as a separate option allows for specifying permit not at the beginning of the chain



52
53
54
55
56
57
# File 'lib/hash_control/validator.rb', line 52

def only
  unless (extra_keys = hash_keys - permitted_keys).empty?
    error "extra #{terms} #{extra_keys.to_a}" + postscript
  end
  self
end

#permit(*keys) ⇒ Object

Specifies keys that can exist with no further restrictions Does no checking on its own



44
45
46
47
# File 'lib/hash_control/validator.rb', line 44

def permit(*keys)
  permitted_keys.merge keys
  self
end

#permit_only(*keys) ⇒ Object

Similar to Rails’ ‘permit’ method.



60
61
62
# File 'lib/hash_control/validator.rb', line 60

def permit_only(*keys)
  permit(*keys).only
end

#require(*keys) ⇒ Object

Specifies keys that must exist



20
21
22
23
24
25
26
27
# File 'lib/hash_control/validator.rb', line 20

def require(*keys)
  permitted_keys.merge keys
  required_keys = keys.to_set
  unless (missing_keys = required_keys - hash_keys).empty?
    error "required #{terms} #{missing_keys.to_a} missing" + postscript
  end
  self
end

#require_n_of(n, *keys) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/hash_control/validator.rb', line 29

def require_n_of(n, *keys)
  permitted_keys.merge keys
  required_keys = keys.to_set
  if (missing_keys = required_keys - hash_keys).length > n
    error "#{n} or more #{terms} in #{missing_keys.to_a} must be given" + postscript
  end
  self
end

#require_one_of(*keys) ⇒ Object



38
39
40
# File 'lib/hash_control/validator.rb', line 38

def require_one_of(*keys)
  require_n_of(1, *keys)
end