Skip to content

Commit 910f2d4

Browse files
lazedoandreineculau
authored andcommitted
allow a setter_fun in schema validator
allows to set values during validation
1 parent da25c40 commit 910f2d4

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-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: 25 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
@@ -63,10 +65,17 @@
6365
, default_schema_ver :: schema_ver()
6466
, schema_loader_fun :: schema_loader_fun()
6567
, external_validator :: external_validator()
68+
, setter_fun :: setter_fun()
6669
, id :: schema_id()
6770
}
6871
).
6972

73+
-type setter_fun() :: fun(( jesse:json_path()
74+
, jesse:json_term()
75+
, jesse:json_term()
76+
) -> jesse:json_term())
77+
| undefined.
78+
7079
-opaque options() :: #options{}.
7180
-opaque state() :: #state{}.
7281

@@ -145,6 +154,9 @@ new(JsonSchema, Options) ->
145154
, Options
146155
, ?default_schema_loader_fun
147156
),
157+
SetterFun = proplists:get_value( setter_fun
158+
, Options
159+
),
148160
NewState = #state{ root_schema = JsonSchema
149161
, current_path = []
150162
, allowed_errors = AllowedErrors
@@ -153,6 +165,7 @@ new(JsonSchema, Options) ->
153165
, default_schema_ver = DefaultSchemaVer
154166
, schema_loader_fun = LoaderFun
155167
, external_validator = ExternalValidator
168+
, setter_fun = SetterFun
156169
},
157170
set_current_schema(NewState, JsonSchema).
158171

@@ -389,3 +402,15 @@ load_schema(#state{schema_loader_fun = LoaderFun}, SchemaURI) ->
389402
%% @private
390403
get_external_validator(#state{external_validator = Fun}) ->
391404
Fun.
405+
406+
%% @doc Getter for `current_value'.
407+
-spec get_current_value(State :: state()) -> jesse:json_term().
408+
get_current_value(#state{current_value = Value}) -> Value.
409+
410+
-spec set_value(State :: state(), jesse:path(), jesse:json_term()) -> state().
411+
set_value(#state{setter_fun=undefined}=State, _Path, _Value) -> State;
412+
set_value(#state{current_value=undefined}=State, _Path, _Value) -> State;
413+
set_value(#state{setter_fun=Setter
414+
,current_value=Value
415+
}=State, Path, NewValue) ->
416+
State#state{current_value = Setter(Path, NewValue, Value)}.

0 commit comments

Comments
 (0)