Page MenuHomePhorge

No OneTemporary

Size
8 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/open_api_spex/cast.ex b/lib/open_api_spex/cast.ex
index f018056..4d62dd3 100644
--- a/lib/open_api_spex/cast.ex
+++ b/lib/open_api_spex/cast.ex
@@ -1,76 +1,85 @@
defmodule OpenApiSpex.Cast do
alias OpenApiSpex.Reference
- alias OpenApiSpex.Cast.{Array, Error, Object, Primitive}
+ alias OpenApiSpex.Cast.{Array, Error, Object, Primitive, String}
@primitives [:boolean, :integer, :number, :string]
defstruct value: nil,
schema: nil,
schemas: %{},
path: [],
key: nil,
index: 0,
errors: []
def cast(schema, value, schemas) do
ctx = %__MODULE__{schema: schema, value: value, schemas: schemas}
cast(ctx)
end
# nil schema
def cast(%__MODULE__{value: value, schema: nil}),
do: {:ok, value}
def cast(%__MODULE__{schema: %Reference{}} = ctx) do
schema = Reference.resolve_schema(ctx.schema, ctx.schemas)
cast(%{ctx | schema: schema})
end
# nullable: true
def cast(%__MODULE__{value: nil, schema: %{nullable: true}}) do
{:ok, nil}
end
# nullable: false
def cast(%__MODULE__{value: nil} = ctx) do
error(ctx, {:null_value})
end
# Enum
def cast(%__MODULE__{schema: %{enum: []}} = ctx) do
cast(%{ctx | enum: nil})
end
# Enum
def cast(%__MODULE__{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
error(ctx, {:invalid_enum})
end
end
end
## Specific types
- def cast(%__MODULE__{schema: %{type: type}} = ctx) when type in @primitives,
- do: Primitive.cast(ctx)
+ def cast(%__MODULE__{schema: %{type: :boolean}} = ctx),
+ do: Primitive.cast_boolean(ctx)
+
+ def cast(%__MODULE__{schema: %{type: :integer}} = ctx),
+ do: Primitive.cast_integer(ctx)
+
+ def cast(%__MODULE__{schema: %{type: :number}} = ctx),
+ do: Primitive.cast_number(ctx)
+
+ def cast(%__MODULE__{schema: %{type: :string}} = ctx),
+ do: String.cast(ctx)
def cast(%__MODULE__{schema: %{type: :array}} = ctx),
do: Array.cast(ctx)
def cast(%__MODULE__{schema: %{type: :object}} = ctx),
do: Object.cast(ctx)
def cast(%__MODULE__{schema: %{type: _other}} = ctx),
do: error(ctx, {:invalid_schema_type})
def cast(%{} = ctx), do: cast(struct(__MODULE__, ctx))
def cast(ctx) when is_list(ctx), do: cast(struct(__MODULE__, ctx))
# Add an error
def error(ctx, error_args) do
error = Error.new(ctx, error_args)
{:error, [error | ctx.errors]}
end
end
diff --git a/lib/open_api_spex/cast/primitive.ex b/lib/open_api_spex/cast/primitive.ex
index c283cde..9a02040 100644
--- a/lib/open_api_spex/cast/primitive.ex
+++ b/lib/open_api_spex/cast/primitive.ex
@@ -1,68 +1,58 @@
defmodule OpenApiSpex.Cast.Primitive do
@moduledoc false
alias OpenApiSpex.Cast
- alias OpenApiSpex.Cast.String
- def cast(%{schema: %{type: :boolean}} = ctx),
- do: cast_boolean(ctx)
+ ## boolean
- 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: String.cast(ctx)
-
- ## Private functions
-
- defp cast_boolean(%{value: value}) when is_boolean(value) do
+ def 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}
+ def cast_boolean(%{value: "true"}), do: {:ok, true}
+ def cast_boolean(%{value: "false"}), do: {:ok, false}
- defp cast_boolean(ctx) do
+ def cast_boolean(ctx) do
Cast.error(ctx, {:invalid_type, :boolean})
end
- defp cast_integer(%{value: value}) when is_integer(value) do
+ ## integer
+
+ def cast_integer(%{value: value}) when is_integer(value) do
{:ok, value}
end
- defp cast_integer(%{value: value}) when is_number(value) do
+ def cast_integer(%{value: value}) when is_number(value) do
{:ok, round(value)}
end
- defp cast_integer(%{value: value} = ctx) when is_binary(value) do
+ def cast_integer(%{value: value} = ctx) when is_binary(value) do
case Float.parse(value) do
{value, ""} -> cast_integer(%{ctx | value: value})
_ -> Cast.error(ctx, {:invalid_type, :integer})
end
end
- defp cast_integer(ctx) do
+ def cast_integer(ctx) do
Cast.error(ctx, {:invalid_type, :integer})
end
- defp cast_number(%{value: value}) when is_number(value) do
+ ## number
+ def cast_number(%{value: value}) when is_number(value) do
{:ok, value}
end
- defp cast_number(%{value: value}) when is_integer(value) do
+ def cast_number(%{value: value}) when is_integer(value) do
{:ok, value / 1}
end
- defp cast_number(%{value: value} = ctx) when is_binary(value) do
+ def cast_number(%{value: value} = ctx) when is_binary(value) do
case Float.parse(value) do
{value, ""} -> {:ok, value}
_ -> Cast.error(ctx, {:invalid_type, :number})
end
end
- defp cast_number(ctx) do
+ def cast_number(ctx) do
Cast.error(ctx, {:invalid_type, :number})
end
end
diff --git a/test/cast/primitive_test.exs b/test/cast/primitive_test.exs
index 3fcff0d..95c6605 100644
--- a/test/cast/primitive_test.exs
+++ b/test/cast/primitive_test.exs
@@ -1,52 +1,38 @@
defmodule OpenApiSpex.PrimitiveTest do
use ExUnit.Case
- alias OpenApiSpex.{Cast, Schema}
+ alias OpenApiSpex.Cast
alias OpenApiSpex.Cast.{Primitive, Error}
-
- defp cast(ctx), do: Primitive.cast(struct(Cast, ctx))
+ import Primitive
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 cast_boolean(%Cast{value: true}) == {:ok, true}
+ assert cast_boolean(%Cast{value: false}) == {:ok, false}
+ assert cast_boolean(%Cast{value: "true"}) == {:ok, true}
+ assert cast_boolean(%Cast{value: "false"}) == {:ok, false}
+ assert {:error, [error]} = cast_boolean(%Cast{value: "other"})
assert %Error{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 cast_integer(%Cast{value: 1}) == {:ok, 1}
+ assert cast_integer(%Cast{value: 1.5}) == {:ok, 2}
+ assert cast_integer(%Cast{value: "1"}) == {:ok, 1}
+ assert cast_integer(%Cast{value: "1.5"}) == {:ok, 2}
+ assert {:error, [error]} = cast_integer(%Cast{value: "other"})
assert %Error{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 cast_number(%Cast{value: 1}) == {:ok, 1.0}
+ assert cast_number(%Cast{value: 1.5}) == {:ok, 1.5}
+ assert cast_number(%Cast{value: "1"}) == {:ok, 1.0}
+ assert cast_number(%Cast{value: "1.5"}) == {:ok, 1.5}
+ assert {:error, [error]} = cast_number(%Cast{value: "other"})
assert %Error{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 %Error{reason: :invalid_type} = error
- assert error.value == %{}
- end
end
end

File Metadata

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

Event Timeline