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

A bit more informative errors of constraints #776

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/cowboy_constraints.erl
Expand Up @@ -16,10 +16,12 @@

-export([validate/2]).

-type dirty_constraint_result() :: true | {true, any()} | false.
-type constraint_result() :: true | {true, any()} | {false, fun() | atom()}.
-type constraint() :: int | nonempty | fun().
-export_type([constraint/0]).
-export_type([constraint/0, constraint_result/0]).

-spec validate(binary(), [constraint()]) -> true | {true, any()} | false.
-spec validate(binary(), [constraint()]) -> constraint_result().
validate(Value, [Constraint]) ->
apply_constraint(Value, Constraint);
validate(Value, Constraints) when is_list(Constraints) ->
Expand All @@ -37,17 +39,21 @@ validate_list(Value, [Constraint|Tail], State) ->
validate_list(Value, Tail, State);
{true, Value2} ->
validate_list(Value2, Tail, modified);
false ->
false
Else ->
Else
end.

%% @todo {int, From, To}, etc.
apply_constraint(Value, int) ->
int(Value);
apply_constraint(Value, nonempty) ->
nonempty(Value);
apply_constraint(Value, int = F) ->
prepare_result(int(Value), F);
apply_constraint(Value, nonempty = F) ->
prepare_result(nonempty(Value), F);
apply_constraint(Value, F) when is_function(F) ->
F(Value).
prepare_result(F(Value), F).

-spec prepare_result(dirty_constraint_result(), fun() | atom()) -> constraint_result().
prepare_result(False, F) when False =:= false -> {false, F};
prepare_result(True, _F) -> True.

%% Constraint functions.

Expand Down
41 changes: 27 additions & 14 deletions src/cowboy_req.erl
Expand Up @@ -1250,27 +1250,40 @@ kvlist_to_map(Keys, [{Key, Value}|Tail], Map) ->
%% Loop through fields, if value is missing and no default, crash;
%% else if value is missing and has a default, set default;
%% otherwise apply constraints. If constraint fails, crash.
filter([], Map) ->
Map;
filter([{Key, Constraints}|Tail], Map) ->
filter_constraints(Tail, Map, Key, maps:get(Key, Map), Constraints);
filter([{Key, Constraints, Default}|Tail], Map) ->
case maps:find(Key, Map) of
filter(Fields, InputM) ->
filter(Fields, InputM, #{}).

filter([], InputM, ErrM) ->
case maps:size(ErrM) of
0 -> InputM;
_ -> throw({badmatch, ErrM})
end;
filter([{Key, Constraints}|Tail], InputM, ErrM) ->
case maps:find(Key, InputM) of
{ok, Val} ->
filter_constraints(Tail, InputM, Key, Val, Constraints, ErrM);
_ ->
filter(Tail, InputM, maps:put(Key, missing, ErrM))
end;
filter([{Key, Constraints, Default}|Tail], InputM, ErrM) ->
case maps:find(Key, InputM) of
{ok, Value} ->
filter_constraints(Tail, Map, Key, Value, Constraints);
filter_constraints(Tail, InputM, Key, Value, Constraints, ErrM);
error ->
filter(Tail, maps:put(Key, Default, Map))
filter(Tail, maps:put(Key, Default, InputM), ErrM)
end;
filter([Key|Tail], Map) ->
true = maps:is_key(Key, Map),
filter(Tail, Map).
filter([Key|Tail], InputM, ErrM) ->
true = maps:is_key(Key, InputM),
filter(Tail, InputM, ErrM).

filter_constraints(Tail, Map, Key, Value, Constraints) ->
filter_constraints(Tail, InputM, Key, Value, Constraints, ErrM) ->
case cowboy_constraints:validate(Value, Constraints) of
true ->
filter(Tail, Map);
filter(Tail, InputM, ErrM);
{true, Value2} ->
filter(Tail, maps:put(Key, Value2, Map))
filter(Tail, maps:put(Key, Value2, InputM), ErrM);
{false, F} ->
filter(Tail, InputM, maps:put(Key, {invalid, F}, ErrM))
end.

%% Tests.
Expand Down
2 changes: 1 addition & 1 deletion src/cowboy_router.erl
Expand Up @@ -285,7 +285,7 @@ check_constraints([Field|Tail], Bindings) ->
Bindings2 = lists:keyreplace(Name, 1, Bindings,
{Name, Value2}),
check_constraints(Tail, Bindings2);
false ->
{false, _} ->
nomatch
end
end.
Expand Down