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, inherited

Constructor Details

#initialize(attrs, options, required, scope) ⇒ 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)
  @default = options
  super
end

Instance Method Details

#validate!(params) ⇒ Object



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

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

#validate_param!(attr_name, params) ⇒ Object



9
10
11
# File 'lib/grape/validations/validators/default.rb', line 9

def validate_param!(attr_name, params)
  params[attr_name] = @default.is_a?(Proc) ? @default.call : @default unless params.key?(attr_name)
end