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

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

Constructor Details

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

Returns a new instance of DefaultValidator.



7
8
9
10
# File 'lib/grape/validations/validators/default_validator.rb', line 7

def initialize(attrs, options, required, scope, **opts)
  @default = options
  super
end

Instance Method Details

#validate!(params) ⇒ Object



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

def validate!(params)
  attrs = SingleAttributeIterator.new(self, @scope, params)
  attrs.each do |resource_params, attr_name|
    next unless @scope.meets_dependency?(resource_params, params)

    validate_param!(attr_name, resource_params) if resource_params.is_a?(Hash) && resource_params[attr_name].nil?
  end
end

#validate_param!(attr_name, params) ⇒ Object



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

def validate_param!(attr_name, params)
  params[attr_name] = if @default.is_a? Proc
                        if @default.parameters.empty?
                          @default.call
                        else
                          @default.call(params)
                        end
                      elsif @default.frozen? || !@default.duplicable?
                        @default
                      else
                        @default.dup
                      end
end