Skip to content

Commit 8d23b53

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 8d23b53

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

src/jesse_state.erl

Lines changed: 9 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_extra_validator/1
2930
, get_current_path/1
3031
, get_current_schema/1
3132
, get_current_schema_id/1
@@ -49,6 +50,7 @@
4950
%% Includes
5051
-include("jesse_schema_validator.hrl").
5152

53+
-type extra_validator() :: fun((jesse:json_term(), state()) -> state()) | undefined.
5254
%% Internal datastructures
5355
-record( state
5456
, { root_schema :: jesse:json_term()
@@ -68,6 +70,7 @@
6870
jesse:json_term() |
6971
?not_found
7072
)
73+
, extra_validator :: extra_validator()
7174
, id :: http_uri:uri() | 'undefined'
7275
}
7376
).
@@ -148,13 +151,17 @@ new(JsonSchema, Options) ->
148151
, Options
149152
, ?default_schema_loader_fun
150153
),
154+
ExtraValidator = proplists:get_value( extra_validator
155+
, Options
156+
),
151157
NewState = #state{ root_schema = JsonSchema
152158
, current_path = []
153159
, allowed_errors = AllowedErrors
154160
, error_list = []
155161
, error_handler = ErrorHandler
156162
, default_schema_ver = DefaultSchemaVer
157163
, schema_loader_fun = LoaderFun
164+
, extra_validator = ExtraValidator
158165
},
159166
set_current_schema(NewState, JsonSchema).
160167

@@ -385,3 +392,5 @@ load_schema(#state{schema_loader_fun = LoaderFun}, SchemaURI) ->
385392
%% io:format("load_schema: ~p\n", [{_C, _E, erlang:get_stacktrace()}]),
386393
?not_found
387394
end.
395+
396+
get_extra_validator(#state{extra_validator=Fun}) -> Fun.

src/jesse_validator_draft3.erl

Lines changed: 11 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_error.
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+
check_external_validation(Value, State);
217218
check_value(Value, [_Attr | Attrs], State) ->
218219
check_value(Value, Attrs, State).
219220

@@ -1031,3 +1032,10 @@ 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+
check_external_validation(Value, State) ->
1038+
case jesse_state:get_extra_validator(State) of
1039+
undefined -> State;
1040+
Fun -> Fun(Value, State)
1041+
end.

src/jesse_validator_draft4.erl

Lines changed: 10 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_error.
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+
check_external_validation(Value, State);
254255
check_value(Value, [_Attr | Attrs], State) ->
255256
check_value(Value, Attrs, State).
256257

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

0 commit comments

Comments
 (0)