Skip to content

Commit d48d8bf

Browse files
committed
Improve options validation
1 parent 7d1ee14 commit d48d8bf

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

lib/polymorphic_embed/options_validator.ex

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@ defmodule PolymorphicEmbed.OptionsValidator do
1818
@valid_on_type_not_found_options [:raise, :changeset_error, :nilify, :ignore]
1919

2020
def validate!(options) do
21-
unless Keyword.fetch!(options, :on_replace) in @valid_on_replace_options do
22-
raise(
23-
"`:on_replace` must be set to `:update` for a single polymorphic embed or `:delete` for a list of polymorphic embeds."
24-
)
21+
unless is_nil(options[:default]) or options[:default] == [] do
22+
raise "`:default` expected to be `nil` or `[]`."
23+
end
24+
25+
if is_nil(options[:default]) and options[:on_replace] != :update do
26+
raise "`:on_replace` must be set to `:update` for a single polymorphic embed."
27+
end
28+
29+
if is_list(options[:default]) and options[:on_replace] != :delete do
30+
raise "`:on_replace` must be set to `:delete` for a list of polymorphic embeds."
2531
end
2632

2733
unless Keyword.fetch!(options, :on_type_not_found) in @valid_on_type_not_found_options do

0 commit comments

Comments
 (0)