Class: Grape::Validations::ContractScope::Validator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*_args, schema:) ⇒ Validator

Returns a new instance of Validator.



36
37
38
# File 'lib/grape/validations/contract_scope.rb', line 36

def initialize(*_args, schema:)
  @schema = schema
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



34
35
36
# File 'lib/grape/validations/contract_scope.rb', line 34

def schema
  @schema
end

Instance Method Details

#fail_fast?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/grape/validations/contract_scope.rb', line 65

def fail_fast?
  false
end

#validate(request) ⇒ void

This method returns an undefined value.

Validates a given request.

Parameters:

Raises:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/grape/validations/contract_scope.rb', line 44

def validate(request)
  res = schema.call(request.params)

  if res.success?
    request.params.deep_merge!(res.to_h)
    return
  end

  errors = []

  res.errors.messages.each do |message|
    full_name = message.path.first.to_s

    full_name += "[#{message.path[1..].join('][')}]" if message.path.size > 1

    errors << Grape::Exceptions::Validation.new(params: [full_name], message: message.text)
  end

  raise Grape::Exceptions::ValidationArrayErrors.new(errors)
end