Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F115984
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/open_api_spex/cast/string.ex b/lib/open_api_spex/cast/string.ex
index 9757d15..723cf9c 100644
--- a/lib/open_api_spex/cast/string.ex
+++ b/lib/open_api_spex/cast/string.ex
@@ -1,79 +1,87 @@
defmodule OpenApiSpex.Cast.String do
@moduledoc """
This module will cast a binary to either an Elixir DateTime or Date. Otherwise it will
validate a binary based on maxLength, minLength, or a Regex pattern passed through the
schema struct.
"""
alias OpenApiSpex.{Cast, Cast.Error}
@schema_fields [:maxLength, :minLength, :pattern]
def cast(%{value: value, schema: %{format: :date}} = ctx) when is_binary(value) do
case Date.from_iso8601(value) do
{:ok, %Date{} = date} ->
{:ok, date}
- _ -> Cast.error(ctx, {:invalid_format, :date})
+
+ _ ->
+ Cast.error(ctx, {:invalid_format, :date})
end
end
def cast(%{value: value, schema: %{format: :"date-time"}} = ctx) when is_binary(value) do
case DateTime.from_iso8601(value) do
{:ok, %DateTime{} = datetime, _offset} ->
{:ok, datetime}
- _ -> Cast.error(ctx, {:invalid_format, :"date-time"})
+
+ _ ->
+ Cast.error(ctx, {:invalid_format, :"date-time"})
end
end
def cast(%{value: value} = ctx) when is_binary(value) do
apply_validation(ctx, @schema_fields)
end
def cast(ctx) do
Cast.error(ctx, {:invalid_type, :string})
end
## Private functions
- defp apply_validation(%{value: value, schema: %{maxLength: max_length}} = ctx, [:maxLength|fields])
- when is_integer(max_length) do
+ defp apply_validation(%{value: value, schema: %{maxLength: max_length}} = ctx, [
+ :maxLength | fields
+ ])
+ when is_integer(max_length) do
if String.length(value) > max_length do
ctx
|> apply_error({:max_length, max_length})
|> apply_validation(fields)
else
apply_validation(ctx, fields)
end
end
- defp apply_validation(%{value: value, schema: %{minLength: min_length}} = ctx, [:minLength|fields])
- when is_integer(min_length) do
+ defp apply_validation(%{value: value, schema: %{minLength: min_length}} = ctx, [
+ :minLength | fields
+ ])
+ when is_integer(min_length) do
if String.length(value) < min_length do
ctx
|> apply_error({:min_length, min_length})
|> apply_validation(fields)
else
apply_validation(ctx, fields)
end
end
- defp apply_validation(%{value: value, schema: %{pattern: pattern}} = ctx, [:pattern|fields])
- when not is_nil(pattern) do
+ defp apply_validation(%{value: value, schema: %{pattern: pattern}} = ctx, [:pattern | fields])
+ when not is_nil(pattern) do
if Regex.match?(pattern, value) do
apply_validation(ctx, fields)
else
ctx
|> apply_error({:invalid_format, pattern})
|> apply_validation(fields)
end
end
- defp apply_validation(ctx, [_field|fields]), do: apply_validation(ctx, fields)
+ defp apply_validation(ctx, [_field | fields]), do: apply_validation(ctx, fields)
defp apply_validation(%{value: value, errors: []}, []), do: {:ok, value}
defp apply_validation(%{errors: errors}, []) when length(errors) > 0, do: {:error, errors}
defp apply_error(%{errors: errors} = ctx, error_args) do
Map.put(ctx, :errors, [Error.new(ctx, error_args) | errors])
end
end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Fri, Nov 29, 9:39 PM (1 d, 18 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41310
Default Alt Text
(3 KB)
Attached To
Mode
R22 open_api_spex
Attached
Detach File
Event Timeline
Log In to Comment