Skip to content

Commit 126cbdf

Browse files
lazedoandreineculau
authored andcommitted
allow a setter_fun in schema validator
allows to set values during validation
1 parent d71b0a2 commit 126cbdf

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/jesse_schema_validator.erl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@
4040
, Options :: [{Key :: atom(), Data :: any()}]
4141
) -> {ok, jesse:json_term()}
4242
| no_return().
43-
validate(JsonSchema, Value, Options) ->
43+
validate(JsonSchema, Value, Options0) ->
44+
Options = [{with_value, Value} | proplists:delete(with_value, Options0)],
4445
State = jesse_state:new(JsonSchema, Options),
4546
NewState = validate_with_state(JsonSchema, Value, State),
46-
{result(NewState), Value}.
47+
{result(NewState), jesse_state:get_current_value(NewState)}.
4748

4849
%% @doc Validates json `Data' against `JsonSchema' with `State'.
4950
%% If the given json is valid, then the latest state is returned to the caller,

src/jesse_state.erl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
-export([ add_to_path/2
2828
, get_allowed_errors/1
2929
, get_extra_validator/1
30+
, get_current_value/1
3031
, get_current_path/1
3132
, get_current_schema/1
3233
, get_current_schema_id/1
@@ -37,6 +38,7 @@
3738
, remove_last_from_path/1
3839
, set_allowed_errors/2
3940
, set_current_schema/2
41+
, set_value/3
4042
, set_error_list/2
4143
, resolve_ref/2
4244
, undo_resolve_ref/2
@@ -51,12 +53,15 @@
5153
-include("jesse_schema_validator.hrl").
5254

5355
-type extra_validator() :: fun((jesse:json_term(), state()) -> state()) | undefined.
56+
-type setter_fun() :: fun((jesse:json_path(), jesse:json_term(), jesse:json_term()) -> jesse:json_term()) | undefined.
57+
5458
%% Internal datastructures
5559
-record( state
5660
, { root_schema :: jesse:json_term()
5761
, current_schema :: jesse:json_term()
5862
, current_path :: [binary() | non_neg_integer()]
5963
%% current path in reversed order
64+
, current_value :: jesse:json_term()
6065
, allowed_errors :: non_neg_integer() | 'infinity'
6166
, error_list :: list()
6267
, error_handler :: fun(( jesse_error:error_reason()
@@ -71,6 +76,7 @@
7176
?not_found
7277
)
7378
, extra_validator :: extra_validator()
79+
, setter_fun :: setter_fun()
7480
, id :: http_uri:uri() | 'undefined'
7581
}
7682
).
@@ -154,6 +160,12 @@ new(JsonSchema, Options) ->
154160
ExtraValidator = proplists:get_value( extra_validator
155161
, Options
156162
),
163+
SetterFun = proplists:get_value( setter_fun
164+
, Options
165+
),
166+
Value = proplists:get_value( with_value
167+
, Options
168+
),
157169
NewState = #state{ root_schema = JsonSchema
158170
, current_path = []
159171
, allowed_errors = AllowedErrors
@@ -162,6 +174,8 @@ new(JsonSchema, Options) ->
162174
, default_schema_ver = DefaultSchemaVer
163175
, schema_loader_fun = LoaderFun
164176
, extra_validator = ExtraValidator
177+
, setter_fun = SetterFun
178+
, current_value = Value
165179
},
166180
set_current_schema(NewState, JsonSchema).
167181

@@ -393,4 +407,16 @@ load_schema(#state{schema_loader_fun = LoaderFun}, SchemaURI) ->
393407
?not_found
394408
end.
395409

410+
%% @doc Getter for `current_value'.
411+
-spec get_current_value(State :: state()) -> jesse:json_term().
412+
get_current_value(#state{current_value = Value}) -> Value.
413+
414+
-spec set_value(State :: state(), jesse:path(), jesse:json_term()) -> state().
415+
set_value(#state{setter_fun=undefined}=State, _Path, _Value) -> State;
416+
set_value(#state{current_value=undefined}=State, _Path, _Value) -> State;
417+
set_value(#state{setter_fun=Setter
418+
,current_value=Value
419+
}=State, Path, NewValue) ->
420+
State#state{current_value = Setter(Path, NewValue, Value)}.
421+
396422
get_extra_validator(#state{extra_validator=Fun}) -> Fun.

0 commit comments

Comments
 (0)