Skip to content

Commit

Permalink
Support validation type for :integer
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonator committed Aug 24, 2024
1 parent d42dc90 commit d7df5c2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
22 changes: 17 additions & 5 deletions lib/nimble_options.ex
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,8 @@ defmodule NimbleOptions do
end
end

defp validate_type(:integer, key, value, redact) when not is_integer(value) do
structured_error_tuple(key, value, "integer", redact)
defp validate_type(:integer = type, key, value, redact) when not is_integer(value) do
structured_error_tuple(key, value, {:type, type}, "integer", redact)
end

defp validate_type(:non_neg_integer, key, value, redact)
Expand Down Expand Up @@ -1019,19 +1019,31 @@ defmodule NimbleOptions do
{:error, %ValidationError{key: key, message: message, redact: redact, value: value}}
end

# FIX: remove after error types are implemented
defp structured_error_tuple(key, value, expected, redact?) do
structured_error_tuple(key, value, expected, inspect(value), redact?)
structured_error_tuple(key, value, false, expected, redact?)
end

defp structured_error_tuple(key, value, expected, got, redact?) do
defp structured_error_tuple(key, value, type, expected, redact?) do
structured_error_tuple(key, value, type, expected, inspect(value), redact?)
end

defp structured_error_tuple(key, value, validation, expected, got, redact?) do
message =
if redact? do
"invalid value for #{render_key(key)}: expected #{expected}"
else
"invalid value for #{render_key(key)}: expected #{expected}, got: #{got}"
end

{:error, %ValidationError{key: key, message: message, redact: redact?, value: value}}
{:error,
%ValidationError{
key: key,
message: message,
redact: redact?,
value: value,
validation: validation
}}
end

defp render_key({__MODULE__, :key}), do: "map key"
Expand Down
5 changes: 4 additions & 1 deletion lib/nimble_options/validation_error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule NimbleOptions.ValidationError do
key: atom(),
keys_path: [atom()],
redact: boolean,
validation: :required | {:type, term()},
value: term()
}

Expand All @@ -28,8 +29,10 @@ defmodule NimbleOptions.ValidationError do
* `:value` (`t:term/0`) - The value that failed to validate. This field is `nil` if there
was no value provided.
* `:validation` (`:required` or `{:type, type}`) - The validation that failed.
"""
defexception [:message, :key, :value, keys_path: [], redact: false]
defexception [:message, :key, :validation, :value, keys_path: [], redact: false]

@impl true
def message(%__MODULE__{message: message, keys_path: keys_path}) do
Expand Down
1 change: 1 addition & 0 deletions test/nimble_options_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ defmodule NimbleOptionsTest do
%ValidationError{
key: :min_demand,
value: 1.5,
validation: {:type, :integer},
message: "invalid value for :min_demand option: expected integer, got: 1.5"
}}

Expand Down

0 comments on commit d7df5c2

Please sign in to comment.