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:



129
130
131
132
133
# File 'lib/deeply_valid/validation_helpers.rb', line 129

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:



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/deeply_valid/validation_helpers.rb', line 111

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:



177
178
179
180
181
182
183
184
185
# File 'lib/deeply_valid/validation_helpers.rb', line 177

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:



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

def boolean
  any(true, false)
end

#date(limit = nil) ⇒ Validation

Validates date with optional limit

Parameters:

  • limit (defaults to: nil)

Returns:



54
55
56
# File 'lib/deeply_valid/validation_helpers.rb', line 54

def date(limit = nil)
  Validation.new { |d| d.is_a?(Date) && in_range?(d, limit) }
end

#datetime(limit = nil) ⇒ Validation

Validates date with optional limit

Parameters:

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

Returns:



74
75
76
# File 'lib/deeply_valid/validation_helpers.rb', line 74

def datetime(limit = nil)
  Validation.new { |d| d.is_a?(DateTime) && in_range?(d, limit) }
end

#float(limit = nil) ⇒ Validation

Validates float with optional limit

Parameters:

  • limit (defaults to: nil)

Returns:



64
65
66
# File 'lib/deeply_valid/validation_helpers.rb', line 64

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:



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/deeply_valid/validation_helpers.rb', line 158

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)


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/deeply_valid/validation_helpers.rb', line 203

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:



84
85
86
# File 'lib/deeply_valid/validation_helpers.rb', line 84

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:



44
45
46
# File 'lib/deeply_valid/validation_helpers.rb', line 44

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:



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/deeply_valid/validation_helpers.rb', line 93

def json(size=nil)
  Validation.new do |d| 
    begin
      JSON.parse(d)
    rescue
      false
    else
      in_range?(d.size, size)
    end
  end
end

#string(size = nil) ⇒ Validation

Validates strings by size

Parameters:

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

    Optional size limitation

Returns:



34
35
36
# File 'lib/deeply_valid/validation_helpers.rb', line 34

def string(size = nil)
  Validation.new { |d| d.is_a?(String) && in_range?(d.size, size) }
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:



24
25
26
# File 'lib/deeply_valid/validation_helpers.rb', line 24

def token(size = nil)
  Validation.new { |d| (d =~ /^[0-9a-zA-Z_]*$/) && in_range?(d.size, size) }
end