Class: Pastvu::ValueCheck
- Inherits:
-
Object
- Object
- Pastvu::ValueCheck
- Defined in:
- lib/pastvu/params_validator/value_check.rb
Constant Summary collapse
- VALIDATIONS =
{ distance: ->(d) { d.between?(1, 1_000_000) }, geo: [->(g) { g.size == 2 }, ->(g) { g.all? { |coordinate| coordinate.instance_of?(Float) || coordinate.instance_of?(Integer) } }], geometry: ->(g) do begin permitted_types = %w[Polygon Multipolyigon] permitted_types.any? { |t| t == g["type"] } rescue TypeError false end end, limit: ->(l) { l.between?(1, 30) }, type: ->(t) { %w[photo painting].any?(t.downcase) } }
Class Method Summary collapse
Class Method Details
.call_each(array, argument) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/pastvu/params_validator/value_check.rb', line 45 def self.call_each(array, argument) array.each do |lambda| return false unless lambda.call(argument) end true end |
.report_errors(errors) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/pastvu/params_validator/value_check.rb', line 37 def self.report_errors(errors) report = errors.map do |k, v| "\n#{k}: #{v[0]}" end.join(", ") raise ArgumentError, "expect params to pass validations\n #{report}" end |
.validate(params) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/pastvu/params_validator/value_check.rb', line 19 def self.validate(params) errors = {} VALIDATIONS.each do |k, v| next if params[k].nil? next if v.instance_of?(Array) ? call_each(v, params[k]) : v.call(params[k]) errors.merge!({ k.to_sym => [params[k], v] }) end if errors.empty? true else report_errors(errors) end end |