Skip to content

Commit 3d90161

Browse files
Allow passing options through to jesse for extra validation etc. (#5)
Co-authored-by: Richard Carlsson <[email protected]>
1 parent a8dbda2 commit 3d90161

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ routes(_Environment) ->
3535

3636
Then put `my_schema.jsons` that contains the schema in your `priv/schemas/` directory (json schemas is read related from priv-directory of your main nova application).
3737

38+
### Additional validation and other options to Jesse
39+
40+
The schema validation is performed by the Jesse library
41+
(https://github.com/for-GET/jesse). To perform additional custom
42+
validation, you can pass an `external_validator` option to Jesse via a
43+
`jesse_options` entry in the `extra_state`. Other options to Jesse are
44+
passed the same way:
45+
46+
``` erlang
47+
#{extra_state => #{json_schema => "...",
48+
jesse_options => [{external_validator, ValidatorFun}]
49+
}
50+
}
51+
```
52+
53+
The validation function takes a value and a Jesse state and should return a
54+
new state; see the Jesse documentation for details.
55+
3856

3957
## Report bugs and/or contribute
4058

src/nova_json_schemas.erl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ load_local_schemas() ->
3838
{ok, Req0 :: cowboy_req:req()}
3939
| {stop, Req0 :: cowboy_req:req()}
4040
| {error, Reason :: term()}.
41-
pre_request(Req = #{extra_state := #{json_schema := SchemaLocation}, json := JSON}, Options) ->
41+
pre_request(Req = #{extra_state := #{json_schema := SchemaLocation}=Extra, json := JSON}, Options) ->
42+
JesseOpts = maps:get(jesse_options, Extra, []),
4243
%% JSON have already been parsed so we can just continue with the validation
43-
case validate_json(SchemaLocation, JSON) of
44+
case validate_json(SchemaLocation, JSON, JesseOpts) of
4445
ok ->
4546
?LOG_DEBUG("Schema validation on JSON body successful"),
4647
{ok, Req};
@@ -96,17 +97,17 @@ post_request(Req, _Options) ->
9697
%%--------------------------------------------------------------------
9798
-spec plugin_info() ->
9899
{Title :: binary(), Version :: binary(), Author :: binary(), Description :: binary(), [
99-
{Key :: atom(), OptionDescription :: atom()}
100+
{Key :: atom(), OptionDescription :: binary()}
100101
]}.
101102
plugin_info() ->
102-
{<<"JSON schema plugin">>, <<"0.0.1">>, <<"Niclas Axelsson <[email protected]">>,
103+
{<<"JSON schema plugin">>, <<"0.0.2">>, <<"Niclas Axelsson <[email protected]">>,
103104
<<"Validating JSON with schemas">>, [
104105
{render_errors, <<"If this is set, validation-errors is returned to the requester">>}
105106
%% Options is specified as {Key, Description}
106107
]}.
107108

108-
validate_json(SchemaLocation, Json) ->
109-
case jesse:validate(SchemaLocation, Json) of
109+
validate_json(SchemaLocation, Json, JesseOpts) ->
110+
case jesse:validate(SchemaLocation, Json, JesseOpts) of
110111
{error, {database_error, _, schema_not_found}} ->
111112
%% Load the schema
112113
{ok, MainApp} = nova:get_main_app(),
@@ -116,7 +117,7 @@ validate_json(SchemaLocation, Json) ->
116117
JsonLib = nova:get_env(json_lib, thoas),
117118
{ok, Schema} = erlang:apply(JsonLib, decode, [Filecontent]),
118119
jesse:add_schema(SchemaLocation, Schema),
119-
validate_json(SchemaLocation, Json);
120+
validate_json(SchemaLocation, Json, JesseOpts);
120121
{error, ValidationError} ->
121122
{error, ValidationError};
122123
{ok, _} ->

0 commit comments

Comments
 (0)