Class: Grape::Validations::Validators::DefaultValidator

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

Instance Attribute Summary

Attributes inherited from Base

#attrs

Instance Method Summary collapse

Methods inherited from Base

default_message_key, inherited, new, #validate

Constructor Details

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

Returns a new instance of DefaultValidator.



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

def initialize(attrs, options, required, scope, opts)
  super
  # !important, lazy call at runtime
  @default_call =
    if options.is_a?(Proc)
      options.arity.zero? ? proc { options.call } : options
    elsif options.duplicable?
      proc { options.dup }
    else
      proc { options }
    end
end

Instance Method Details

#validate!(params) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/grape/validations/validators/default_validator.rb', line 20

def validate!(params)
  @iterator.each(params) do |resource_params, attr_name|
    next unless scope.meets_dependency?(resource_params, params)

    resource_params[attr_name] = @default_call.call(resource_params) if hash_like?(resource_params) && resource_params[attr_name].nil?
  end
end