Skip to content

Commit

Permalink
allow a setter_fun in schema validator
Browse files Browse the repository at this point in the history
allows to set values during validation
  • Loading branch information
lazedo authored and andreineculau committed May 22, 2017
1 parent 459b932 commit 79b87b9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/jesse.erl
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,20 @@
| ?not_found
).

-type setter_fun() :: fun(( jesse_json_path:path()
, json_term()
, json_term()
) -> json_term())
| undefined.

-type option() :: {allowed_errors, allowed_errors()}
| {default_schema_ver, schema_ver()}
| {error_handler, error_handler()}
| {external_validator, external_validator()}
| {meta_schema_ver, schema_ver()}
| {parser_fun, parser_fun()}
| {schema_loader_fun, schema_loader_fun()}.
| {schema_loader_fun, schema_loader_fun()}
| {setter_fun, setter_fun()}.

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

Expand Down
5 changes: 3 additions & 2 deletions src/jesse_schema_validator.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@
, Options :: [{Key :: atom(), Data :: any()}]
) -> {ok, jesse:json_term()}
| no_return().
validate(JsonSchema, Value, Options) ->
validate(JsonSchema, Value, Options0) ->
Options = [{with_value, Value} | proplists:delete(with_value, Options0)],
State = jesse_state:new(JsonSchema, Options),
NewState = validate_with_state(JsonSchema, Value, State),
{result(NewState), Value}.
{result(NewState), jesse_state:get_current_value(NewState)}.

%% @doc Validates json `Data' against `JsonSchema' with `State'.
%% If the given json is valid, then the latest state is returned to the caller,
Expand Down
24 changes: 24 additions & 0 deletions src/jesse_state.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
-export([ add_to_path/2
, get_allowed_errors/1
, get_external_validator/1
, get_current_value/1
, get_current_path/1
, get_current_schema/1
, get_current_schema_id/1
Expand All @@ -37,6 +38,7 @@
, remove_last_from_path/1
, set_allowed_errors/2
, set_current_schema/2
, set_value/3
, set_error_list/2
, resolve_ref/2
, undo_resolve_ref/2
Expand All @@ -55,13 +57,15 @@
, { allowed_errors :: jesse:allowed_errors()
, current_path :: current_path()
, current_schema :: jesse:schema()
, current_value :: jesse:json_term()
, default_schema_ver :: jesse:schema_ver()
, error_handler :: jesse:error_handler()
, error_list :: jesse:error_list()
, external_validator :: jesse:external_validator()
, id :: jesse:schema_id()
, root_schema :: jesse:schema()
, schema_loader_fun :: jesse:schema_loader_fun()
, setter_fun :: jesse:setter_fun()
}
).

Expand Down Expand Up @@ -146,6 +150,9 @@ new(JsonSchema, Options) ->
, Options
, ?default_schema_loader_fun
),
SetterFun = proplists:get_value( setter_fun
, Options
),
NewState = #state{ root_schema = JsonSchema
, current_path = []
, allowed_errors = AllowedErrors
Expand All @@ -154,6 +161,7 @@ new(JsonSchema, Options) ->
, default_schema_ver = DefaultSchemaVer
, schema_loader_fun = LoaderFun
, external_validator = ExternalValidator
, setter_fun = SetterFun
},
set_current_schema(NewState, JsonSchema).

Expand Down Expand Up @@ -392,3 +400,19 @@ load_schema(#state{schema_loader_fun = LoaderFun}, SchemaURI) ->
%% @private
get_external_validator(#state{external_validator = Fun}) ->
Fun.

%% @doc Getter for `current_value'.
-spec get_current_value(State :: state()) -> jesse:json_term().
get_current_value(#state{current_value = Value}) ->
Value.

-spec set_value(State :: state(), jesse:path(), jesse:json_term()) -> state().
set_value(#state{setter_fun = undefined}=State, _Path, _Value) -> State;
set_value(#state{current_value = undefined}=State, _Path, _Value) -> State;
set_value( #state{ setter_fun = Setter
, current_value = Value
} = State
, Path
, NewValue
) ->
State#state{current_value = Setter(Path, NewValue, Value)}.

0 comments on commit 79b87b9

Please sign in to comment.