Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F116153
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/open_api_spex/cast.ex b/lib/open_api_spex/cast.ex
index cbaa55e..9af2f38 100644
--- a/lib/open_api_spex/cast.ex
+++ b/lib/open_api_spex/cast.ex
@@ -1,57 +1,57 @@
defmodule OpenApiSpex.Cast do
alias OpenApiSpex.{CastArray, CastContext, CastObject, CastPrimitive, Reference}
@primitives [:boolean, :integer, :number, :string]
def cast(schema, value, schemas) do
ctx = %CastContext{schema: schema, value: value, schemas: schemas}
cast(ctx)
end
# nil schema
def cast(%CastContext{value: value, schema: nil}),
do: {:ok, value}
def cast(%CastContext{schema: %Reference{}} = ctx) do
schema = Reference.resolve_schema(ctx.schema, ctx.schemas)
cast(%{ctx | schema: schema})
end
# nullable: true
def cast(%CastContext{value: nil, schema: %{nullable: true}}) do
{:ok, nil}
end
# nullable: false
def cast(%CastContext{value: nil} = ctx) do
CastContext.error(ctx, {:null_value})
end
# Enum
def cast(%CastContext{schema: %{enum: []}} = ctx) do
cast(%{ctx | enum: nil})
end
# Enum
def cast(%CastContext{schema: %{enum: enum}} = ctx) when is_list(enum) do
with {:ok, value} <- cast(%{ctx | schema: %{ctx.schema | enum: nil}}) do
if value in enum do
{:ok, value}
else
CastContext.error(ctx, {:invalid_enum})
end
end
end
- # Specific types
+ ## Specific types
def cast(%CastContext{schema: %{type: type}} = ctx) when type in @primitives,
do: CastPrimitive.cast(ctx)
def cast(%CastContext{schema: %{type: :array}} = ctx),
do: CastArray.cast(ctx)
def cast(%CastContext{schema: %{type: :object}} = ctx),
do: CastObject.cast(ctx)
def cast(%{} = ctx), do: cast(struct(CastContext, ctx))
end
diff --git a/lib/open_api_spex/cast_array.ex b/lib/open_api_spex/cast_array.ex
index 23c0bd6..5d99c2f 100644
--- a/lib/open_api_spex/cast_array.ex
+++ b/lib/open_api_spex/cast_array.ex
@@ -1,35 +1,41 @@
defmodule OpenApiSpex.CastArray do
@moduledoc false
alias OpenApiSpex.{Cast, CastContext}
def cast(%{value: []}) do
{:ok, []}
end
def cast(%{value: items} = ctx) when is_list(items) do
- results =
+ case cast_items(ctx) do
+ {items, []} -> {:ok, items}
+ {_, errors} -> {:error, errors}
+ end
+ end
+
+ def cast(ctx) do
+ CastContext.error(ctx, {:invalid_type, :array})
+ end
+
+ ## Private functions
+
+ defp cast_items(%{value: items} = ctx) do
+ cast_results =
items
|> Enum.with_index()
|> Enum.map(fn {item, index} ->
path = [index | ctx.path]
Cast.cast(%{ctx | value: item, schema: ctx.schema.items, path: path})
end)
errors =
- Enum.flat_map(results, fn
+ Enum.flat_map(cast_results, fn
{:error, errors} -> errors
_ -> []
end)
- if errors == [] do
- items = Enum.map(results, fn {:ok, item} -> item end)
- {:ok, items}
- else
- {:error, errors}
- end
- end
+ items = for {:ok, item} <- cast_results, do: item
- def cast(ctx) do
- CastContext.error(ctx, {:invalid_type, :array})
+ {items, errors}
end
end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Nov 30, 2:38 PM (1 d, 18 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41456
Default Alt Text
(3 KB)
Attached To
Mode
R22 open_api_spex
Attached
Detach File
Event Timeline
Log In to Comment