Skip to content
Merged
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
40 changes: 40 additions & 0 deletions src/jesse.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
, validate/3
, validate_definition/3
, validate_definition/4
, validate_local_ref/3
, validate_local_ref/4
, validate_with_schema/2
, validate_with_schema/3
]).
Expand Down Expand Up @@ -255,6 +257,44 @@ validate_definition(Defintion, Schema, Data, Options) ->
throw:Error -> {error, Error}
end.

%% @doc Equivalent to {@link validate_local_ref/4} where `Options' is an empty list.
-spec validate_local_ref( RefPath :: string()
, Schema :: json_term() | binary()
, Data :: json_term() | binary()
) -> {ok, json_term()}
| jesse_error:error().
validate_local_ref(RefPath, Schema, Data) ->
validate_local_ref(RefPath, Schema, Data, []).

%% @doc Validates json `Data' agains the given definition path 'RefPath' in the given
%% schema `Schema', using `Options'.
%% If the given json is valid, then it is returned to the caller, otherwise
%% an error with an appropriate error reason is returned. If the `parser_fun'
%% option is provided, then both `Schema' and `Data' are considered to be a
%% binary string, so `parser_fun' is used to convert both binary strings to a
%% supported internal representation of json.
%% If `parser_fun' is not provided, then both `Schema' and `Data' are considered
%% to already be a supported internal representation of json.
-spec validate_local_ref( RefPath :: string()
, Schema :: json_term() | binary()
, Data :: json_term() | binary()
, Options :: [{Key :: atom(), Data :: any()}]
) -> {ok, json_term()}
| jesse_error:error().
validate_local_ref(RefPath, Schema, Data, Options) ->
try
ParserFun = proplists:get_value(parser_fun, Options, fun(X) -> X end),
ParsedSchema = try_parse(schema, ParserFun, Schema),
ParsedData = try_parse(data, ParserFun, Data),
jesse_schema_validator:validate_local_ref( RefPath
, ParsedSchema
, ParsedData
, Options
)
catch
throw:Error -> {error, Error}
end.

%% @doc Equivalent to {@link validate_with_schema/3} where `Options'
%% is an empty list.
-spec validate_with_schema( Schema :: schema() | binary()
Expand Down
32 changes: 23 additions & 9 deletions src/jesse_schema_validator.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
%% API
-export([ validate/3
, validate_definition/4
, validate_local_ref/4
, validate_with_state/3
]).

Expand Down Expand Up @@ -62,7 +63,7 @@ validate(JsonSchema, Value, Options) ->
NewState = validate_with_state(JsonSchema, Value, State),
{result(NewState), Value}.

%% @doc Validates json `Data' against `Definition' in `JsonSchema' with `Options'.
%% @doc Validates json `Data' against a given $ref path in `JsonSchema' with `Options'.
%% If the given json is valid, then it is returned to the caller as is,
%% otherwise an exception will be thrown.
-spec validate_definition( Definition :: string()
Expand All @@ -71,10 +72,23 @@ validate(JsonSchema, Value, Options) ->
, Options :: [{Key :: atom(), Data :: any()}]
) -> {ok, jesse:json_term()}
| no_return().
validate_definition(Defintion, JsonSchema, Value, Options) ->
State = jesse_state:new(JsonSchema, Options),
Schema = make_definition_ref(Defintion),
NewState = validate_with_state(Schema, Value, State),
validate_definition(Definition, JsonSchema, Value, Options) ->
RefPath = "#/definitions/" ++ Definition,
validate_local_ref(RefPath, JsonSchema, Value, Options).

%% @doc Validates json `Data' against `Definition' in `JsonSchema' with `Options'.
%% If the given json is valid, then it is returned to the caller as is,
%% otherwise an exception will be thrown.
-spec validate_local_ref( RefPath :: string()
, JsonSchema :: jesse:json_term()
, Data :: jesse:json_term()
, Options :: [{Key :: atom(), Data :: any()}]
) -> {ok, jesse:json_term()}
| no_return().
validate_local_ref(RefPath, JsonSchema, Value, Options) ->
State = jesse_state:new(JsonSchema, Options),
Ref = make_ref(RefPath),
NewState = validate_with_state(Ref, Value, State),
{result(NewState), Value}.

%% @doc Validates json `Data' against `JsonSchema' with `State'.
Expand Down Expand Up @@ -140,9 +154,9 @@ run_validator(Validator, Value, [Attr | Attrs], State0) ->
),
run_validator(Validator, Value, Attrs, State).

%% @doc Makes a $ref schema object pointing to the given `Definition'
%% @doc Makes a $ref schema object pointing to the given path
%% in schema defintions.
%% @private
make_definition_ref(Definition) ->
Definition1 = list_to_binary(Definition),
[{<<"$ref">>, <<"#/definitions/", Definition1/binary>>}].
make_ref(RefPath) ->
RefPath1 = list_to_binary(RefPath),
[{<<"$ref">>, RefPath1}].