Page MenuHomePhorge

No OneTemporary

Size
5 KB
Referenced Files
None
Subscribers
None
diff --git a/test/cast_primitive_test.exs b/test/cast_primitive_test.exs
index 7445762..3dfd8d6 100644
--- a/test/cast_primitive_test.exs
+++ b/test/cast_primitive_test.exs
@@ -1,66 +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}
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
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 %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 %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 %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 %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 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 82cd329..c925e2b 100644
--- a/test/support/cast_primitive.ex
+++ b/test/support/cast_primitive.ex
@@ -1,80 +1,88 @@
defmodule OpenApiSpex.CastPrimitive do
@moduledoc false
alias OpenApiSpex.Error
def cast(value, _schema, _schemas \\ %{})
def cast(nil, %{nullable: true}, _schemas),
do: {:ok, nil}
def cast(value, %{type: :boolean}, _schemas),
do: cast_boolean(value)
def cast(value, %{type: :integer}, _schemas),
do: cast_integer(value)
def cast(value, %{type: :number}, _schemas),
do: cast_number(value)
- def cast(value, %{type: :string}, _schemas),
- do: cast_string(value)
+ def cast(value, %{type: :string} = schema, _schemas),
+ do: cast_string(value, schema)
## 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) when is_binary(value) do
+ defp cast_string(value, %{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
{:ok, value}
end
- defp cast_string(value) do
+ defp cast_string(value, _schema) do
{:error, Error.new(:invalid_type, :string, value)}
end
end

File Metadata

Mime Type
text/x-diff
Expires
Sat, Nov 30, 6:34 PM (1 d, 16 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41521
Default Alt Text
(5 KB)

Event Timeline