diff --git a/rebar.config b/rebar.config index d24d90d..b795237 100644 --- a/rebar.config +++ b/rebar.config @@ -13,7 +13,7 @@ ]}. {deps, [ - {nova, ".*", {git, "https://github.com/novaframework/nova.git", {branch, "master"}}} + {nova_json_schemas, {git, "https://github.com/novaframework/nova_json_schemas", {branch, "master"}}} ]}. diff --git a/rebar.config.script b/rebar.config.script index 41860d1..a07c545 100644 --- a/rebar.config.script +++ b/rebar.config.script @@ -2,17 +2,14 @@ Conf0 = CONFIG, NovaBranch = list_to_binary(os:getenv("NOVA_BRANCH", "master")), -Deps = case NovaBranch of - <<"master">> -> {deps, [ - {nova, ".*", {git, "https://github.com/novaframework/nova.git", {branch, "master"}}} - ]}; - <<"refs/tags/", Rest/binary>> -> - {deps, [{nova, ".*", {git, "https://github.com/novaframework/nova.git", {tag, binary_to_list(Rest)}}}]}; - NovaBranch -> {deps, [ - {nova, ".*", {git, "https://github.com/novaframework/nova.git", {branch, binary_to_list(NovaBranch)}}} - ]} +Deps = proplists:get_value(deps, Conf0), +NovaDeps = case NovaBranch of + <<"master">> -> {nova, {git, "https://github.com/novaframework/nova.git", {branch, "master"}}}; + <<"refs/tags/", Rest/binary>> -> {nova, {git, "https://github.com/novaframework/nova.git", {tag, binary_to_list(Rest)}}}; + NovaBranch -> {nova, {git, "https://github.com/novaframework/nova.git", {branch, binary_to_list(NovaBranch)}}} end, -Conf1 = proplists:delete(deps, Conf0), +Deps2 = {deps, [NovaDeps|Deps]}, -[Deps|Conf1]. +Conf1 = proplists:delete(deps, Conf0), +[Deps2|Conf1]. diff --git a/src/nova_request_app.app.src b/src/nova_request_app.app.src index 8c39300..26756d9 100644 --- a/src/nova_request_app.app.src +++ b/src/nova_request_app.app.src @@ -9,7 +9,8 @@ [ kernel, stdlib, - nova + nova, + nova_json_schemas ]}, {env,[]}, {modules, []}, diff --git a/src/nova_request_app_router.erl b/src/nova_request_app_router.erl index 202f31d..ddce9f1 100644 --- a/src/nova_request_app_router.erl +++ b/src/nova_request_app_router.erl @@ -79,6 +79,16 @@ with_fun() -> security => false, routes => [ {"/", fun nova_request_app_main_controller:get_user/1, #{methods => [get]}}, - {"/", fun nova_request_app_main_controller:delete_user/1, #{methods => [delete]}} - - ]}]. \ No newline at end of file + {"/", fun nova_request_app_main_controller:delete_user/1, #{methods => [delete]}}] + }, + #{prefix => "/fun/json_schemas", + security => false, + plugins => [ + {pre_request, nova_request_plugin, #{decode_json_body => true}}, + {pre_request, nova_json_schemas, #{render_errors => true}} + ], + routes => [ + {"/", fun(_) -> {status, 200} end, + #{methods => [post], extra_state => #{ json_schema => "./schemas/sample_json_schema.json" }}}] + } +]. diff --git a/test/nova_request_app_fun_SUITE.erl b/test/nova_request_app_fun_SUITE.erl index 11e714d..298b3d7 100644 --- a/test/nova_request_app_fun_SUITE.erl +++ b/test/nova_request_app_fun_SUITE.erl @@ -125,7 +125,10 @@ all() -> fallback, view_with_ok, view_with_view, - heartbeat]. + heartbeat, + json_schema_ok, + json_schema_validation_error +]. %%-------------------------------------------------------------------- %% @spec TestCase(Config0) -> %% ok | exit() | {skip,Reason} | {comment,Comment} | @@ -239,6 +242,17 @@ heartbeat(_) -> Path = [?BASEPATH, <<"heartbeat">>], #{status := {200, _}} = jhn_shttpc:get(Path, opts(json_get)). +json_schema_ok(_) -> + Path = [?BASEPATH, <<"json_schemas">>], + Json = #{<<"a_string">> => <<"whatever">>}, + #{status := {200, _}} = jhn_shttpc:post(Path, encode(Json), opts(json_post)). +json_schema_validation_error(_) -> + Path = [?BASEPATH, <<"json_schemas">>], + Json = #{<<"a_string">> => 5}, + #{status := {400, _}, body := Body} = jhn_shttpc:post(Path, encode(Json), opts(json_post)), + ct:pal("Body: ~p~njson:decode(Body): ~p~n", [Body, json:decode(Body)]), + [#{<<"error_type">> := <<"wrong_type">>}] = json:decode(Body). + ws(_) -> Wohoo = <<"wohoo">>, websocket([<<"/ws/">>, Wohoo], <<>>), @@ -303,4 +317,4 @@ encode(Json) -> decode(Json) -> {ok, Result} = thoas:decode(Json), - Result. \ No newline at end of file + Result.