Skip to content

Commit 07bafff

Browse files
lazedoandreineculau
authored andcommitted
allow extra validator
this will extra validations for schema elements example : lookup some value in a database an provide an error
1 parent ef226b2 commit 07bafff

4 files changed

+39
-7
lines changed

src/jesse_schema_validator.hrl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
-define(not_one_schema_valid, 'not_one_schema_valid').
115115
-define(not_schema_valid, 'not_schema_valid').
116116
-define(wrong_not_schema, 'wrong_not_schema').
117-
-define(external_error, 'external_error').
117+
-define(external, 'external').
118118

119119
%%
120120
-define(not_found, not_found).

src/jesse_state.erl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
%% API
2727
-export([ add_to_path/2
2828
, get_allowed_errors/1
29+
, get_external_validator/1
2930
, get_current_path/1
3031
, get_current_schema/1
3132
, get_current_schema_id/1
@@ -68,10 +69,14 @@
6869
jesse:json_term() |
6970
?not_found
7071
)
72+
, external_validator :: external_validator()
7173
, id :: http_uri:uri() | 'undefined'
7274
}
7375
).
7476

77+
-type external_validator() :: fun((jesse:json_term(), state()) -> state())
78+
| undefined.
79+
7580
-opaque state() :: #state{}.
7681

7782
%%% API
@@ -148,13 +153,17 @@ new(JsonSchema, Options) ->
148153
, Options
149154
, ?default_schema_loader_fun
150155
),
156+
ExternalValidator = proplists:get_value( external_validator
157+
, Options
158+
),
151159
NewState = #state{ root_schema = JsonSchema
152160
, current_path = []
153161
, allowed_errors = AllowedErrors
154162
, error_list = []
155163
, error_handler = ErrorHandler
156164
, default_schema_ver = DefaultSchemaVer
157165
, schema_loader_fun = LoaderFun
166+
, external_validator = ExternalValidator
158167
},
159168
set_current_schema(NewState, JsonSchema).
160169

@@ -385,3 +394,7 @@ load_schema(#state{schema_loader_fun = LoaderFun}, SchemaURI) ->
385394
%% io:format("load_schema: ~p\n", [{_C, _E, erlang:get_stacktrace()}]),
386395
?not_found
387396
end.
397+
398+
%% @private
399+
get_external_validator(#state{external_validator = Fun}) ->
400+
Fun.

src/jesse_validator_draft3.erl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
| ?not_in_range
4949
| ?wrong_length
5050
| ?wrong_size
51-
| ?wrong_type.
51+
| ?wrong_type
52+
| ?external.
5253

5354
-type data_error_type() :: data_error()
5455
| {data_error(), binary()}.
@@ -209,11 +210,11 @@ check_value(Value, [{?DISALLOW, Disallow} | Attrs], State) ->
209210
check_value(Value, [{?EXTENDS, Extends} | Attrs], State) ->
210211
NewState = check_extends(Value, Extends, State),
211212
check_value(Value, Attrs, NewState);
212-
check_value(_Value, [], State) ->
213-
State;
214213
check_value(Value, [{?REF, RefSchemaURI} | Attrs], State) ->
215214
NewState = validate_ref(Value, RefSchemaURI, State),
216215
check_value(Value, Attrs, NewState);
216+
check_value(Value, [], State) ->
217+
maybe_external_check_value(Value, State);
217218
check_value(Value, [_Attr | Attrs], State) ->
218219
check_value(Value, Attrs, State).
219220

@@ -1031,3 +1032,12 @@ add_to_path(State, Property) ->
10311032
%% @private
10321033
remove_last_from_path(State) ->
10331034
jesse_state:remove_last_from_path(State).
1035+
1036+
%% @private
1037+
maybe_external_check_value(Value, State) ->
1038+
case jesse_state:get_external_validator(State) of
1039+
undefined ->
1040+
State;
1041+
Fun ->
1042+
Fun(Value, State)
1043+
end.

src/jesse_validator_draft4.erl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@
6565
| ?too_many_properties
6666
| ?wrong_length
6767
| ?wrong_size
68-
| ?wrong_type.
68+
| ?wrong_type
69+
| ?external.
6970

7071
-type data_error_type() :: data_error()
7172
| {data_error(), binary()}.
@@ -246,11 +247,11 @@ check_value(Value, [{?ONEOF, Schemas} | Attrs], State) ->
246247
check_value(Value, [{?NOT, Schema} | Attrs], State) ->
247248
NewState = check_not(Value, Schema, State),
248249
check_value(Value, Attrs, NewState);
249-
check_value(_Value, [], State) ->
250-
State;
251250
check_value(Value, [{?REF, RefSchemaURI} | Attrs], State) ->
252251
NewState = validate_ref(Value, RefSchemaURI, State),
253252
check_value(Value, Attrs, NewState);
253+
check_value(Value, [], State) ->
254+
maybe_external_check_value(Value, State);
254255
check_value(Value, [_Attr | Attrs], State) ->
255256
check_value(Value, Attrs, State).
256257

@@ -1362,3 +1363,11 @@ valid_datetime(DateTimeBin) ->
13621363
_ ->
13631364
false
13641365
end.
1366+
1367+
maybe_external_check_value(Value, State) ->
1368+
case jesse_state:get_external_validator(State) of
1369+
undefined ->
1370+
State;
1371+
Fun ->
1372+
Fun(Value, State)
1373+
end.

0 commit comments

Comments
 (0)