Class: Steppe::Endpoint::HeaderValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/steppe/endpoint.rb

Overview

Note:

HTTP header names in Rack env use the format ‘HTTP_*’ (e.g., ‘HTTP_AUTHORIZATION’)

Note:

Security schemes often use this to validate required headers (e.g., Authorization)

Internal step that validates HTTP headers against a schema. Validates headers from the Rack env and merges validated values back into the env. Returns 422 Unprocessable Entity if validation fails.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header_schema) ⇒ HeaderValidator

Returns a new instance of HeaderValidator.

Parameters:

  • header_schema (Hash, Plumb::Composable)

    Schema definition for HTTP headers



107
108
109
# File 'lib/steppe/endpoint.rb', line 107

def initialize(header_schema)
  @header_schema = header_schema.is_a?(Hash) ? Types::Hash[header_schema] : header_schema
end

Instance Attribute Details

#header_schemaObject (readonly)

Returns the value of attribute header_schema.



104
105
106
# File 'lib/steppe/endpoint.rb', line 104

def header_schema
  @header_schema
end

Instance Method Details

#call(conn) ⇒ Result

Validates headers from the request environment.

Parameters:

  • conn (Result)

    The current result/connection object

Returns:

  • (Result)

    Updated result with validated env or error response



115
116
117
118
119
120
121
# File 'lib/steppe/endpoint.rb', line 115

def call(conn)
  result = header_schema.resolve(conn.request.env)
  conn.request.env.merge!(result.value)
  return conn.respond_with(422).invalid(errors: { headers: result.errors }) unless result.valid?

  conn.valid
end