Module: RequestParamsValidation::Params::Validators::Length

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

Instance Method Summary collapse

Instance Method Details

#default_invalid_length_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/length.rb', line 19

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

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

#validate_length!Object



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

def validate_length!
  min = param.length.min
  max = param.length.max

  if (min && value.length < min) || (max && value.length > max)
    raise_error(
      :on_invalid_parameter_length,
      min: min,
      max: max,
      details: param.length.message || default_invalid_length_message(min, max)
    )
  end
end