Skip to content

Commit 79b87b9

Browse files
lazedoandreineculau
authored andcommitted
allow a setter_fun in schema validator
allows to set values during validation
1 parent 459b932 commit 79b87b9

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

src/jesse.erl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,20 @@
8888
| ?not_found
8989
).
9090

91+
-type setter_fun() :: fun(( jesse_json_path:path()
92+
, json_term()
93+
, json_term()
94+
) -> json_term())
95+
| undefined.
96+
9197
-type option() :: {allowed_errors, allowed_errors()}
9298
| {default_schema_ver, schema_ver()}
9399
| {error_handler, error_handler()}
94100
| {external_validator, external_validator()}
95101
| {meta_schema_ver, schema_ver()}
96102
| {parser_fun, parser_fun()}
97-
| {schema_loader_fun, schema_loader_fun()}.
103+
| {schema_loader_fun, schema_loader_fun()}
104+
| {setter_fun, setter_fun()}.
98105

99106
-type options() :: [option()].
100107

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: 24 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_external_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
@@ -55,13 +57,15 @@
5557
, { allowed_errors :: jesse:allowed_errors()
5658
, current_path :: current_path()
5759
, current_schema :: jesse:schema()
60+
, current_value :: jesse:json_term()
5861
, default_schema_ver :: jesse:schema_ver()
5962
, error_handler :: jesse:error_handler()
6063
, error_list :: jesse:error_list()
6164
, external_validator :: jesse:external_validator()
6265
, id :: jesse:schema_id()
6366
, root_schema :: jesse:schema()
6467
, schema_loader_fun :: jesse:schema_loader_fun()
68+
, setter_fun :: jesse:setter_fun()
6569
}
6670
).
6771

@@ -146,6 +150,9 @@ new(JsonSchema, Options) ->
146150
, Options
147151
, ?default_schema_loader_fun
148152
),
153+
SetterFun = proplists:get_value( setter_fun
154+
, Options
155+
),
149156
NewState = #state{ root_schema = JsonSchema
150157
, current_path = []
151158
, allowed_errors = AllowedErrors
@@ -154,6 +161,7 @@ new(JsonSchema, Options) ->
154161
, default_schema_ver = DefaultSchemaVer
155162
, schema_loader_fun = LoaderFun
156163
, external_validator = ExternalValidator
164+
, setter_fun = SetterFun
157165
},
158166
set_current_schema(NewState, JsonSchema).
159167

@@ -392,3 +400,19 @@ load_schema(#state{schema_loader_fun = LoaderFun}, SchemaURI) ->
392400
%% @private
393401
get_external_validator(#state{external_validator = Fun}) ->
394402
Fun.
403+
404+
%% @doc Getter for `current_value'.
405+
-spec get_current_value(State :: state()) -> jesse:json_term().
406+
get_current_value(#state{current_value = Value}) ->
407+
Value.
408+
409+
-spec set_value(State :: state(), jesse:path(), jesse:json_term()) -> state().
410+
set_value(#state{setter_fun = undefined}=State, _Path, _Value) -> State;
411+
set_value(#state{current_value = undefined}=State, _Path, _Value) -> State;
412+
set_value( #state{ setter_fun = Setter
413+
, current_value = Value
414+
} = State
415+
, Path
416+
, NewValue
417+
) ->
418+
State#state{current_value = Setter(Path, NewValue, Value)}.

0 commit comments

Comments
 (0)