Skip to content

Optional Enumerables do not create empty instances when omitted #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions lib/speck.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ defmodule Speck do
!is_nil(opts[:default]) && is_nil(raw_value) ->
{Map.put(fields, name, opts[:default]), errors}

type == :map ->
case do_validate(type, raw_value, opts, attributes) do
{value, map_errors} when map_errors == %{} ->
{Map.put(fields, name, value), errors}

{value, map_errors} ->
{Map.put(fields, name, value), Map.put(errors, name, map_errors)}
end

opts[:optional] && is_nil(raw_value) && type == :boolean ->
{Map.put(fields, name, false), errors}

Expand All @@ -43,6 +34,15 @@ defmodule Speck do
opts[:optional] && is_nil(raw_value) ->
{fields, errors}

type == :map ->
case do_validate(type, raw_value, opts, attributes) do
{value, map_errors} when map_errors == %{} ->
{Map.put(fields, name, value), errors}

{value, map_errors} ->
{Map.put(fields, name, value), Map.put(errors, name, map_errors)}
end

is_list(type) ->
raw_value
|> Enum.with_index
Expand Down Expand Up @@ -142,6 +142,12 @@ defmodule Speck do
end)

case coerced_maplist do
{[], []} ->
{fields, errors}

{[], error} ->
{fields, Map.put(errors, name, error)}

{value, []} ->
{Map.put(fields, name, value), errors}

Expand Down
9 changes: 1 addition & 8 deletions test/speck_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,7 @@ defmodule Speck.Test do
rs485_address: :not_present,
serial_number: :not_present,
dns_servers: :not_present,
metadata: %{
location: :not_present,
department: :not_present,
commissioned_at: :not_present,
ports: %{
rs485: :not_present,
}
}
metadata: :not_present
}}
end

Expand Down