Class: Grape::Validations::DefaultValidator

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

Instance Attribute Summary

Attributes inherited from Validator

#attrs

Instance Method Summary collapse

Methods inherited from Validator

inherited

Constructor Details

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

Returns a new instance of DefaultValidator.


4
5
6
7
# File 'lib/grape/validations/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
# File 'lib/grape/validations/default.rb', line 13

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

#validate_param!(attr_name, params) ⇒ Object


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

def validate_param!(attr_name, params)
  params[attr_name] = @default unless params.has_key?(attr_name)
end