Skip to content

Commit

Permalink
Add the "nil" type
Browse files Browse the repository at this point in the history
  • Loading branch information
whatyouhide committed Feb 17, 2023
1 parent be87ca1 commit 5a0b0a9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
23 changes: 17 additions & 6 deletions lib/nimble_options.ex
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ defmodule NimbleOptions do
* `:reference` - A reference (see `t:reference/0`).
* `nil` - The value `nil` itself. Available since v0.6.0.
* `:mfa` - A named function in the format `{module, function, arity}` where
`arity` is a list of arguments. For example, `{MyModule, :my_fun, [arg1, arg2]}`.
Expand Down Expand Up @@ -169,7 +171,7 @@ defmodule NimbleOptions do
same position. For example, to describe 3-element tuples with an atom, a string, and
a list of integers you would use the type `{:tuple, [:atom, :string, {:list, :integer}]}`.
*Available since v0.4.1*.
* `{:struct, struct_name}` - An instance of the struct type given.
## Example
Expand Down Expand Up @@ -285,7 +287,8 @@ defmodule NimbleOptions do
:boolean,
:timeout,
:pid,
:reference
:reference,
nil
]

@typedoc """
Expand Down Expand Up @@ -763,6 +766,18 @@ defmodule NimbleOptions do
end
end

defp validate_type(nil, key, value) do
if is_nil(value) do
{:ok, value}
else
error_tuple(
key,
value,
"invalid value for #{render_key(key)}: expected nil, got: #{inspect(value)}"
)
end
end

defp validate_type({:custom, mod, fun, args}, key, value) do
case apply(mod, fun, [value | args]) do
{:ok, value} ->
Expand Down Expand Up @@ -927,10 +942,6 @@ defmodule NimbleOptions do
end
end

defp validate_type(nil, key, value) do
validate_type(:any, key, value)
end

defp validate_type(_type, _key, value) do
{:ok, value}
end
Expand Down
3 changes: 3 additions & 0 deletions lib/nimble_options/docs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ defmodule NimbleOptions.Docs do
{:non_empty_keyword_list, _keys} ->
quote(do: keyword())

nil ->
quote(do: nil)

:map ->
quote(do: map())

Expand Down
21 changes: 20 additions & 1 deletion test/nimble_options_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ defmodule NimbleOptionsTest do
Available types: :any, :keyword_list, :non_empty_keyword_list, :map, :atom, \
:integer, :non_neg_integer, :pos_integer, :float, :mfa, :mod_arg, :string, :boolean, :timeout, \
:pid, :reference, {:fun, arity}, {:in, choices}, {:or, subtypes}, {:custom, mod, fun, args}, \
:pid, :reference, nil, {:fun, arity}, {:in, choices}, {:or, subtypes}, {:custom, mod, fun, args}, \
{:list, subtype}, {:tuple, list_of_subtypes}, {:map, key_type, value_type}, {:struct, struct_name} \
(in options [:stages])\
"""
Expand Down Expand Up @@ -597,6 +597,25 @@ defmodule NimbleOptionsTest do
}
end

test "valid nil" do
schema = [name: [type: nil, required: true]]
opts = [name: nil]
assert NimbleOptions.validate(opts, schema) == {:ok, opts}
end

test "invalid nil" do
schema = [name: [type: nil, required: true]]
opts = [name: :not_nil]

assert NimbleOptions.validate(opts, schema) ==
{:error,
%ValidationError{
key: :name,
value: :not_nil,
message: "invalid value for :name option: expected nil, got: :not_nil"
}}
end

test "valid {:in, choices}" do
schema = [batch_mode: [type: {:in, [:flush, :bulk]}]]

Expand Down

0 comments on commit 5a0b0a9

Please sign in to comment.