Class: Grape::Validations::ValuesValidator

Inherits:
Validator
  • Object
show all
Defined in:
lib/grape/validations/values.rb

Instance Attribute Summary

Attributes inherited from Validator

#attrs

Instance Method Summary collapse

Methods inherited from Validator

convert_to_short_name, inherited, #validate!

Constructor Details

#initialize(attrs, options, required, scope) ⇒ ValuesValidator

Returns a new instance of ValuesValidator.



4
5
6
7
8
# File 'lib/grape/validations/values.rb', line 4

def initialize(attrs, options, required, scope)
  @values = options
  @required = required
  super
end

Instance Method Details

#validate_param!(attr_name, params) ⇒ Object



10
11
12
13
14
# File 'lib/grape/validations/values.rb', line 10

def validate_param!(attr_name, params)
  if (params[attr_name] || required_for_root_scope?) && !(@values.is_a?(Proc) ? @values.call : @values).include?(params[attr_name])
    raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message_key: :values
  end
end