Page MenuHomePhorge

No OneTemporary

Size
5 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/open_api_spex/cast/string.ex b/lib/open_api_spex/cast/string.ex
index a181973..aea0776 100644
--- a/lib/open_api_spex/cast/string.ex
+++ b/lib/open_api_spex/cast/string.ex
@@ -1,68 +1,60 @@
defmodule OpenApiSpex.Cast.String do
@moduledoc false
alias OpenApiSpex.Cast
def cast(%{value: value} = ctx) when is_binary(value) do
case cast_binary(ctx) do
{:cast, ctx} -> cast(ctx)
result -> result
end
end
def cast(ctx) do
Cast.error(ctx, {:invalid_type, :string})
end
## Private functions
defp cast_binary(%{value: value, schema: %{format: :"date-time"}} = ctx)
when is_binary(value) do
case DateTime.from_iso8601(value) do
{:ok, %DateTime{}, _offset} -> Cast.success(ctx, :format)
_ -> Cast.error(ctx, {:invalid_format, :"date-time"})
end
end
defp cast_binary(%{value: value, schema: %{format: :date}} = ctx) do
case Date.from_iso8601(value) do
{:ok, %Date{}} -> Cast.success(ctx, :format)
_ -> Cast.error(ctx, {:invalid_format, :date})
end
end
defp cast_binary(%{value: value, schema: %{pattern: pattern}} = ctx) when not is_nil(pattern) do
if Regex.match?(pattern, value) do
Cast.success(ctx, :pattern)
else
Cast.error(ctx, {:invalid_format, pattern})
end
end
defp cast_binary(%{value: value, schema: %{minLength: min_length}} = ctx)
- when is_integer(min_length) do
- # Note: This is not part of the JSON Shema spec: trim string before measuring length
- # It's just too important to miss
- length = String.trim(value) |> String.length()
-
- if length < min_length do
+ when is_integer(min_length) do
+ if String.length(value) < min_length do
Cast.error(ctx, {:min_length, min_length})
else
Cast.success(ctx, :minLength)
end
end
defp cast_binary(%{value: value, schema: %{maxLength: max_length}} = ctx)
- when is_integer(max_length) do
- # Note: This is not part of the JSON Shema spec: trim string before measuring length
- # It's just too important to miss
- length = String.trim(value) |> String.length()
-
- if length > max_length do
+ when is_integer(max_length) do
+ if String.length(value) > max_length do
Cast.error(ctx, {:max_length, max_length})
else
Cast.success(ctx, :maxLength)
end
end
defp cast_binary(ctx), do: Cast.ok(ctx)
end
diff --git a/test/cast/string_test.exs b/test/cast/string_test.exs
index b17195b..d21c772 100644
--- a/test/cast/string_test.exs
+++ b/test/cast/string_test.exs
@@ -1,75 +1,75 @@
defmodule OpenApiSpex.CastStringTest do
use ExUnit.Case
alias OpenApiSpex.{Cast, Schema}
alias OpenApiSpex.Cast.{Error, String}
defp cast(ctx), do: String.cast(struct(Cast, 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 %Error{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
test "string with format (date time)" do
schema = %Schema{type: :string, format: :"date-time"}
time_string = DateTime.utc_now() |> DateTime.to_string()
assert cast(value: time_string, schema: schema) == {:ok, time_string}
assert {:error, [error]} = cast(value: "hello", schema: schema)
assert error.reason == :invalid_format
assert error.value == "hello"
assert error.format == :"date-time"
end
test "string with format (date)" do
schema = %Schema{type: :string, format: :date}
date_string = DateTime.utc_now() |> DateTime.to_date() |> Date.to_string()
assert cast(value: date_string, schema: schema) == {:ok, date_string}
assert {:error, [error]} = cast(value: "hello", schema: schema)
assert error.reason == :invalid_format
assert error.value == "hello"
assert error.format == :date
end
# Note: we measure length of string after trimming leading and trailing whitespace
test "minLength" do
schema = %Schema{type: :string, minLength: 1}
assert {:ok, "a"} = cast(value: "a", schema: schema)
- assert {:error, [error]} = cast(value: " ", schema: schema)
+ assert {:error, [error]} = cast(value: "", schema: schema)
assert %Error{} = error
assert error.reason == :min_length
end
# Note: we measure length of string after trimming leading and trailing whitespace
test "maxLength" do
schema = %Schema{type: :string, maxLength: 1}
assert {:ok, "a"} = cast(value: "a", schema: schema)
assert {:error, [error]} = cast(value: "aa", schema: schema)
assert %Error{} = error
assert error.reason == :max_length
end
test "maxLength and minLength" do
schema = %Schema{type: :string, minLength: 1, maxLength: 2}
assert {:error, [error]} = cast(value: "", schema: schema)
assert %Error{} = error
assert error.reason == :min_length
assert {:error, [error]} = cast(value: "aaa", schema: schema)
assert %Error{} = error
assert error.reason == :max_length
end
end
end

File Metadata

Mime Type
text/x-diff
Expires
Sat, Nov 30, 10:54 AM (1 d, 19 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41417
Default Alt Text
(5 KB)

Event Timeline