Skip to content

Commit 65f7fa1

Browse files
committed
Merge branch 'allow-nil-values-in-schemas'
2 parents 83ac526 + 70f2e9b commit 65f7fa1

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

lib/norm/schema.ex

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,13 @@ defmodule Norm.Schema do
9090
end
9191

9292
defp check_spec({key, spec}, input, path) do
93-
val = Map.get(input, key)
93+
case Map.has_key?(input, key) do
94+
false ->
95+
{key, {:error, [error(path ++ [key], input, ":required")]}}
9496

95-
if val == nil do
96-
{key, {:error, [error(path ++ [key], input, ":required")]}}
97-
else
98-
{key, Conformable.conform(spec, val, path ++ [key])}
97+
true ->
98+
val = Map.get(input, key)
99+
{key, Conformable.conform(spec, val, path ++ [key])}
99100
end
100101
end
101102

test/norm/schema_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ defmodule Norm.SchemaTest do
5858
assert %{bool: false} == conform!(%{bool: false}, s)
5959
end
6060

61+
test "allows keys to have nil values" do
62+
s = schema(%{foo: spec(is_nil())})
63+
64+
assert %{foo: nil} == conform!(%{foo: nil}, s)
65+
assert {:error, errors} = conform(%{foo: 123}, s)
66+
assert errors == ["val: 123 in: :foo fails: is_nil()"]
67+
end
68+
6169
describe "generation" do
6270
test "works with maps" do
6371
s =

0 commit comments

Comments
 (0)