Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F116217
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
8 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 9fb2895..59be6e9 100644
--- a/lib/open_api_spex/cast.ex
+++ b/lib/open_api_spex/cast.ex
@@ -1,18 +1,19 @@
defmodule OpenApiSpex.Cast do
- alias OpenApiSpex.{CastArray, CastObject, CastPrimitive}
+ 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
- CastPrimitive.cast(value, schema)
+ 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: :object} = schema, schemas),
do: CastObject.cast(value, schema, schemas)
end
diff --git a/lib/open_api_spex/cast_context.ex b/lib/open_api_spex/cast_context.ex
new file mode 100644
index 0000000..cbd8ece
--- /dev/null
+++ b/lib/open_api_spex/cast_context.ex
@@ -0,0 +1,7 @@
+defmodule OpenApiSpex.CastContext do
+ defstruct value: nil,
+ schema: nil,
+ schemas: %{},
+ path: [],
+ errors: []
+end
diff --git a/test/cast_primitive_test.exs b/test/cast_primitive_test.exs
index 3dfd8d6..4fca245 100644
--- a/test/cast_primitive_test.exs
+++ b/test/cast_primitive_test.exs
@@ -1,75 +1,75 @@
defmodule OpenApiSpex.CastPrimitiveTest do
use ExUnit.Case
alias OpenApiSpex.{Error, Schema}
import OpenApiSpex.CastPrimitive
describe "cast/3" do
@types [:boolean, :integer, :number, :string]
for type <- @types do
@type_value type
test "nil input for nullable:true, type:#{type}" do
schema = %Schema{type: @type_value, nullable: true}
- assert cast(nil, schema) == {:ok, nil}
+ assert cast(%{value: nil, schema: schema}) == {:ok, nil}
end
test "nil input for nullable:false, type:#{type}" do
schema = %Schema{type: @type_value, nullable: false}
- assert {:error, error} = cast(nil, schema)
+ assert {:error, error} = cast(%{value: nil, schema: schema})
assert %Error{} = error
assert error.reason == :invalid_type
assert error.value == nil
end
end
test "boolean" do
schema = %Schema{type: :boolean}
- assert cast(true, schema) == {:ok, true}
- assert cast(false, schema) == {:ok, false}
- assert cast("true", schema) == {:ok, true}
- assert cast("false", schema) == {:ok, false}
- assert {:error, error} = cast("other", schema)
+ assert cast(%{value: true, schema: schema}) == {:ok, true}
+ assert cast(%{value: false, schema: schema}) == {:ok, false}
+ assert cast(%{value: "true", schema: schema}) == {:ok, true}
+ assert cast(%{value: "false", schema: schema}) == {:ok, false}
+ assert {:error, error} = cast(%{value: "other", schema: schema})
assert %Error{reason: :invalid_type} = error
assert error.value == "other"
end
test "integer" do
schema = %Schema{type: :integer}
- assert cast(1, schema) == {:ok, 1}
- assert cast(1.5, schema) == {:ok, 2}
- assert cast("1", schema) == {:ok, 1}
- assert cast("1.5", schema) == {:ok, 2}
- assert {:error, error} = cast("other", schema)
+ assert cast(%{value: 1, schema: schema}) == {:ok, 1}
+ assert cast(%{value: 1.5, schema: schema}) == {:ok, 2}
+ assert cast(%{value: "1", schema: schema}) == {:ok, 1}
+ assert cast(%{value: "1.5", schema: schema}) == {:ok, 2}
+ assert {:error, error} = cast(%{value: "other", schema: schema})
assert %Error{reason: :invalid_type} = error
assert error.value == "other"
end
test "number" do
schema = %Schema{type: :number}
- assert cast(1, schema) == {:ok, 1.0}
- assert cast(1.5, schema) == {:ok, 1.5}
- assert cast("1", schema) == {:ok, 1.0}
- assert cast("1.5", schema) == {:ok, 1.5}
- assert {:error, error} = cast("other", schema)
+ assert cast(%{value: 1, schema: schema}) == {:ok, 1.0}
+ assert cast(%{value: 1.5, schema: schema}) == {:ok, 1.5}
+ assert cast(%{value: "1", schema: schema}) == {:ok, 1.0}
+ assert cast(%{value: "1.5", schema: schema}) == {:ok, 1.5}
+ assert {:error, error} = cast(%{value: "other", schema: schema})
assert %Error{reason: :invalid_type} = error
assert error.value == "other"
end
test "string" do
schema = %Schema{type: :string}
- assert cast("hello", schema) == {:ok, "hello"}
- assert cast("", schema) == {:ok, ""}
- assert {:error, error} = cast(%{}, schema)
+ assert cast(%{value: "hello", schema: schema}) == {:ok, "hello"}
+ assert cast(%{value: "", schema: schema}) == {:ok, ""}
+ assert {:error, error} = cast(%{value: %{}, schema: schema})
assert %Error{reason: :invalid_type} = error
assert error.value == %{}
end
test "string with pattern" do
schema = %Schema{type: :string, pattern: ~r/\d-\d/}
- assert cast("1-2", schema) == {:ok, "1-2"}
- assert {:error, error} = cast("hello", schema)
+ assert cast(%{value: "1-2", schema: schema}) == {:ok, "1-2"}
+ assert {:error, error} = cast(%{value: "hello", schema: schema})
assert error.reason == :invalid_format
assert error.value == "hello"
assert error.format == ~r/\d-\d/
end
end
end
diff --git a/test/support/cast_primitive.ex b/test/support/cast_primitive.ex
index c925e2b..5686f18 100644
--- a/test/support/cast_primitive.ex
+++ b/test/support/cast_primitive.ex
@@ -1,88 +1,87 @@
defmodule OpenApiSpex.CastPrimitive do
@moduledoc false
alias OpenApiSpex.Error
- def cast(value, _schema, _schemas \\ %{})
-
- def cast(nil, %{nullable: true}, _schemas),
+ def cast(%{value: nil, schema: %{nullable: true}}),
do: {:ok, nil}
- def cast(value, %{type: :boolean}, _schemas),
+ def cast(%{value: value, schema: %{type: :boolean}}),
do: cast_boolean(value)
- def cast(value, %{type: :integer}, _schemas),
+ def cast(%{value: value, schema: %{type: :integer}}),
do: cast_integer(value)
- def cast(value, %{type: :number}, _schemas),
+ def cast(%{value: value, schema: %{type: :number}}),
do: cast_number(value)
- def cast(value, %{type: :string} = schema, _schemas),
- do: cast_string(value, schema)
+ def cast(%{schema: %{type: :string}} = ctx),
+ do: cast_string(ctx)
## Private functions
defp cast_boolean(value) when is_boolean(value) do
{:ok, value}
end
defp cast_boolean("true"), do: {:ok, true}
defp cast_boolean("false"), do: {:ok, false}
defp cast_boolean(value) do
{:error, Error.new(:invalid_type, :integer, value)}
end
defp cast_integer(value) when is_integer(value) do
{:ok, value}
end
defp cast_integer(value) when is_number(value) do
{:ok, round(value)}
end
defp cast_integer(value) when is_binary(value) do
case Float.parse(value) do
{value, ""} -> cast_integer(value)
_ -> {:error, Error.new(:invalid_type, :integer, value)}
end
end
defp cast_integer(value) do
{:error, Error.new(:invalid_type, :integer, value)}
end
defp cast_number(value) when is_number(value) do
{:ok, value}
end
defp cast_number(value) when is_integer(value) do
{:ok, value / 1}
end
defp cast_number(value) when is_binary(value) do
case Float.parse(value) do
{value, ""} -> {:ok, value}
_ -> {:error, Error.new(:invalid_type, :number, value)}
end
end
defp cast_number(value) do
{:error, Error.new(:invalid_type, :number, value)}
end
- defp cast_string(value, %{pattern: pattern}) when not is_nil(pattern) and is_binary(value) do
+ defp cast_string(%{value: value, schema: %{pattern: pattern}})
+ when not is_nil(pattern) and is_binary(value) do
if Regex.match?(pattern, value) do
{:ok, value}
else
{:error, Error.new(:invalid_format, pattern, value)}
end
end
- defp cast_string(value, _schema) when is_binary(value) do
+ defp cast_string(%{value: value}) when is_binary(value) do
{:ok, value}
end
- defp cast_string(value, _schema) do
+ defp cast_string(%{value: value}) do
{:error, Error.new(:invalid_type, :string, value)}
end
end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Nov 30, 5:57 PM (1 d, 16 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41516
Default Alt Text
(8 KB)
Attached To
Mode
R22 open_api_spex
Attached
Detach File
Event Timeline
Log In to Comment