Skip to content

Commit

Permalink
Merge branch 'allow-nil-values-in-schemas'
Browse files Browse the repository at this point in the history
  • Loading branch information
keathley committed Sep 22, 2019
2 parents 83ac526 + 70f2e9b commit 65f7fa1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/norm/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,13 @@ defmodule Norm.Schema do
end

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

if val == nil do
{key, {:error, [error(path ++ [key], input, ":required")]}}
else
{key, Conformable.conform(spec, val, path ++ [key])}
true ->
val = Map.get(input, key)
{key, Conformable.conform(spec, val, path ++ [key])}
end
end

Expand Down
8 changes: 8 additions & 0 deletions test/norm/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ defmodule Norm.SchemaTest do
assert %{bool: false} == conform!(%{bool: false}, s)
end

test "allows keys to have nil values" do
s = schema(%{foo: spec(is_nil())})

assert %{foo: nil} == conform!(%{foo: nil}, s)
assert {:error, errors} = conform(%{foo: 123}, s)
assert errors == ["val: 123 in: :foo fails: is_nil()"]
end

describe "generation" do
test "works with maps" do
s =
Expand Down

0 comments on commit 65f7fa1

Please sign in to comment.