mirror of
https://github.com/wanderer-industries/wanderer
synced 2025-12-11 02:05:58 +00:00
69 lines
2.0 KiB
Elixir
69 lines
2.0 KiB
Elixir
defmodule WandererAppWeb.OpenAPIHelpers do
|
|
@moduledoc """
|
|
Helpers for validating API responses against OpenAPI schemas.
|
|
"""
|
|
|
|
@doc """
|
|
Validates that the given data conforms to the specified OpenAPI schema.
|
|
|
|
## Examples
|
|
|
|
assert_schema(response_data, "MapSystem", api_spec())
|
|
assert_schema(error_response, "ErrorResponse", api_spec())
|
|
"""
|
|
def assert_schema(data, schema_name, spec) do
|
|
# For now, just do basic validation that the structure is correct
|
|
# until we can fix the OpenApiSpex issue
|
|
schema = spec.components.schemas[schema_name]
|
|
|
|
if schema do
|
|
# Basic validation - check required fields exist
|
|
validate_required_fields(data, schema)
|
|
else
|
|
raise "Schema #{schema_name} not found in spec"
|
|
end
|
|
end
|
|
|
|
defp validate_required_fields(data, %{required: required, properties: properties})
|
|
when is_list(required) do
|
|
Enum.each(required, fn field_name ->
|
|
field_key = if is_map_key(data, field_name), do: field_name, else: to_string(field_name)
|
|
|
|
unless Map.has_key?(data, field_key) do
|
|
raise "Missing required field: #{field_name}"
|
|
end
|
|
|
|
# Recursively validate nested objects
|
|
field_atom = if is_atom(field_name), do: field_name, else: String.to_atom(field_name)
|
|
|
|
if Map.has_key?(properties, field_atom) do
|
|
nested_schema = Map.get(properties, field_atom)
|
|
|
|
if nested_schema && Map.has_key?(nested_schema, :properties) do
|
|
validate_required_fields(Map.get(data, field_key), nested_schema)
|
|
end
|
|
end
|
|
end)
|
|
|
|
data
|
|
end
|
|
|
|
defp validate_required_fields(data, _schema), do: data
|
|
|
|
@doc """
|
|
Validates a request body against its OpenAPI schema.
|
|
"""
|
|
def assert_request_schema(_data, _operation_id, _spec) do
|
|
# This would be more complex in a real implementation
|
|
# For now, we'll implement basic validation
|
|
:ok
|
|
end
|
|
|
|
@doc """
|
|
Gets the API specification for testing.
|
|
"""
|
|
def api_spec do
|
|
WandererAppWeb.ApiSpec.spec()
|
|
end
|
|
end
|