Module: DeeplyValid::ValidationHelpers::ClassMethods
- Defined in:
- lib/deeply_valid/validation_helpers.rb
Instance Method Summary collapse
-
#all(*options) ⇒ Validation
Check that all of the specified Validations are met.
-
#any(*options) ⇒ Validation
Check that any of the specified Validations are met.
-
#array(rule = nil) ⇒ Validation
Validate all values in an array.
-
#boolean ⇒ Validation
Validate true or false.
-
#date(limit = nil) ⇒ Validation
Validates date with optional limit.
-
#float(limit = nil) ⇒ Validation
Validates float with optional limit.
-
#hash(example = nil) ⇒ Validation
Validate all keys / values in a hash.
-
#in_range?(val, range) ⇒ Boolean
Determine if a val is included in a range.
-
#instance_of(klass) ⇒ Validation
Validate by class.
-
#integer(limit = nil) ⇒ Validation
Validates integer with optional limit.
-
#json(size = nil) ⇒ Validation
Validate that a string is valid JSON.
-
#nil_value ⇒ Validation
Validates nil.
-
#optional(validation) ⇒ Validation
Set a validation as optional.
-
#string(size = nil) ⇒ Validation
Validates strings by size.
-
#time(limit = nil) ⇒ Validation
Validates time with optional limit.
-
#token(size = nil) ⇒ Validation
Validates tokens, ie.
Instance Method Details
#all(*options) ⇒ Validation
Check that all of the specified Validations are met
140 141 142 143 144 |
# File 'lib/deeply_valid/validation_helpers.rb', line 140 def all(*) Validation.new do |d| .all? { |v| v.valid?(d) } end end |
#any(*options) ⇒ Validation
Check that any of the specified Validations are met
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/deeply_valid/validation_helpers.rb', line 122 def any(*) if .all? { |v| v.is_a?(Validation) } Validation.new do |d| .any? { |v| v.valid?(d) } end else Validation.new do |d| .include?(d) end end end |
#array(rule = nil) ⇒ Validation
Validate all values in an array
188 189 190 191 192 193 194 195 196 |
# File 'lib/deeply_valid/validation_helpers.rb', line 188 def array(rule = nil) return instance_of(Array) unless rule validation = rule.is_a?(Validation) ? rule : Validation.new(rule) Validation.new do |d| d.is_a?(Array) && d.all? { |v| validation.valid?(v) } end end |
#boolean ⇒ Validation
Validate true or false
151 152 153 |
# File 'lib/deeply_valid/validation_helpers.rb', line 151 def boolean any(true, false) end |
#date(limit = nil) ⇒ Validation
Validates date with optional limit
55 56 57 |
# File 'lib/deeply_valid/validation_helpers.rb', line 55 def date(limit = nil) Validation.new { |d| d.is_a?(Date) && in_range?(d, limit) } end |
#float(limit = nil) ⇒ Validation
Validates float with optional limit
65 66 67 |
# File 'lib/deeply_valid/validation_helpers.rb', line 65 def float(limit = nil) Validation.new { |d| d.is_a?(Float) && in_range?(d, limit) } end |
#hash(example = nil) ⇒ Validation
Validate all keys / values in a hash
Example
To make sure that all keys are 32 char long tokens, and all values have a size from 1 to 128 chars, do this:
hash( token(32) => string(1..128) )
169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/deeply_valid/validation_helpers.rb', line 169 def hash(example = nil) return instance_of(Hash) unless example k_rule, v_rule = example.to_a.first k_validation = k_rule.is_a?(Validation) ? k_rule : Validation.new(k_rule) v_validation = v_rule.is_a?(Validation) ? v_rule : Validation.new(v_rule) Validation.new do |d| d.is_a?(Hash) && d.all? { |k, v| k_validation.valid?(k) && v_validation.valid?(v) } end end |
#in_range?(val, range) ⇒ Boolean
Determine if a val is included in a range. A number of range formats are supportet
Example:
All these will return true
in_range(1, 1)
in_range(1, 0..9)
in_range(1, :min => 0, :max => 9)
in_range(1, :less_than_or_equal_to => 2)
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/deeply_valid/validation_helpers.rb', line 214 def in_range?(val, range) return true unless range if range.is_a?(Hash) result = true result &= ( val < range[:before] ) if range[:before] result &= ( val > range[:after] ) if range[:after] result &= ( val <= range[:max] ) if range[:max] result &= ( val >= range[:min] ) if range[:min] result &= ( val < range[:less_than] ) if range[:less_than] result &= ( val <= range[:less_than_or_equal_to] ) if range[:less_than_or_equal_to] result &= ( val > range[:greater_than] ) if range[:greater_than] result &= ( val >= range[:greater_than_or_equal_to] ) if range[:greater_than_or_equal_to] result else range = [range] unless range.respond_to?(:include?) range.include?(val) end end |
#instance_of(klass) ⇒ Validation
Validate by class
95 96 97 |
# File 'lib/deeply_valid/validation_helpers.rb', line 95 def instance_of(klass) Validation.new { |d| d.kind_of?(klass) } end |
#integer(limit = nil) ⇒ Validation
Validates integer with optional limit
45 46 47 |
# File 'lib/deeply_valid/validation_helpers.rb', line 45 def integer(limit = nil) Validation.new { |d| d.is_a?(Integer) && in_range?(d, limit) } end |
#json(size = nil) ⇒ Validation
Validate that a string is valid JSON
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/deeply_valid/validation_helpers.rb', line 104 def json(size=nil) Validation.new do |d| begin JSON.parse(d) rescue false else in_range?(d.size, size) end end end |
#nil_value ⇒ Validation
Validates nil. Normally a nil means “don’t validate”. Use this if you’re expecting an actual nil
85 86 87 |
# File 'lib/deeply_valid/validation_helpers.rb', line 85 def nil_value Validation.new { |d| d.nil? } end |
#optional(validation) ⇒ Validation
Set a validation as optional. This only has any effect if the validation is part of a structure.
The following validation:
{:key => optional(integer)}
Will validate both:
{:key => 1}
{}
250 251 252 253 |
# File 'lib/deeply_valid/validation_helpers.rb', line 250 def optional(validation) validation.[:optional] = true validation end |
#string(size = nil) ⇒ Validation
Validates strings by size
35 36 37 |
# File 'lib/deeply_valid/validation_helpers.rb', line 35 def string(size = nil) Validation.new { |d| d.is_a?(String) && in_range?(d.size, size) } end |
#time(limit = nil) ⇒ Validation
Validates time with optional limit
75 76 77 |
# File 'lib/deeply_valid/validation_helpers.rb', line 75 def time(limit = nil) Validation.new { |d| d.is_a?(Time) && in_range?(d, limit) } end |
#token(size = nil) ⇒ Validation
Validates tokens, ie. strings with letters, numbers, and underscores
25 26 27 |
# File 'lib/deeply_valid/validation_helpers.rb', line 25 def token(size = nil) Validation.new { |d| (d =~ /^[0-9a-zA-Z_]*$/) && in_range?(d.size, size) } end |