Class: API::Validations::Validators::IntegerOrCustomValue

Inherits:
Grape::Validations::Base
  • Object
show all
Defined in:
lib/api/validations/validators/integer_or_custom_value.rb

Direct Known Subclasses

IntegerNoneAny

Instance Method Summary collapse

Constructor Details

#initialize(attrs, options, required, scope, **opts) ⇒ IntegerOrCustomValue

Returns a new instance of IntegerOrCustomValue.


7
8
9
10
# File 'lib/api/validations/validators/integer_or_custom_value.rb', line 7

def initialize(attrs, options, required, scope, **opts)
  @custom_values = extract_custom_values(options)
  super
end

Instance Method Details

#validate_param!(attr_name, params) ⇒ Object

Raises:

  • (Grape::Exceptions::Validation)

12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/api/validations/validators/integer_or_custom_value.rb', line 12

def validate_param!(attr_name, params)
  value = params[attr_name]

  return if value.is_a?(Integer)
  return if @custom_values.map(&:downcase).include?(value.to_s.downcase)

  valid_options = Gitlab::Utils.to_exclusive_sentence(['an integer'] + @custom_values)
  raise Grape::Exceptions::Validation.new(
    params: [@scope.full_name(attr_name)],
    message: "should be #{valid_options}, however got #{value}"
  )
end