Module: DeeplyValid::ValidationHelpers::ClassMethods

Defined in:
lib/deeply_valid/validation_helpers.rb

Instance Method Summary collapse

Instance Method Details

#all(*options) ⇒ Validation

Check that all of the specified Validations are met

Parameters:

  • options (Validation)

    One or more Validation instances

Returns:



140
141
142
143
144
# File 'lib/deeply_valid/validation_helpers.rb', line 140

def all(*options)
  Validation.new do |d| 
    options.all? { |v| v.valid?(d) }
  end
end

#any(*options) ⇒ Validation

Check that any of the specified Validations are met

Parameters:

  • options (Validation)

    One or more Validation instances

Returns:



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/deeply_valid/validation_helpers.rb', line 122

def any(*options)
  if options.all? { |v| v.is_a?(Validation) }
    Validation.new do |d| 
      options.any? { |v| v.valid?(d) }
    end
  else
    Validation.new do |d| 
      options.include?(d)
    end
  end
end

#array(rule = nil) ⇒ Validation

Validate all values in an array

Parameters:

  • rule (Validation) (defaults to: nil)

    The validation rule

Returns:



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

#booleanValidation

Validate true or false

Returns:



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

Parameters:

  • limit (defaults to: nil)

Returns:



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

Parameters:

  • limit (defaults to: nil)

Returns:



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) )

Parameters:

  • example (Hash) (defaults to: nil)

    The desired hash format

Returns:



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)

Parameters:

  • val

    A number, date, or other other object to compare

  • range (Integer, Range, Hash)

    The range to test ‘val` against

Returns:

  • (Boolean)


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

Parameters:

  • klass (Class)

    The class

Returns:



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

Parameters:

  • limit (Integer, Range) (defaults to: nil)

Returns:



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

Returns:



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_valueValidation

Validates nil. Normally a nil means “don’t validate”. Use this if you’re expecting an actual nil

Returns:



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}
{}

Parameters:

Returns:



250
251
252
253
# File 'lib/deeply_valid/validation_helpers.rb', line 250

def optional(validation)
  validation.options[:optional] = true
  validation
end

#string(size = nil) ⇒ Validation

Validates strings by size

Parameters:

  • size (Integer, Range) (defaults to: nil)

    Optional size limitation

Returns:



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

Parameters:

  • limit (Integer, Range) (defaults to: nil)

Returns:



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

Parameters:

  • size (Integer, Range) (defaults to: nil)

    Optional size limitation

Returns:



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