Skip to content

Commit

Permalink
feat(nomasystems#59): Handle ndto resp & return map on 400
Browse files Browse the repository at this point in the history
Handles new ndto (0.3.0) response format and return a simple
map with the error description & schema path on validation failure
Issue nomasystems#59
  • Loading branch information
LoisSotoLopez committed Feb 9, 2024
1 parent b5bfbf8 commit eb4cef1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
4 changes: 2 additions & 2 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

{deps, [
{elli, {git, "[email protected]:elli-lib/elli.git", {branch, "main"}}},
{ndto, {git, "[email protected]:nomasystems/ndto.git", {tag, "0.2.0"}}},
{ndto, {git, "[email protected]:nomasystems/ndto.git", {tag, "0.3.0"}}},
{njson, {git, "[email protected]:nomasystems/njson.git", {branch, "main"}}}
]}.

{plugins, [
{rebar3_ndto, {git, "[email protected]:nomasystems/rebar3_ndto.git", {tag, "0.2.0"}}}
{rebar3_ndto, {git, "[email protected]:nomasystems/rebar3_ndto.git", {branch, "build/0-3-0"}}}
]}.
{ndto, [
{specs, [
Expand Down
2 changes: 1 addition & 1 deletion rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
1},
{<<"ndto">>,
{git,"[email protected]:nomasystems/ndto.git",
{ref,"ecb52baafa44eba1d58661e4658e34b011aeb58c"}},
{ref,"5cdde58bd80bd6b1d12b553534baaf2537483519"}},
0},
{<<"njson">>,
{git,"[email protected]:nomasystems/njson.git",
Expand Down
71 changes: 51 additions & 20 deletions src/erf_router.erl
Original file line number Diff line number Diff line change
Expand Up @@ -205,23 +205,6 @@ apply_postprocess_middlewares(Request, RawResponse, [Middleware | Rest]) ->
apply_postprocess_middlewares(Request, Response, Rest)
end.

-spec chain_conditions(FunCalls, Operator) -> Result when
FunCalls :: [erl_syntax:syntaxTree()],
Operator :: 'andalso',
Result :: erl_syntax:syntaxTree().
chain_conditions(FunCalls, 'andalso' = Operator) ->
chain_conditions(FunCalls, Operator, erl_syntax:atom(true)).

chain_conditions([], _Operator, Acc) ->
Acc;
chain_conditions([FunCall | Rest], Operator, Acc) ->
NewAcc = erl_syntax:infix_expr(
Acc,
erl_syntax:operator(Operator),
FunCall
),
chain_conditions(Rest, Operator, NewAcc).

-spec handle_ast(API, Opts) -> Result when
API :: erf:api(),
Opts :: generator_opts(),
Expand Down Expand Up @@ -368,14 +351,44 @@ handle_ast(API, #{callback := Callback} = Opts) ->
]
),
erl_syntax:clause(
[erl_syntax:atom(false)],
[erl_syntax:tuple([
erl_syntax:atom('false'),
erl_syntax:tuple([
erl_syntax:tuple([
erl_syntax:variable('SchemaPath'),
erl_syntax:variable('Description')
]),
erl_syntax:variable('_ConditionIndex')
])
])],
none,
[
erl_syntax:tuple(
[
erl_syntax:integer(400),
erl_syntax:list([]),
erl_syntax:atom(undefined)
erl_syntax:map_expr([
erl_syntax:map_field_assoc(
erl_syntax:binary([
erl_syntax:binary_field(
erl_syntax:string("schema_path")
)
]),
erl_syntax:application(
erl_syntax:atom(erlang),
erl_syntax:atom(atom_to_binary),
[erl_syntax:variable('SchemaPath')]
)
),
erl_syntax:map_field_assoc(
erl_syntax:binary([
erl_syntax:binary_field(
erl_syntax:string("description")
)
]),
erl_syntax:variable('Description')
)
])
]
)
]
Expand Down Expand Up @@ -715,7 +728,25 @@ is_valid_request(RawParameters, Request) ->
end,
FilteredParameters
),
chain_conditions([RequestBody | Parameters], 'andalso').
erl_syntax:application(
erl_syntax:atom('ndto_validation'),
erl_syntax:atom('andalso'),
[
erl_syntax:list(
[ erl_syntax:tuple([
erl_syntax:fun_expr([
erl_syntax:clause(
none,
[
Condition
]
)
]),
erl_syntax:list([])
]) || Condition <- [RequestBody | Parameters]]
)
]
).

-spec load_binary(ModuleName, Bin) -> Result when
ModuleName :: atom(),
Expand Down

0 comments on commit eb4cef1

Please sign in to comment.