Class: Grape::Validations::Validators::ValuesValidator

Inherits:
Base
  • Object
show all
Defined in:
lib/grape/validations/validators/values_validator.rb

Instance Attribute Summary

Attributes inherited from Base

#attrs

Instance Method Summary collapse

Methods inherited from Base

#fail_fast?, inherited, #message, #options_key?, #validate, #validate!

Constructor Details

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

Returns a new instance of ValuesValidator.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/grape/validations/validators/values_validator.rb', line 7

def initialize(attrs, options, required, scope, **opts)
  if options.is_a?(Hash)
    @excepts = options[:except]
    @values = options[:value]
    @proc = options[:proc]

    ActiveSupport::Deprecation.warn('The values validator except option is deprecated. Use the except validator instead.') if @excepts

    raise ArgumentError, 'proc must be a Proc' if @proc && !@proc.is_a?(Proc)

    ActiveSupport::Deprecation.warn('The values validator proc option is deprecated. The lambda expression can now be assigned directly to values.') if @proc
  else
    @excepts = nil
    @values = nil
    @proc = nil
    @values = options
  end
  super
end

Instance Method Details

#validate_param!(attr_name, params) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/grape/validations/validators/values_validator.rb', line 27

def validate_param!(attr_name, params)
  return unless params.is_a?(Hash)

  val = params[attr_name]

  return if val.nil? && !required_for_root_scope?

  # don't forget that +false.blank?+ is true
  return if val != false && val.blank? && @allow_blank

  param_array = val.nil? ? [nil] : Array.wrap(val)

  raise validation_exception(attr_name, except_message) \
  unless check_excepts(param_array)

  raise validation_exception(attr_name, message(:values)) \
  unless check_values(param_array, attr_name)

  raise validation_exception(attr_name, message(:values)) \
    if @proc && !validate_proc(@proc, param_array)
end