Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: enable getting root cause for extra_validation errors #20

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/eon_type.erl
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,10 @@ typecheck(#spec{term=Term0, type=Type, p_have=P_have}) ->
true ->
case Type:extra_validation(Term, P_have) of
ok -> Term;
{error, Reason} -> {error, {untypable, Reason}}
%% Since the extra_validation is typically used for checks
%% involving several terms, we return a reason given by
%% the validation code instead of a term name.
{error, Reason} -> {error, {untypable, [{x, Type, x, Reason, []}]}}
cdyb-kivra marked this conversation as resolved.
Show resolved Hide resolved
end;
false ->
Term
Expand Down
6 changes: 6 additions & 0 deletions src/eon_type_error.erl
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,10 @@ get_root_cause__list_type__test() ->
{error, Rsn} = eon_type:check_term([<<"foo">>, 42], test_strings),
?assertEqual({42, test_string}, get_root_cause(Rsn)).

get_root_cause__extra_validation__test() ->
{error, Rsn} = eon_type:check_term([{<<"foo">>, 17}], test_rec_extra),
?assertMatch({nope, test_rec_extra}, get_root_cause(Rsn)),
{error, Rsn2} = eon_type:check_term([{<<"foo">>, 117}], test_rec_extra),
?assertMatch({117, test_prim}, get_root_cause(Rsn2)).

-endif.
15 changes: 15 additions & 0 deletions test/test_rec_extra.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-module(test_rec_extra).
-behaviour(eon_type_rec).
-include_lib("stdlib2/include/prelude.hrl").
-export([name/0, parameters/0, decl/2, extra_validation/2]).

name() -> some_obj.

parameters() -> [].

decl(_Obj, _Params) ->
[ <<"foo">>, {test_prim, [min,0, max,100]}
].

extra_validation(_Term, _Params) ->
{error, nope}.
Loading