Method: Focuslight::Validator.validate

Defined in:
lib/focuslight/validator.rb

.validate(params, spec) ⇒ Object

Validator.validate(params, {

:request_param_key_name => { # single key, single value
  :default => default_value,
  :rule => [
    Validator.rule(:not_null),
    Validator.rule(:int_range, 0..10),
  ],
},
:array_value_key_name => { # single key, array value
  :array => true
  :size => 1..10 # default is unlimited (empty also allowed)
  # default cannot be used
  :rule => [ ... ]
}
# ...
[:param1, :param2, :param3] => {
  # default cannot be used
  :rule => Validator::Rule.new(->(p1, p2, p3){ ... }, "error_message")
},

}



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/focuslight/validator.rb', line 25

def self.validate(params, spec)
  result = Result.new
  spec.each do |key, specitem|
    if key.is_a?(Array)
      validate_multi_key(result, params, key, specitem)
    elsif specitem[:array]
      validate_array(result, params, key, specitem)
    else
      validate_single(result, params, key, specitem)
    end
  end
  result
end