Class: Grape::Validations::ValuesValidator

Inherits:
Base show all
Defined in:
lib/grape/validations/validators/values.rb

Instance Attribute Summary

Attributes inherited from Base

#attrs

Instance Method Summary collapse

Methods inherited from Base

convert_to_short_name, inherited, #validate!

Constructor Details

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

Returns a new instance of ValuesValidator.



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

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

Instance Method Details

#validate_param!(attr_name, params) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/grape/validations/validators/values.rb', line 9

def validate_param!(attr_name, params)
  return unless params[attr_name] || required_for_root_scope?

  values = @values.is_a?(Proc) ? @values.call : @values
  param_array = params[attr_name].nil? ? [nil] : Array.wrap(params[attr_name])
  unless param_array.all? { |param| values.include?(param) }
    fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message_key: :values
  end
end