Page MenuHomePhorge

No OneTemporary

Size
6 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/open_api_spex/cast_primitive.ex b/lib/open_api_spex/cast_primitive.ex
index 03f5e14..eb24180 100644
--- a/lib/open_api_spex/cast_primitive.ex
+++ b/lib/open_api_spex/cast_primitive.ex
@@ -1,84 +1,67 @@
defmodule OpenApiSpex.CastPrimitive do
@moduledoc false
- alias OpenApiSpex.CastContext
+ alias OpenApiSpex.{CastContext, CastString}
def cast(%{schema: %{type: :boolean}} = ctx),
do: cast_boolean(ctx)
def cast(%{schema: %{type: :integer}} = ctx),
do: cast_integer(ctx)
def cast(%{schema: %{type: :number}} = ctx),
do: cast_number(ctx)
def cast(%{schema: %{type: :string}} = ctx),
- do: cast_string(ctx)
+ do: CastString.cast(ctx)
## Private functions
defp cast_boolean(%{value: value}) when is_boolean(value) do
{:ok, value}
end
defp cast_boolean(%{value: "true"}), do: {:ok, true}
defp cast_boolean(%{value: "false"}), do: {:ok, false}
defp cast_boolean(ctx) do
CastContext.error(ctx, {:invalid_type, :boolean})
end
defp cast_integer(%{value: value}) when is_integer(value) do
{:ok, value}
end
defp cast_integer(%{value: value}) when is_number(value) do
{:ok, round(value)}
end
defp cast_integer(%{value: value} = ctx) when is_binary(value) do
case Float.parse(value) do
{value, ""} -> cast_integer(%{ctx | value: value})
_ -> CastContext.error(ctx, {:invalid_type, :integer})
end
end
defp cast_integer(ctx) do
CastContext.error(ctx, {:invalid_type, :integer})
end
defp cast_number(%{value: value}) when is_number(value) do
{:ok, value}
end
defp cast_number(%{value: value}) when is_integer(value) do
{:ok, value / 1}
end
defp cast_number(%{value: value} = ctx) when is_binary(value) do
case Float.parse(value) do
{value, ""} -> {:ok, value}
_ -> CastContext.error(ctx, {:invalid_type, :number})
end
end
defp cast_number(ctx) do
CastContext.error(ctx, {:invalid_type, :number})
end
-
- defp cast_string(%{value: value, schema: %{pattern: pattern}} = ctx)
- when not is_nil(pattern) and is_binary(value) do
- if Regex.match?(pattern, value) do
- {:ok, value}
- else
- CastContext.error(ctx, {:invalid_format, pattern})
- end
- end
-
- defp cast_string(%{value: value}) when is_binary(value) do
- {:ok, value}
- end
-
- defp cast_string(ctx) do
- CastContext.error(ctx, {:invalid_type, :string})
- end
end
diff --git a/lib/open_api_spex/cast_string.ex b/lib/open_api_spex/cast_string.ex
new file mode 100644
index 0000000..5ab2a63
--- /dev/null
+++ b/lib/open_api_spex/cast_string.ex
@@ -0,0 +1,21 @@
+defmodule OpenApiSpex.CastString do
+ @moduledoc false
+ alias OpenApiSpex.CastContext
+
+ def cast(%{value: value, schema: %{pattern: pattern}} = ctx)
+ when not is_nil(pattern) and is_binary(value) do
+ if Regex.match?(pattern, value) do
+ {:ok, value}
+ else
+ CastContext.error(ctx, {:invalid_format, pattern})
+ end
+ end
+
+ def cast(%{value: value}) when is_binary(value) do
+ {:ok, value}
+ end
+
+ def cast(ctx) do
+ CastContext.error(ctx, {:invalid_type, :string})
+ end
+end
diff --git a/test/cast_primitive_test.exs b/test/cast_primitive_test.exs
index 1fe9aba..b30a964 100644
--- a/test/cast_primitive_test.exs
+++ b/test/cast_primitive_test.exs
@@ -1,59 +1,51 @@
defmodule OpenApiSpex.CastPrimitiveTest do
use ExUnit.Case
alias OpenApiSpex.{CastContext, CastPrimitive, CastError, Schema}
defp cast(ctx), do: CastPrimitive.cast(struct(CastContext, ctx))
describe "cast/3" do
test "boolean" do
schema = %Schema{type: :boolean}
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 %CastError{reason: :invalid_type} = error
assert error.value == "other"
end
test "integer" do
schema = %Schema{type: :integer}
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 %CastError{reason: :invalid_type} = error
assert error.value == "other"
end
test "number" do
schema = %Schema{type: :number}
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 %CastError{reason: :invalid_type} = error
assert error.value == "other"
end
+ # Additional string tests are covered in CastStringTest
test "string" do
schema = %Schema{type: :string}
assert cast(value: "hello", schema: schema) == {:ok, "hello"}
assert cast(value: "", schema: schema) == {:ok, ""}
assert {:error, [error]} = cast(value: %{}, schema: schema)
assert %CastError{reason: :invalid_type} = error
assert error.value == %{}
end
-
- test "string with pattern" do
- schema = %Schema{type: :string, pattern: ~r/\d-\d/}
- 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/cast_string_test.exs b/test/cast_string_test.exs
new file mode 100644
index 0000000..082d2a4
--- /dev/null
+++ b/test/cast_string_test.exs
@@ -0,0 +1,26 @@
+defmodule OpenApiSpex.CastStringTest do
+ use ExUnit.Case
+ alias OpenApiSpex.{CastContext, CastError, CastString, Schema}
+
+ defp cast(ctx), do: CastString.cast(struct(CastContext, ctx))
+
+ describe "cast/1" do
+ test "basics" do
+ schema = %Schema{type: :string}
+ assert cast(value: "hello", schema: schema) == {:ok, "hello"}
+ assert cast(value: "", schema: schema) == {:ok, ""}
+ assert {:error, [error]} = cast(value: %{}, schema: schema)
+ assert %CastError{reason: :invalid_type} = error
+ assert error.value == %{}
+ end
+
+ test "string with pattern" do
+ schema = %Schema{type: :string, pattern: ~r/\d-\d/}
+ 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

File Metadata

Mime Type
text/x-diff
Expires
Sat, Nov 30, 2:36 PM (1 d, 18 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41453
Default Alt Text
(6 KB)

Event Timeline