Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for configurable and extendable validators #55

Open
wants to merge 1 commit into
base: no-external-validator
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ eunit:

.PHONY: ct
ct:
$(REBAR) ct skip_deps=true suites="jesse_tests_draft3,jesse_tests_draft4"
$(REBAR) ct skip_deps=true suites="jesse_tests_draft3,jesse_tests_draft4,jesse_tests_generic"

.PHONY: xref
xref:
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ the given schema), one should use 'default_schema_ver' option when call
a binary consisting a schema path,
i.e. <<"http://json-schema.org/draft-03/schema#">>.

It is also possible to specify a validator module to use via `validator` option.
This option supersedes the mechanism with the $schema property described above.
Custom validator module can be specified as well. Such module should implement
`jesse_schema_validator` behaviour.

## Validation errors

The validation functions `jesse:validate/2` and `jesse:validate_with_schema/2,3`
Expand Down
51 changes: 34 additions & 17 deletions src/jesse_schema_validator.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,31 @@ validate(JsonSchema, Value, Options) ->
, State :: jesse_state:state()
) -> jesse_state:state()
| no_return().
validate_with_state(JsonSchema, Value, State) ->
SchemaVer = get_schema_ver(JsonSchema, State),
select_and_run_validator(SchemaVer, JsonSchema, Value, State).
validate_with_state(JsonSchema0, Value, State) ->
Validator = select_validator(JsonSchema0, State),
JsonSchema = jesse_json_path:unwrap_value(JsonSchema0),
run_validator(Validator, Value, JsonSchema, State).


%%% Internal functions
%% @doc Gets validator from the state or else
%% selects an appropriate one by schema version.
%% @private
select_validator(JsonSchema, State) ->
case jesse_state:get_validator(State) of
undefined ->
select_validator_by_schema(get_schema_ver(JsonSchema, State), State);
Validator ->
Validator
end.

select_validator_by_schema(?json_schema_draft3, _) ->
jesse_validator_draft3;
select_validator_by_schema(?json_schema_draft4, _) ->
jesse_validator_draft4;
select_validator_by_schema(SchemaURI, State) ->
jesse_error:handle_schema_invalid({?schema_unsupported, SchemaURI}, State).

%% @doc Returns "$schema" property from `JsonSchema' if it is present,
%% otherwise the default schema version from `State' is returned.
%% @private
Expand All @@ -76,18 +96,15 @@ result(State) ->
_ -> throw(ErrorList)
end.

%% @doc Runs appropriate validator depending on schema version
%% it is called with.
%% @doc Goes through attributes of the given `JsonSchema' and
%% validates the `Value' against them calling `Validator'.
%% @private
select_and_run_validator(?json_schema_draft3, JsonSchema, Value, State) ->
jesse_validator_draft3:check_value( Value
, jesse_json_path:unwrap_value(JsonSchema)
, State
);
select_and_run_validator(?json_schema_draft4, JsonSchema, Value, State) ->
jesse_validator_draft4:check_value( Value
, jesse_json_path:unwrap_value(JsonSchema)
, State
);
select_and_run_validator(SchemaURI, _JsonSchema, _Value, State) ->
jesse_error:handle_schema_invalid({?schema_unsupported, SchemaURI}, State).
run_validator(_Validator, _Value, [], State) ->
State;
run_validator(Validator, Value, [Attr | Attrs], State0) ->
State = jesse_validator:check_value( Validator
, Value
, Attr
, State0
),
run_validator(Validator, Value, Attrs, State).
51 changes: 49 additions & 2 deletions src/jesse_state.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@
, get_current_schema/1
, get_current_schema_id/1
, get_default_schema_ver/1
, get_validator/1
, get_validator_state/1
, get_error_handler/1
, get_error_list/1
, new/2
, remove_last_from_path/1
, set_allowed_errors/2
, set_current_schema/2
, set_error_list/2
, set_validator_state/2
, resolve_ref/2
, undo_resolve_ref/2
, canonical_path/2
Expand All @@ -50,6 +53,8 @@
-include("jesse_schema_validator.hrl").

%% Internal datastructures
-type http_uri() :: string().

-record( state
, { allowed_errors :: jesse:allowed_errors()
, current_path :: current_path()
Expand All @@ -61,6 +66,8 @@
, id :: jesse:schema_id()
, root_schema :: jesse:schema()
, schema_loader_fun :: jesse:schema_loader_fun()
, validator :: module() | 'undefined'
, validator_state :: jesse_validator:state()
}
).

