This commit is contained in:
Alexander Dergachev 2014-04-07 22:14:54 +02:00
parent 503bf72a39
commit d9ace26916
2 changed files with 40 additions and 20 deletions

View File

@ -125,7 +125,7 @@ result(State) ->
%% will be thrown. %% will be thrown.
-spec get_schema_id(Schema :: jesse:json_term()) -> string(). -spec get_schema_id(Schema :: jesse:json_term()) -> string().
get_schema_id(Schema) -> get_schema_id(Schema) ->
case get_path(?ID, Schema) of case get_value(?ID, Schema) of
[] -> throw({schema_invalid, Schema, missing_id_field}); [] -> throw({schema_invalid, Schema, missing_id_field});
Id -> erlang:binary_to_list(Id) Id -> erlang:binary_to_list(Id)
end. end.
@ -223,9 +223,9 @@ check_value(Value, [{?DEPENDENCIES, Dependencies} | Attrs], State) ->
check_value(Value, [{?MINIMUM, Minimum} | Attrs], State) -> check_value(Value, [{?MINIMUM, Minimum} | Attrs], State) ->
NewState = case is_number(Value) of NewState = case is_number(Value) of
true -> true ->
ExclusiveMinimum = get_path( ?EXCLUSIVEMINIMUM ExclusiveMinimum = get_value( ?EXCLUSIVEMINIMUM
, get_current_schema(State) , get_current_schema(State)
), ),
check_minimum(Value, Minimum, ExclusiveMinimum, State); check_minimum(Value, Minimum, ExclusiveMinimum, State);
false -> false ->
State State
@ -234,9 +234,9 @@ check_value(Value, [{?MINIMUM, Minimum} | Attrs], State) ->
check_value(Value, [{?MAXIMUM, Maximum} | Attrs], State) -> check_value(Value, [{?MAXIMUM, Maximum} | Attrs], State) ->
NewState = case is_number(Value) of NewState = case is_number(Value) of
true -> true ->
ExclusiveMaximum = get_path( ?EXCLUSIVEMAXIMUM ExclusiveMaximum = get_value( ?EXCLUSIVEMAXIMUM
, get_current_schema(State) , get_current_schema(State)
), ),
check_maximum(Value, Maximum, ExclusiveMaximum, State); check_maximum(Value, Maximum, ExclusiveMaximum, State);
false -> false ->
State State
@ -427,7 +427,7 @@ wrong_type(Value, State) ->
check_properties(Value, Properties, State) -> check_properties(Value, Properties, State) ->
TmpState TmpState
= lists:foldl( fun({PropertyName, PropertySchema}, CurrentState) -> = lists:foldl( fun({PropertyName, PropertySchema}, CurrentState) ->
case get_path(PropertyName, Value) of case get_value(PropertyName, Value) of
[] -> [] ->
%% @doc 5.7. required %% @doc 5.7. required
%% %%
@ -435,7 +435,7 @@ check_properties(Value, Properties, State) ->
%% be undefined. This is false by default, making the instance %% be undefined. This is false by default, making the instance
%% optional. %% optional.
%% @end %% @end
case get_path(?REQUIRED, PropertySchema) of case get_value(?REQUIRED, PropertySchema) of
true -> true ->
handle_data_invalid( {?missing_required_property handle_data_invalid( {?missing_required_property
, PropertyName} , PropertyName}
@ -504,8 +504,8 @@ check_match({PropertyName, PropertyValue}, {Pattern, Schema}, State) ->
%% @private %% @private
check_additional_properties(Value, false, State) -> check_additional_properties(Value, false, State) ->
JsonSchema = get_current_schema(State), JsonSchema = get_current_schema(State),
Properties = get_path(?PROPERTIES, JsonSchema), Properties = get_value(?PROPERTIES, JsonSchema),
PatternProperties = get_path(?PATTERNPROPERTIES, JsonSchema), PatternProperties = get_value(?PATTERNPROPERTIES, JsonSchema),
case get_additional_properties(Value, Properties, PatternProperties) of case get_additional_properties(Value, Properties, PatternProperties) of
[] -> State; [] -> State;
_Extras -> _Extras ->
@ -515,8 +515,8 @@ check_additional_properties(_Value, true, State) ->
State; State;
check_additional_properties(Value, AdditionalProperties, State) -> check_additional_properties(Value, AdditionalProperties, State) ->
JsonSchema = get_current_schema(State), JsonSchema = get_current_schema(State),
Properties = get_path(?PROPERTIES, JsonSchema), Properties = get_value(?PROPERTIES, JsonSchema),
PatternProperties = get_path(?PATTERNPROPERTIES, JsonSchema), PatternProperties = get_value(?PATTERNPROPERTIES, JsonSchema),
case get_additional_properties(Value, Properties, PatternProperties) of case get_additional_properties(Value, Properties, PatternProperties) of
[] -> State; [] -> State;
Extras -> Extras ->
@ -551,7 +551,7 @@ get_additional_properties(Value, Properties, PatternProperties) ->
, ExtraNames0 , ExtraNames0
, Patterns , Patterns
), ),
lists:map(fun(Name) -> {Name, get_path(Name, Value)} end, ExtraNames). lists:map(fun(Name) -> {Name, get_value(Name, Value)} end, ExtraNames).
%% @private %% @private
filter_extra_names(Pattern, ExtraNames) -> filter_extra_names(Pattern, ExtraNames) ->
@ -613,7 +613,7 @@ check_items_array(Value, Items, State) ->
%% to indicate additional items in the array are not allowed, or it can %% to indicate additional items in the array are not allowed, or it can
%% be a schema that defines the schema of the additional items. %% be a schema that defines the schema of the additional items.
%% @end %% @end
case get_path(?ADDITIONALITEMS, JsonSchema) of case get_value(?ADDITIONALITEMS, JsonSchema) of
[] -> State; [] -> State;
true -> State; true -> State;
false -> false ->
@ -665,7 +665,7 @@ check_items_fun(Tuples, State) ->
%% @private %% @private
check_dependencies(Value, Dependencies, State) -> check_dependencies(Value, Dependencies, State) ->
lists:foldl( fun({DependencyName, DependencyValue}, CurrentState) -> lists:foldl( fun({DependencyName, DependencyValue}, CurrentState) ->
case get_path(DependencyName, Value) of case get_value(DependencyName, Value) of
[] -> CurrentState; [] -> CurrentState;
_ -> check_dependency_value( Value _ -> check_dependency_value( Value
, DependencyValue , DependencyValue
@ -679,7 +679,7 @@ check_dependencies(Value, Dependencies, State) ->
%% @private %% @private
check_dependency_value(Value, Dependency, State) when is_binary(Dependency) -> check_dependency_value(Value, Dependency, State) when is_binary(Dependency) ->
case get_path(Dependency, Value) of case get_value(Dependency, Value) of
[] -> [] ->
handle_data_invalid({?missing_dependency, Dependency}, Value, State); handle_data_invalid({?missing_dependency, Dependency}, Value, State);
_ -> _ ->
@ -1028,7 +1028,7 @@ compare_objects(Value1, Value2) ->
%% @private %% @private
compare_properties(Value1, Value2) -> compare_properties(Value1, Value2) ->
lists:all( fun({PropertyName1, PropertyValue1}) -> lists:all( fun({PropertyName1, PropertyValue1}) ->
case get_path(PropertyName1, Value2) of case get_value(PropertyName1, Value2) of
[] -> false; [] -> false;
PropertyValue2 -> is_equal(PropertyValue1, PropertyValue2) PropertyValue2 -> is_equal(PropertyValue1, PropertyValue2)
end end
@ -1112,8 +1112,8 @@ default_error_handler(Error, ErrorList, AllowedErrors) ->
%%============================================================================= %%=============================================================================
%% @private %% @private
get_path(Key, Schema) -> get_value(Key, Schema) ->
jesse_json_path:path(Key, Schema). jesse_json_path:value(Key, Schema, []).
%% @private %% @private
unwrap(Value) -> unwrap(Value) ->

View File

@ -88,3 +88,23 @@ data_invalid_test() ->
), ),
ok. ok.
dots_used_in_keys_test() ->
Schema = {[ {<<"type">>, <<"object">>}
, {<<"properties">>
, {[{<<"3.4.5.6.7">>, {[{<<"type">>, <<"string">>}]}}]}
}]},
ValidJson = {[{<<"3.4.5.6.7">>, <<"Hello world!">>}]},
InvalidJson = {[{<<"3.4.5.6.7">>, true}]},
?assertEqual( {ok, ValidJson}
, jesse_schema_validator:validate(Schema, ValidJson, [])
),
?assertThrow([{data_invalid,{[{<<"type">>,<<"string">>}]}, wrong_type, true, [<<"3.4.5.6.7">>]}]
, jesse_schema_validator:validate(Schema, InvalidJson, [])
).
%%% Local Variables:
%%% erlang-indent-level: 2
%%% End: