class OpenapiValidator::PathValidator

Attributes

api_doc[R]

Public Class Methods

call(**params) click to toggle source
# File lib/openapi_validator/path_validator.rb, line 25
def self.call(**params)
  new(**params).call
end
new(request:, api_doc:) click to toggle source
# File lib/openapi_validator/path_validator.rb, line 40
def initialize(request:, api_doc:)
  @request = request
  @api_doc = api_doc
end

Public Instance Methods

call() click to toggle source
# File lib/openapi_validator/path_validator.rb, line 29
def call
  validate_path_exists
  self
end
empty_schema?() click to toggle source
# File lib/openapi_validator/path_validator.rb, line 9
def empty_schema?
  @empty_schema || false
end
fragment(media_type: nil) click to toggle source
# File lib/openapi_validator/path_validator.rb, line 17
def fragment(media_type: nil)
  build_fragment(media_type: media_type).tap do |array|
    array.define_singleton_method(:split) do |_|
      self
    end
  end
end
path() click to toggle source
# File lib/openapi_validator/path_validator.rb, line 13
def path
  [path_key, method, @schema_code]
end

Private Instance Methods

build_fragment(media_type: nil) click to toggle source
# File lib/openapi_validator/path_validator.rb, line 96
def build_fragment(media_type: nil)
  fragment =
    if @fragment_path
      @fragment_path.split("/")
    else
      ["#", "paths", path_key, method, "responses", @schema_code]
    end

  fragment += ["content", media_type || self.media_type, "schema"] unless @empty_schema

  fragment
end
code_schema() click to toggle source
# File lib/openapi_validator/path_validator.rb, line 55
def code_schema
  schema = schema_code
  ref_schema(schema) || content_schema(schema)
end
content_schema(responses) click to toggle source
# File lib/openapi_validator/path_validator.rb, line 60
def content_schema(responses)
  responses.dig("content")
end
path_schema() click to toggle source
# File lib/openapi_validator/path_validator.rb, line 91
def path_schema
  api_doc.dig("paths", path_key) ||
    raise(Error, "OpenAPI documentation does not have a documented path for #{path_key}")
end
ref_schema(responses) click to toggle source
# File lib/openapi_validator/path_validator.rb, line 64
def ref_schema(responses)
  schema = responses.dig("$ref")
  return unless schema

  @fragment_path = schema
  api_doc.dig(*schema[2..-1].split("/"), "content")
end
responses_schema() click to toggle source
# File lib/openapi_validator/path_validator.rb, line 86
def responses_schema
  path_schema.dig(method, "responses") ||
    raise(Error, "OpenAPI documentation does not have a documented path for #{method.upcase} #{path_key}")
end
schema_code() click to toggle source
# File lib/openapi_validator/path_validator.rb, line 72
def schema_code
  responses = responses_schema
  if responses.dig(code)
    @schema_code = code
  elsif responses.dig("default")
    @schema_code = "default"
  else
    raise(Error, "OpenAPI documentation does not have a documented response for code #{code}"\
                " at path #{method.upcase} #{path_key}")
  end

  responses.dig(@schema_code)
end
validate_path_exists() click to toggle source
# File lib/openapi_validator/path_validator.rb, line 45
def validate_path_exists
  if code_schema.nil?
    @empty_schema = true
    return
  end
  code_schema.dig(media_type) ||
    raise(Error, "OpenAPI documentation does not have a documented response"\
                 " for #{media_type} media-type at path #{method.upcase} #{path_key}")
end