Module: RequestParamsValidation::Params::Validators::Value

Defined in:
lib/request_params_validation/params/validators/value.rb

Instance Method Summary collapse

Instance Method Details

#default_invalid_value_message(min, max) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/request_params_validation/params/validators/value.rb', line 19

def default_invalid_value_message(min, max)
  message = if param.element_of_array?
              'All elements of the array should have a value'
            else
              'Value should be'
            end

  if min && max
    "#{message} between #{min} and #{max}"
  elsif min
    "#{message} greater or equal than #{min}"
  else
    "#{message} less or equal than #{max}"
  end
end

#validate_value!Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/request_params_validation/params/validators/value.rb', line 5

def validate_value!
  min = param.value.min
  max = param.value.max

  if (min && value < min) || (max && value > max)
    raise_error(
      :on_invalid_parameter_value_size,
      min: min,
      max: max,
      details: param.value.message || default_invalid_value_message(min, max)
    )
  end
end