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

Inherits:
Validators::Base show all
Defined in:
lib/grape/validations/contract_scope.rb

Instance Attribute Summary collapse

Attributes inherited from Validators::Base

#attrs

Instance Method Summary collapse

Methods inherited from Validators::Base

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

Constructor Details

#initialize(_attrs, _options, _required, _scope, opts) ⇒ Validator

Returns a new instance of Validator.



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

def initialize(_attrs, _options, _required, _scope, opts)
  super
  @schema = opts.fetch(: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

#validate(request) ⇒ void

This method returns an undefined value.

Validates a given request.

Parameters:

Raises:



45
46
47
48
49
50
51
52
53
54
# File 'lib/grape/validations/contract_scope.rb', line 45

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

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

  raise Grape::Exceptions::ValidationArrayErrors.new(build_errors_from_messages(res.errors.messages))
end