From de06780d5b7e491181cfab33e47db6d71523c18c Mon Sep 17 00:00:00 2001 From: Alex McLain Date: Wed, 17 Sep 2025 16:23:42 -0700 Subject: [PATCH] Optional boolean that is not present returns nil --- lib/speck.ex | 13 +++++-------- test/speck_test.exs | 9 +++++---- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/speck.ex b/lib/speck.ex index f8d4fe9..e6390e8 100644 --- a/lib/speck.ex +++ b/lib/speck.ex @@ -57,13 +57,6 @@ defmodule Speck do } end - opts[:optional] && is_nil(raw_value) && type == :boolean -> - { - Map.put(fields, name, false), - errors, - make_meta(name, field_status, raw_value) - } - is_nil(opts[:optional]) && is_nil(raw_value) -> { fields, @@ -72,7 +65,11 @@ defmodule Speck do } opts[:optional] && is_nil(raw_value) -> - {fields, errors, make_meta(name, field_status, raw_value)} + { + Map.put(fields, name, nil), + errors, + make_meta(name, field_status, raw_value) + } is_list(type) -> raw_value diff --git a/test/speck_test.exs b/test/speck_test.exs index 6a71501..7798c62 100644 --- a/test/speck_test.exs +++ b/test/speck_test.exs @@ -7,6 +7,7 @@ defmodule Speck.Test do "type" => "air_quality", "rs485_address" => "5", "serial_number" => "DEVICE1234567890", + # wifi_ssid => not present # low_power_mode => not present "dns_servers" => [ "1.1.1.1", @@ -34,7 +35,7 @@ defmodule Speck.Test do rs485_address: 5, serial_number: "DEVICE1234567890", wifi_ssid: nil, - low_power_mode: false, + low_power_mode: nil, dns_servers: [ "1.1.1.1", "1.0.0.1", @@ -702,8 +703,8 @@ defmodule Speck.Test do attribute_5: 5 }, list_attribute_1: [ - %{}, - %{attribute_9: 9} + %{attribute_9: nil}, + %{attribute_9: 9}, ], list_attribute_2: [] } @@ -767,7 +768,7 @@ defmodule Speck.Test do partially_known_nested: %{ attribute_5: 5 }, - list_attribute_1: [%{}], + list_attribute_1: [%{attribute_9: nil}], list_attribute_2: [] }