Page MenuHomePhorge

No OneTemporary

Size
4 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/open_api_spex/cast.ex b/lib/open_api_spex/cast.ex
index 59be6e9..6057c6c 100644
--- a/lib/open_api_spex/cast.ex
+++ b/lib/open_api_spex/cast.ex
@@ -1,19 +1,21 @@
defmodule OpenApiSpex.Cast do
alias OpenApiSpex.{CastArray, CastContext, CastObject, CastPrimitive}
@primitives [:boolean, :integer, :number, :string]
def cast(value, schema, schemas \\ nil)
def cast(value, nil, _schemas), do: {:ok, value}
def cast(value, %{type: type} = schema, schemas) when type in @primitives do
ctx = %CastContext{value: value, schema: schema, schemas: schemas}
CastPrimitive.cast(ctx)
end
- def cast(value, %{type: :array} = schema, schemas),
- do: CastArray.cast(value, schema, schemas)
+ def cast(value, %{type: :array} = schema, schemas) do
+ ctx = %CastContext{value: value, schema: schema, schemas: schemas}
+ CastArray.cast(ctx)
+ end
def cast(value, %{type: :object} = schema, schemas),
do: CastObject.cast(value, schema, schemas)
end
diff --git a/lib/open_api_spex/cast_array.ex b/lib/open_api_spex/cast_array.ex
index 0de9a6d..914cbb7 100644
--- a/lib/open_api_spex/cast_array.ex
+++ b/lib/open_api_spex/cast_array.ex
@@ -1,27 +1,25 @@
defmodule OpenApiSpex.CastArray do
@moduledoc false
alias OpenApiSpex.{Cast, Error}
- def cast(value, schema, schemas \\ %{}, index \\ 0)
-
- def cast([], _schema, _schemas, _index) do
+ def cast(%{value: []}) do
{:ok, []}
end
- def cast([item | rest], schema, schemas, index) do
- with {:ok, cast_item} <- cast_item(item, schema.items, schemas, index),
- {:ok, cast_rest} <- cast(rest, schema, schemas, index + 1) do
+ def cast(%{value: [item | rest]} = ctx) do
+ with {:ok, cast_item} <- cast_item(%{ctx | value: item, schema: ctx.schema.items}),
+ {:ok, cast_rest} <- cast(%{ctx | value: rest, index: ctx.index + 1}) do
{:ok, [cast_item | cast_rest]}
end
end
- def cast(value, _schema, _schemas, _index) do
- {:error, Error.new(:invalid_type, :array, value)}
+ def cast(ctx) do
+ {:error, Error.new(:invalid_type, :array, ctx.value)}
end
- defp cast_item(item, items_schema, schemas, index) do
- with {:error, error} <- Cast.cast(item, items_schema, schemas) do
- {:error, %{error | path: [index | error.path]}}
+ defp cast_item(%{value: item, schema: items_schema} = ctx) do
+ with {:error, error} <- Cast.cast(item, items_schema, ctx.schemas) do
+ {:error, %{error | path: [ctx.index | error.path]}}
end
end
end
diff --git a/lib/open_api_spex/cast_context.ex b/lib/open_api_spex/cast_context.ex
index cbd8ece..e96920c 100644
--- a/lib/open_api_spex/cast_context.ex
+++ b/lib/open_api_spex/cast_context.ex
@@ -1,7 +1,8 @@
defmodule OpenApiSpex.CastContext do
defstruct value: nil,
schema: nil,
schemas: %{},
path: [],
+ index: 0,
errors: []
end
diff --git a/test/cast_array_test.exs b/test/cast_array_test.exs
index 124e929..de74419 100644
--- a/test/cast_array_test.exs
+++ b/test/cast_array_test.exs
@@ -1,33 +1,36 @@
defmodule OpenApiSpec.CastArrayTest do
use ExUnit.Case
- import OpenApiSpex.CastArray
- alias OpenApiSpex.{Error, Schema}
+ alias OpenApiSpex.{CastArray, CastContext, Error, Schema}
+
+ defp cast(map) do
+ CastArray.cast(struct(CastContext, map))
+ end
describe "cast/4" do
test "array" do
schema = %Schema{type: :array}
- assert cast([], schema) == {:ok, []}
- assert cast([1, 2, 3], schema) == {:ok, [1, 2, 3]}
- assert cast(["1", "2", "3"], schema) == {:ok, ["1", "2", "3"]}
+ assert cast(%{value: [], schema: schema}) == {:ok, []}
+ assert cast(%{value: [1, 2, 3], schema: schema}) == {:ok, [1, 2, 3]}
+ assert cast(%{value: ["1", "2", "3"], schema: schema}) == {:ok, ["1", "2", "3"]}
- assert {:error, error} = cast(%{}, schema)
+ assert {:error, error} = cast(%{value: %{}, schema: schema})
assert %Error{} = error
assert error.reason == :invalid_type
assert error.value == %{}
end
test "array with items schema" do
items_schema = %Schema{type: :integer}
schema = %Schema{type: :array, items: items_schema}
- assert cast([], schema) == {:ok, []}
- assert cast([1, 2, 3], schema) == {:ok, [1, 2, 3]}
- assert cast(["1", "2", "3"], schema) == {:ok, [1, 2, 3]}
+ assert cast(%{value: [], schema: schema}) == {:ok, []}
+ assert cast(%{value: [1, 2, 3], schema: schema}) == {:ok, [1, 2, 3]}
+ assert cast(%{value: ["1", "2", "3"], schema: schema}) == {:ok, [1, 2, 3]}
- assert {:error, error} = cast([1, "two"], schema)
+ assert {:error, error} = cast(%{value: [1, "two"], schema: schema})
assert %Error{} = error
assert error.reason == :invalid_type
assert error.value == "two"
assert error.path == [1]
end
end
end

File Metadata

Mime Type
text/x-diff
Expires
Sat, Nov 30, 5:46 PM (1 d, 18 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41508
Default Alt Text
(4 KB)

Event Timeline