Skip to content

Commit

Permalink
Fix main_validator and halt the error when we have errors inside vali…
Browse files Browse the repository at this point in the history
…dator and not load main_validator, based on #27
  • Loading branch information
shahryarjb committed Apr 6, 2024
1 parent ae56c63 commit 5beacb5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
15 changes: 14 additions & 1 deletion lib/macros/guarded_struct/guarded_struct.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,9 @@ defmodule GuardedStruct do

{status, main_outputs} =
cond do
length(validated_errors) > 0 ->
{:error, %{}}

!is_nil(main_validator) ->
{module, func} = main_validator
apply(module, func, [validated_allowed_data])
Expand Down Expand Up @@ -2649,18 +2652,28 @@ defmodule GuardedStruct do
{:ok, 0, sub_errors, true} when sub_errors != [] ->
{:error, :nested, sub_builders_errors, struct(module, main_error_or_data), conds.data}

{:ok, val_err, _, false} when val_err > 0 ->
errors = cond_errors_converter(conds)
{:error, validated_errors ++ sub_builders_errors ++ errors}

{:ok, _, _, false} ->
errors = cond_errors_converter(conds)
{:error, validated_errors ++ sub_builders_errors ++ errors, main_error_or_data}

{:error, _, _, false} ->
{:error, val_err, _, false} when val_err > 0 ->
errors = cond_errors_converter(conds)
{:error, validated_errors ++ sub_builders_errors ++ errors}

{:error, _, _, false} ->
errors = cond_errors_converter(conds)
{:error, validated_errors ++ sub_builders_errors ++ [main_error_or_data] ++ errors}

{:ok, _, _, true} ->
{:error, validated_errors ++ sub_builders_errors}

{:error, val_err, _, true} when val_err > 0 ->
{:error, validated_errors ++ sub_builders_errors}

{:error, _, _, true} ->
{:error, validated_errors ++ sub_builders_errors ++ [main_error_or_data]}
end
Expand Down
1 change: 0 additions & 1 deletion lib/modules/mnesia_assistant/operation/table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,6 @@ defmodule MnesiaAssistant.Table do
end

defp re_wait_for_tables(false, {:ok, :atomic}, _, _, _, identifier, _) do
:persistent_term.put(identifier, %{table: true})
{:ok, :create_table, identifier}
end

Expand Down
10 changes: 7 additions & 3 deletions test/guarded_struct_test/validator_derive_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule MishkaDeveloperToolsTest.GuardedStruct.ValidatorDeriveTest do
end

def main_validator(value) do
if Map.get(value, :changed) == 555_555 do
if Map.get(value, :changed) == 555_555 or Map.get(value, :action) == 25 do
{:error, %{message: "there is an Error", field: :global, action: :main_validator}}
else
{:ok, value}
Expand Down Expand Up @@ -465,9 +465,13 @@ defmodule MishkaDeveloperToolsTest.GuardedStruct.ValidatorDeriveTest do

{:error,
[
%{message: "No, never", field: :changed, action: :validator},
%{message: "there is an Error", field: :global, action: :main_validator}
%{message: "No, never", field: :changed, action: :validator}
]} = assert TestAuthStruct.builder(%{changed: 555_555})

{:error,
[
%{message: "there is an Error", field: :global, action: :main_validator}
]} = assert TestAuthStruct.builder(%{changed: "https://github.com/mishka-group", action: 25})
end

test "key entries inside derive when derives used" do
Expand Down

0 comments on commit 5beacb5

Please sign in to comment.