From d48d8bf0242882333544db0e24423dca9ef23e3b Mon Sep 17 00:00:00 2001 From: Mathieu Decaffmeyer <5883963+mathieuprog@users.noreply.github.com> Date: Wed, 5 Jun 2024 22:16:19 +0800 Subject: [PATCH] Improve options validation --- lib/polymorphic_embed/options_validator.ex | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/polymorphic_embed/options_validator.ex b/lib/polymorphic_embed/options_validator.ex index a787865..249c384 100644 --- a/lib/polymorphic_embed/options_validator.ex +++ b/lib/polymorphic_embed/options_validator.ex @@ -18,10 +18,16 @@ defmodule PolymorphicEmbed.OptionsValidator do @valid_on_type_not_found_options [:raise, :changeset_error, :nilify, :ignore] def validate!(options) do - unless Keyword.fetch!(options, :on_replace) in @valid_on_replace_options do - raise( - "`:on_replace` must be set to `:update` for a single polymorphic embed or `:delete` for a list of polymorphic embeds." - ) + unless is_nil(options[:default]) or options[:default] == [] do + raise "`:default` expected to be `nil` or `[]`." + end + + if is_nil(options[:default]) and options[:on_replace] != :update do + raise "`:on_replace` must be set to `:update` for a single polymorphic embed." + end + + if is_list(options[:default]) and options[:on_replace] != :delete do + raise "`:on_replace` must be set to `:delete` for a list of polymorphic embeds." end unless Keyword.fetch!(options, :on_type_not_found) in @valid_on_type_not_found_options do