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

default_message_key, #fail_fast?, inherited, new, #validate, #validate!

Constructor Details

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

Returns a new instance of ValuesValidator.



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

def initialize(attrs, options, required, scope, opts)
  super
  values = option_value

  # Zero-arity procs return a collection per-request (e.g. DB-backed lists).
  # Non-zero-arity procs are per-element predicates, called directly at validation time.
  # Non-Proc values are wrapped in a zero-arity lambda for a uniform call interface.
  if values.is_a?(Proc)
    @values_call = values
    @values_is_predicate = !values.arity.zero?
  else
    @values_call = -> { values }
    @values_is_predicate = false
  end
end

Instance Method Details

#validate_param!(attr_name, params) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/grape/validations/validators/values_validator.rb', line 25

def validate_param!(attr_name, params)
  return unless hash_like?(params)

  val = scrub(params[attr_name])

  return if val.nil? && !required_for_root_scope?
  return if val != false && val.blank? && @allow_blank
  return if check_values?(val, attr_name)

  validation_error!(attr_name)
end