Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F116204
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/open_api_spex/cast_array.ex b/lib/open_api_spex/cast_array.ex
index 79b812b..0de9a6d 100644
--- a/lib/open_api_spex/cast_array.ex
+++ b/lib/open_api_spex/cast_array.ex
@@ -1,27 +1,27 @@
defmodule OpenApiSpex.CastArray do
@moduledoc false
alias OpenApiSpex.{Cast, Error}
- def cast(_value, _schema, _schemas, index \\ 0)
+ def cast(value, schema, schemas \\ %{}, index \\ 0)
def cast([], _schema, _schemas, _index) 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
{:ok, [cast_item | cast_rest]}
end
end
def cast(value, _schema, _schemas, _index) do
{:error, Error.new(:invalid_type, :array, 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]}}
end
end
end
diff --git a/test/cast_array_test.exs b/test/cast_array_test.exs
new file mode 100644
index 0000000..124e929
--- /dev/null
+++ b/test/cast_array_test.exs
@@ -0,0 +1,33 @@
+defmodule OpenApiSpec.CastArrayTest do
+ use ExUnit.Case
+ import OpenApiSpex.CastArray
+ alias OpenApiSpex.{Error, Schema}
+
+ 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 {:error, error} = cast(%{}, 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 {:error, error} = cast([1, "two"], schema)
+ assert %Error{} = error
+ assert error.reason == :invalid_type
+ assert error.value == "two"
+ assert error.path == [1]
+ end
+ end
+end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Nov 30, 5:34 PM (1 d, 15 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41503
Default Alt Text
(2 KB)
Attached To
Mode
R22 open_api_spex
Attached
Detach File
Event Timeline
Log In to Comment