Merge pull request #11526 from paulozulato/fix-validator-return-msg

fix: flatten error message on resource validator
This commit is contained in:
Paulo Zulato 2023-08-25 17:27:31 -03:00 committed by GitHub
commit 2bb415f897
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 3 deletions

View File

@ -28,10 +28,18 @@ max(Type, Max) ->
min(Type, Min) ->
limit(Type, '>=', Min).
not_empty(ErrMsg) ->
not_empty(ErrMsg0) ->
ErrMsg =
try
lists:flatten(ErrMsg0)
catch
_:_ ->
ErrMsg0
end,
fun
(undefined) -> {error, ErrMsg};
(<<>>) -> {error, ErrMsg};
("") -> {error, ErrMsg};
(_) -> ok
end.
@ -50,7 +58,8 @@ len(string) -> fun string:length/1;
len(_Type) -> fun(Val) -> Val end.
err_limit({Type, {Op, Expected}, {got, Got}}) ->
io_lib:format("Expect the ~ts value ~ts ~p but got: ~p", [Type, Op, Expected, Got]).
Msg = io_lib:format("Expect the ~ts value ~ts ~p but got: ~p", [Type, Op, Expected, Got]),
lists:flatten(Msg).
return(true, _) -> ok;
return(false, Error) -> {error, Error}.

View File

@ -1121,10 +1121,58 @@ t_create_dry_run_local_failed(_) ->
).
t_test_func(_) ->
IsErrorMsgPlainString = fun({error, Msg}) -> io_lib:printable_list(Msg) end,
?assertEqual(ok, erlang:apply(emqx_resource_validator:not_empty("not_empty"), [<<"someval">>])),
?assertEqual(ok, erlang:apply(emqx_resource_validator:min(int, 3), [4])),
?assertEqual(ok, erlang:apply(emqx_resource_validator:max(array, 10), [[a, b, c, d]])),
?assertEqual(ok, erlang:apply(emqx_resource_validator:max(string, 10), ["less10"])).
?assertEqual(ok, erlang:apply(emqx_resource_validator:max(string, 10), ["less10"])),
?assertEqual(
true, IsErrorMsgPlainString(erlang:apply(emqx_resource_validator:min(int, 66), [42]))
),
?assertEqual(
true, IsErrorMsgPlainString(erlang:apply(emqx_resource_validator:max(int, 42), [66]))
),
?assertEqual(
true, IsErrorMsgPlainString(erlang:apply(emqx_resource_validator:min(array, 3), [[1, 2]]))
),
?assertEqual(
true,
IsErrorMsgPlainString(erlang:apply(emqx_resource_validator:max(array, 3), [[1, 2, 3, 4]]))
),
?assertEqual(
true, IsErrorMsgPlainString(erlang:apply(emqx_resource_validator:min(string, 3), ["1"]))
),
?assertEqual(
true, IsErrorMsgPlainString(erlang:apply(emqx_resource_validator:max(string, 3), ["1234"]))
),
NestedMsg = io_lib:format("The answer: ~p", [42]),
ExpectedMsg = "The answer: 42",
BinMsg = <<"The answer: 42">>,
MapMsg = #{question => "The question", answer => 42},
?assertEqual(
{error, ExpectedMsg},
erlang:apply(emqx_resource_validator:not_empty(NestedMsg), [""])
),
?assertEqual(
{error, ExpectedMsg},
erlang:apply(emqx_resource_validator:not_empty(NestedMsg), [<<>>])
),
?assertEqual(
{error, ExpectedMsg},
erlang:apply(emqx_resource_validator:not_empty(NestedMsg), [undefined])
),
?assertEqual(
{error, ExpectedMsg},
erlang:apply(emqx_resource_validator:not_empty(NestedMsg), [undefined])
),
?assertEqual(
{error, BinMsg},
erlang:apply(emqx_resource_validator:not_empty(BinMsg), [undefined])
),
?assertEqual(
{error, MapMsg},
erlang:apply(emqx_resource_validator:not_empty(MapMsg), [""])
).
t_reset_metrics(_) ->
{ok, _} = emqx_resource:create(