Class: OpenapiFirst::ValidatedRequest

Inherits:
SimpleDelegator
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/openapi_first/validated_request.rb

Overview

A validated request. It can be valid or not.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_request, error:, parsed_values: {}, request_definition: nil) ⇒ ValidatedRequest

Returns a new instance of ValidatedRequest.



11
12
13
14
15
16
# File 'lib/openapi_first/validated_request.rb', line 11

def initialize(original_request, error:, parsed_values: {}, request_definition: nil)
  super(original_request)
  @parsed_values = Hash.new({}).merge(parsed_values)
  @error = error
  @request_definition = request_definition
end

Instance Attribute Details

#errorFailure? (readonly)

A Failure object if the request is invalid

Returns:



20
21
22
# File 'lib/openapi_first/validated_request.rb', line 20

def error
  @error
end

#request_definitionRequest? (readonly)

The request definition if this request is defined in the API description

Returns:



24
25
26
# File 'lib/openapi_first/validated_request.rb', line 24

def request_definition
  @request_definition
end

Instance Method Details

#invalid?Boolean

Checks if the request is invalid.

Returns:

  • (Boolean)


73
74
75
# File 'lib/openapi_first/validated_request.rb', line 73

def invalid?
  !valid?
end

#known?Boolean

Returns true if the request is defined.

Returns:

  • (Boolean)


78
79
80
# File 'lib/openapi_first/validated_request.rb', line 78

def known?
  request_definition != nil
end

#operationHash

Returns The raw OpenAPI 3 operation object.

Returns:

  • (Hash)

    The raw OpenAPI 3 operation object



32
# File 'lib/openapi_first/validated_request.rb', line 32

def_delegator :request_definition, :operation

#operation_idString?

Returns The OpenAPI 3 operationId.

Returns:

  • (String, nil)

    The OpenAPI 3 operationId



28
# File 'lib/openapi_first/validated_request.rb', line 28

def_delegator :request_definition, :operation_id

#parsed_bodyHash<String, anything>

Parsed body. This parses the body according to the content type. Note that this returns the hole body, not only the fields that are defined in the OpenAPI spec. You can use JSON Schemas ‘additionalProperties` or `unevaluatedProperties` to returns a validation error if the body contains unknown fields.

Returns:

  • (Hash<String, anything>)


63
64
65
# File 'lib/openapi_first/validated_request.rb', line 63

def parsed_body
  @parsed_values[:body]
end

#parsed_cookiesHash<String, anything>

Parsed cookies. This only returns the query parameters that are defined in the OpenAPI spec.

Returns:

  • (Hash<String, anything>)


54
55
56
# File 'lib/openapi_first/validated_request.rb', line 54

def parsed_cookies
  @parsed_values[:cookies]
end

#parsed_headersHash<String, anything>

Parsed headers. This only returns the query parameters that are defined in the OpenAPI spec.

Returns:

  • (Hash<String, anything>)


48
49
50
# File 'lib/openapi_first/validated_request.rb', line 48

def parsed_headers
  @parsed_values[:headers]
end

#parsed_paramsHash<String, anything>

Merged path, query, body parameters. Here path has the highest precedence, then query, then body.

Returns:

  • (Hash<String, anything>)


85
86
87
# File 'lib/openapi_first/validated_request.rb', line 85

def parsed_params
  @parsed_params ||= parsed_body.merge(parsed_query, parsed_path_parameters)
end

#parsed_path_parametersHash<String, anything>

Parsed path parameters

Returns:

  • (Hash<String, anything>)


36
37
38
# File 'lib/openapi_first/validated_request.rb', line 36

def parsed_path_parameters
  @parsed_values[:path]
end

#parsed_queryHash<String, anything>

Parsed query parameters. This only returns the query parameters that are defined in the OpenAPI spec.

Returns:

  • (Hash<String, anything>)


42
43
44
# File 'lib/openapi_first/validated_request.rb', line 42

def parsed_query
  @parsed_values[:query]
end

#valid?Boolean

Checks if the request is valid.

Returns:

  • (Boolean)


68
69
70
# File 'lib/openapi_first/validated_request.rb', line 68

def valid?
  error.nil?
end