Expand Down Expand Up @@ -107,6 +114,16 @@ get_current_schema_id(#state{ current_schema = CurrentSchema
get_default_schema_ver(#state{default_schema_ver = SchemaVer}) ->
SchemaVer.

%% @doc Getter for `validator'.
-spec get_validator(State :: state()) -> module() | undefined.
get_validator(#state{validator = Validator}) ->
Validator.

%% @doc Getter for `validator_state'.
-spec get_validator_state(State :: state()) -> jesse_validator:state().
get_validator_state(#state{validator_state = ValidatorState}) ->
ValidatorState.

%% @doc Getter for `error_handler'.
-spec get_error_handler(State :: state()) -> jesse:error_handler().
get_error_handler(#state{error_handler = ErrorHandler}) ->
Expand Down Expand Up @@ -142,11 +159,24 @@ new(JsonSchema, Options) ->
, Options
, ?default_schema_loader_fun
),
Validator = proplists:get_value( validator
, Options
, undefined
),
ValidatorOpts = proplists:get_value( validator_opts
, Options
, undefined
),
ValidatorState = init_validator_state( Validator
, ValidatorOpts
),
NewState = #state{ root_schema = JsonSchema
, current_path = []
, allowed_errors = AllowedErrors
, error_list = []
, error_handler = ErrorHandler
, validator = Validator
, validator_state = ValidatorState
, default_schema_ver = DefaultSchemaVer
, schema_loader_fun = LoaderFun
},
Expand Down Expand Up @@ -179,6 +209,13 @@ set_current_schema(#state{id = Id} = State, NewSchema) ->
set_error_list(State, ErrorList) ->
State#state{error_list = ErrorList}.

%% @doc Setter for `validator_state'.
-spec set_validator_state( State :: state()
, ValidatorState :: jesse_validator:state()
) -> state().
set_validator_state(State, ValidatorState) ->
State#state{validator_state = ValidatorState}.

%% @doc Resolve a reference.
-spec resolve_ref(State :: state(), Reference :: jesse:schema_ref()) -> state().
resolve_ref(State, Reference) ->
Expand Down Expand Up @@ -234,6 +271,16 @@ undo_resolve_ref(RefState, OriginalState) ->
, id = OriginalState#state.id
}.

%% @doc Init custom validator state.
%% @private
-spec init_validator_state( Validator :: module() | undefined
, Opts :: jesse_validator:opts()
) -> jesse_validator:state().
init_validator_state(undefined, _) ->
undefined;
init_validator_state(Validator, Opts) ->
jesse_validator:init_state(Validator, Opts).

%% @doc Retrieve a specific part of a schema
%% @private
-spec load_local_schema( Schema :: ?not_found | jesse:schema()
Expand Down Expand Up @@ -273,8 +320,8 @@ load_local_schema(Schema, [Key | Keys]) ->

%% @doc Resolve a new id
%% @private
-spec combine_id(undefined | http_uri:uri(),
undefined | binary()) -> http_uri:uri().
-spec combine_id(undefined | http_uri(),
undefined | binary()) -> http_uri().
combine_id(Id, undefined) ->
Id;
combine_id(Id, RefBin) ->
Expand Down
61 changes: 61 additions & 0 deletions src/jesse_validator.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
%%%=============================================================================
%% Copyright 2014- Klarna AB
%% Copyright 2015- AUTHORS
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% @doc Json schema validation module.
%%
%% This module is the behaviour definition for jesse validator.
%% @end
%%%=============================================================================

-module(jesse_validator).

%% API
-export([ init_state/2
, check_value/4
]).

%% Behaviour definition
-callback init_state(Opts :: opts()) ->
state().

-callback check_value( Value :: any()
, Attr :: {binary(), jesse:json_term()}
, State :: jesse_state:state()
) -> jesse_state:state() | no_return().


-type opts() :: any().

-type state() :: any().

-export_type([ opts/0
, state/0
]).

%% API
-spec init_state( Validator :: module()
, Opts :: opts()
) -> state().
init_state(Validator, Opts) ->
Validator:init_state(Opts).

-spec check_value( Validator :: module()
, Value :: any()
, Attr :: {binary(), jesse:json_term()}
, State :: jesse_state:state()
) -> jesse_state:state() | no_return().
check_value(Validator, Value, Attr, State) ->
Validator:check_value(Value, Attr, State).
Loading