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


4
5
6
7
# File 'lib/grape/validations/validators/default.rb', line 4

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

Instance Method Details

#validate!(params) ⇒ Object


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

def validate!(params)
  attrs = AttributesIterator.new(self, @scope, params)
  attrs.each do |resource_params, attr_name|
    if resource_params.is_a?(Hash) && resource_params[attr_name].nil?
      validate_param!(attr_name, resource_params)
    end
  end
end

#validate_param!(attr_name, params) ⇒ Object


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

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