Class: Grape::Validations::DefaultValidator

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

Instance Attribute Summary

Attributes inherited from Base

#attrs

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

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

Returns a new instance of DefaultValidator.


6
7
8
9
# File 'lib/grape/validations/validators/default.rb', line 6

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

Instance Method Details

#validate!(params) ⇒ Object


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

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


11
12
13
14
15
16
17
18
19
# File 'lib/grape/validations/validators/default.rb', line 11

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