From 94f19f98b3f28089e35cb0adc916a1399f3cf7f0 Mon Sep 17 00:00:00 2001 From: Fernando Braga Date: Mon, 21 Mar 2022 11:33:49 -0300 Subject: [PATCH 1/3] validate now remove previous errors of the specified field, if validator./1 returns true --- lib/changeset.ex | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/changeset.ex b/lib/changeset.ex index e3aa746..088d332 100644 --- a/lib/changeset.ex +++ b/lib/changeset.ex @@ -27,7 +27,7 @@ if Code.ensure_compiled(Ecto) do def validate_cnpj(changeset, field, opts) when is_atom(field) do validate(changeset, field, fn value -> - if Brcpfcnpj.cnpj_valid?(value) do + if Brcpfcnpj.cnpj_valid?(%Cnpj{number: value}) do [] else [{field, message(opts, {"Invalid Cnpj", validation: :cnpj})}] @@ -60,7 +60,7 @@ if Code.ensure_compiled(Ecto) do def validate_cpf(changeset, field, opts) when is_atom(field) do validate(changeset, field, fn value -> - if Brcpfcnpj.cpf_valid?(value) do + if Brcpfcnpj.cpf_valid?(%Cpf{number: value}) do [] else [{field, message(opts, {"Invalid Cpf", validation: :cpf})}] @@ -81,7 +81,10 @@ if Code.ensure_compiled(Ecto) do new = if is_nil(value), do: [], else: validator.(value) case new do - [] -> changeset + [] -> + changeset = %{changeset | errors: Keyword.delete(errors, field)} + %{changeset | valid?: Enum.count(changeset.errors) == 0} + [_ | _] -> %{changeset | errors: new ++ errors, valid?: false} end end From 6abf7a057d3a5845309d59b3cc3eab7ffed9fe42 Mon Sep 17 00:00:00 2001 From: Fernando Braga Date: Mon, 21 Mar 2022 12:08:41 -0300 Subject: [PATCH 2/3] Revert "validate now remove previous errors of the specified field, if validator./1 returns true" This reverts commit 94f19f98b3f28089e35cb0adc916a1399f3cf7f0. --- lib/changeset.ex | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/changeset.ex b/lib/changeset.ex index 088d332..e3aa746 100644 --- a/lib/changeset.ex +++ b/lib/changeset.ex @@ -27,7 +27,7 @@ if Code.ensure_compiled(Ecto) do def validate_cnpj(changeset, field, opts) when is_atom(field) do validate(changeset, field, fn value -> - if Brcpfcnpj.cnpj_valid?(%Cnpj{number: value}) do + if Brcpfcnpj.cnpj_valid?(value) do [] else [{field, message(opts, {"Invalid Cnpj", validation: :cnpj})}] @@ -60,7 +60,7 @@ if Code.ensure_compiled(Ecto) do def validate_cpf(changeset, field, opts) when is_atom(field) do validate(changeset, field, fn value -> - if Brcpfcnpj.cpf_valid?(%Cpf{number: value}) do + if Brcpfcnpj.cpf_valid?(value) do [] else [{field, message(opts, {"Invalid Cpf", validation: :cpf})}] @@ -81,10 +81,7 @@ if Code.ensure_compiled(Ecto) do new = if is_nil(value), do: [], else: validator.(value) case new do - [] -> - changeset = %{changeset | errors: Keyword.delete(errors, field)} - %{changeset | valid?: Enum.count(changeset.errors) == 0} - + [] -> changeset [_ | _] -> %{changeset | errors: new ++ errors, valid?: false} end end From be2c14e85d0a13dcd837311e18192b02a56841d4 Mon Sep 17 00:00:00 2001 From: Fernando Braga Date: Mon, 21 Mar 2022 12:10:27 -0300 Subject: [PATCH 3/3] validate now remove previous errors of the specified field, if validator./1 returns true --- lib/changeset.ex | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/changeset.ex b/lib/changeset.ex index e3aa746..08707c3 100644 --- a/lib/changeset.ex +++ b/lib/changeset.ex @@ -81,7 +81,9 @@ if Code.ensure_compiled(Ecto) do new = if is_nil(value), do: [], else: validator.(value) case new do - [] -> changeset + [] -> + changeset = %{changeset | errors: Keyword.delete(errors, field)} + %{changeset | valid?: Enum.count(changeset.errors) == 0} [_ | _] -> %{changeset | errors: new ++ errors, valid?: false} end end