Skip to content

Commit

Permalink
style
Browse files Browse the repository at this point in the history
  • Loading branch information
andreineculau committed May 20, 2017
1 parent f63a05c commit d29dd1c
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 84 deletions.
22 changes: 16 additions & 6 deletions src/jesse_lib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,20 @@ is_null(_Value) ->

%% @doc check if json object is_empty.
-spec is_json_object_empty(Value :: any()) -> boolean().
is_json_object_empty({struct, Value}) when is_list(Value) andalso Value =:= [] -> true;
is_json_object_empty({Value}) when is_list(Value) andalso Value =:= [] -> true;
is_json_object_empty({struct, Value})
when is_list(Value) andalso Value =:= [] ->
true;
is_json_object_empty({Value})
when is_list(Value)
andalso Value =:= [] ->
true;
%% handle `jsx' empty objects
is_json_object_empty([{}]) -> true;
?IF_MAPS(is_json_object_empty(Map) when erlang:is_map(Map) -> maps:size(Map) =:= 0;)
is_json_object_empty(_) -> false.

is_json_object_empty([{}]) ->
true;
?IF_MAPS(
is_json_object_empty(Map)
when erlang:is_map(Map) ->
maps:size(Map) =:= 0;
)
is_json_object_empty(_) ->
false.
32 changes: 23 additions & 9 deletions src/jesse_state.erl
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,22 @@ resolve_ref(State, Reference) ->
Path = jesse_json_path:parse(Pointer),
case load_local_schema(State#state.root_schema, Path) of
?not_found ->
jesse_error:handle_schema_invalid({?schema_not_found, CanonicalReference}, State);
jesse_error:handle_schema_invalid( { ?schema_not_found
, CanonicalReference
}
, State
);
Schema ->
set_current_schema(State, Schema)
end;
false ->
case load_schema(State, BaseURI) of
?not_found ->
jesse_error:handle_schema_invalid({?schema_not_found, CanonicalReference}, State);
jesse_error:handle_schema_invalid( { ?schema_not_found
, CanonicalReference
}
, State
);
RemoteSchema ->
SchemaVer =
jesse_json_path:value(?SCHEMA, RemoteSchema, ?default_schema_ver),
Expand All @@ -256,7 +264,11 @@ resolve_ref(State, Reference) ->
Path = jesse_json_path:parse(Pointer),
case load_local_schema(RemoteSchema, Path) of
?not_found ->
jesse_error:handle_schema_invalid({?schema_not_found, CanonicalReference}, State);
jesse_error:handle_schema_invalid( { ?schema_not_found
, CanonicalReference
}
, State
);
Schema ->
set_current_schema(NewState, Schema)
end
Expand Down Expand Up @@ -435,12 +447,14 @@ set_value(#state{setter_fun=Setter
get_extra_validator(#state{extra_validator=Fun}) -> Fun.

-spec validator_options(State :: state()) -> options().
validator_options(#state{validator_options=Options}) -> Options.
validator_options(#state{validator_options = Options}) ->
Options.

-spec validator_option(Option :: atom(), State :: state()) -> any().
validator_option(Option, #state{validator_options=Options}) ->
proplists:get_value(Option, Options).
validator_option(Option, #state{validator_options = Options}) ->
proplists:get_value(Option, Options).

-spec validator_option(Option :: atom(), State :: state(), Default :: any()) -> any().
validator_option(Option, #state{validator_options=Options}, Default) ->
proplists:get_value(Option, Options, Default).
-spec validator_option(Option :: atom(), State :: state(), Default :: any()) ->
any().
validator_option(Option, #state{validator_options = Options}, Default) ->
proplists:get_value(Option, Options, Default).
105 changes: 65 additions & 40 deletions src/jesse_validator_draft3.erl
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,20 @@ check_properties(Value, Properties, State) ->
= lists:foldl( fun({PropertyName, PropertySchema}, CurrentState) ->
case get_value(PropertyName, Value) of
?not_found ->
case get_value(?DEFAULT, PropertySchema) of
?not_found -> check_required( PropertySchema
, PropertyName
, Value
, CurrentState
);
Default -> check_default(PropertyName, PropertySchema, Default, CurrentState)
end;
case get_value(?DEFAULT, PropertySchema) of
?not_found ->
check_required( PropertySchema
, PropertyName
, Value
, CurrentState
);
Default ->
check_default( PropertyName
, PropertySchema
, Default
, CurrentState
)
end;
Property ->
NewState = set_current_schema( CurrentState
, PropertySchema
Expand Down Expand Up @@ -585,15 +591,15 @@ check_items_fun(Tuples, State) ->
%% optional.
%% @private
check_required(PropertySchema, PropertyName, Value, CurrentState) ->
case get_value(?REQUIRED, PropertySchema) of
true ->
handle_data_invalid( {?missing_required_property
, PropertyName}
, Value
, CurrentState);
_ ->
CurrentState
end.
case get_value(?REQUIRED, PropertySchema) of
true ->
handle_data_invalid( {?missing_required_property
, PropertyName}
, Value
, CurrentState);
_ ->
CurrentState
end.

%% @doc 5.8. dependencies
%%
Expand Down Expand Up @@ -916,7 +922,8 @@ validate_ref(Value, Reference, State) ->
{error, NewState} ->
undo_resolve_ref(NewState, State);
{ok, NewState, Schema} ->
ResultState = jesse_schema_validator:validate_with_state(Schema, Value, NewState),
ResultState =
jesse_schema_validator:validate_with_state(Schema, Value, NewState),
undo_resolve_ref(ResultState, State)
end.

Expand Down Expand Up @@ -1056,44 +1063,62 @@ check_external_validation(Value, State) ->
Fun -> Fun(Value, State)
end.

%% @private
validator_option(Option, State, Default) ->
jesse_state:validator_option(Option, State, Default).

%% @private
set_value(PropertyName, Value, State) ->
Path = lists:reverse([PropertyName] ++ jesse_state:get_current_path(State)),
jesse_state:set_value(State, Path, Value).
Path = lists:reverse([PropertyName] ++ jesse_state:get_current_path(State)),
jesse_state:set_value(State, Path, Value).

%% @private
check_default_for_type(Default, State) ->
jesse_state:validator_option('use_defaults', State, false)
andalso (not jesse_lib:is_json_object(Default)
orelse jesse_state:validator_option('apply_defaults_to_empty_objects', State, false)
orelse not jesse_lib:is_json_object_empty(Default)).
validator_option('use_defaults', State, false)
andalso (not jesse_lib:is_json_object(Default)
orelse validator_option( 'apply_defaults_to_empty_objects'
, State
, false
)
orelse not jesse_lib:is_json_object_empty(Default)).

%% @private
check_default(PropertyName, PropertySchema, Default, State) ->
Type = get_value(?TYPE, PropertySchema, ?not_found),
case is_valid_default(Type, Default, State) of
true -> set_default(PropertyName, PropertySchema, Default, State);
false -> State
end.
Type = get_value(?TYPE, PropertySchema, ?not_found),
case is_valid_default(Type, Default, State) of
true ->
set_default(PropertyName, PropertySchema, Default, State);
false ->
State
end.

is_valid_default(?not_found, _Default, _State) -> false;
%% @private
is_valid_default(?not_found, _Default, _State) ->
false;
is_valid_default(Type, Default, State)
when is_binary(Type) ->
check_default_for_type(Default, State)
andalso is_type_valid(Default, Type, State);
check_default_for_type(Default, State)
andalso is_type_valid(Default, Type, State);
is_valid_default(Types, Default, State)
when is_list(Types) ->
check_default_for_type(Default, State)
andalso lists:any(fun(Type) -> is_type_valid(Default, Type, State) end, Types);
check_default_for_type(Default, State)
andalso lists:any( fun(Type) ->
is_type_valid(Default, Type, State)
end
, Types
);
is_valid_default(_, _Default, _State) -> false.

%% @private
set_default(PropertyName, PropertySchema, Default, State) ->
State1 = set_value(PropertyName, Default, State),
State2 = add_to_path(State1, PropertyName),
case validate_schema(Default, PropertySchema, State2) of
{true, State4} -> jesse_state:remove_last_from_path(State4);
_ -> State
end.
State1 = set_value(PropertyName, Default, State),
State2 = add_to_path(State1, PropertyName),
case validate_schema(Default, PropertySchema, State2) of
{true, State4} ->
jesse_state:remove_last_from_path(State4);
_ ->
State
end.

%% @doc Validate a value against a schema in a given state.
%% Used by all combinators to run validation on a schema.
Expand Down
75 changes: 50 additions & 25 deletions src/jesse_validator_draft4.erl
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,14 @@ check_properties(Value, Properties, State) ->
case get_value(PropertyName, Value) of
?not_found ->
case get_value(?DEFAULT, PropertySchema) of
?not_found -> CurrentState;
Default -> check_default(PropertyName, PropertySchema, Default, CurrentState)
?not_found ->
CurrentState;
Default ->
check_default( PropertyName
, PropertySchema
, Default
, CurrentState
)
end;
Property ->
NewState = set_current_schema( CurrentState
Expand Down Expand Up @@ -1229,7 +1235,8 @@ validate_ref(Value, Reference, State) ->
{error, NewState} ->
undo_resolve_ref(NewState, State);
{ok, NewState, Schema} ->
ResultState = jesse_schema_validator:validate_with_state(Schema, Value, NewState),
ResultState =
jesse_schema_validator:validate_with_state(Schema, Value, NewState),
undo_resolve_ref(ResultState, State)
end.

Expand Down Expand Up @@ -1362,6 +1369,10 @@ add_to_path(State, Property) ->
remove_last_from_path(State) ->
jesse_state:remove_last_from_path(State).

%% @private
validator_option(Option, State, Default) ->
jesse_state:validator_option(Option, State, Default).

%% @private
valid_datetime(DateTimeBin) ->
case rfc3339:parse(DateTimeBin) of
Expand All @@ -1379,39 +1390,53 @@ check_external_validation(Value, State) ->

%% @private
set_value(PropertyName, Value, State) ->
Path = lists:reverse([PropertyName] ++ jesse_state:get_current_path(State)),
jesse_state:set_value(State, Path, Value).
Path = lists:reverse([PropertyName] ++ jesse_state:get_current_path(State)),
jesse_state:set_value(State, Path, Value).

%% @private
check_default_for_type(Default, State) ->
jesse_state:validator_option('use_defaults', State, false)
andalso (not jesse_lib:is_json_object(Default)
orelse jesse_state:validator_option('apply_defaults_to_empty_objects', State, false)
orelse not jesse_lib:is_json_object_empty(Default)).
validator_option('use_defaults', State, false)
andalso (not jesse_lib:is_json_object(Default)
orelse validator_option( 'apply_defaults_to_empty_objects'
, State
, false
)
orelse not jesse_lib:is_json_object_empty(Default)).

%% @private
check_default(PropertyName, PropertySchema, Default, State) ->
Type = get_value(?TYPE, PropertySchema, ?not_found),
case is_valid_default(Type, Default, State) of
true -> set_default(PropertyName, PropertySchema, Default, State);
false -> State
end.
Type = get_value(?TYPE, PropertySchema, ?not_found),
case is_valid_default(Type, Default, State) of
true ->
set_default(PropertyName, PropertySchema, Default, State);
false ->
State
end.

is_valid_default(?not_found, _Default, _State) -> false;
%% @private
is_valid_default(?not_found, _Default, _State) ->
false;
is_valid_default(Type, Default, State)
when is_binary(Type) ->
check_default_for_type(Default, State)
andalso is_type_valid(Default, Type);
check_default_for_type(Default, State)
andalso is_type_valid(Default, Type);
is_valid_default(Types, Default, State)
when is_list(Types) ->
check_default_for_type(Default, State)
andalso lists:any(fun(Type) -> is_type_valid(Default, Type) end, Types);
check_default_for_type(Default, State)
andalso lists:any( fun(Type) ->
is_type_valid(Default, Type)
end
, Types
);
is_valid_default(_, _Default, _State) -> false.

%% @private
set_default(PropertyName, PropertySchema, Default, State) ->
State1 = set_value(PropertyName, Default, State),
State2 = add_to_path(State1, PropertyName),
case validate_schema(Default, PropertySchema, State2) of
{true, State4} -> jesse_state:remove_last_from_path(State4);
_ -> State
end.
State1 = set_value(PropertyName, Default, State),
State2 = add_to_path(State1, PropertyName),
case validate_schema(Default, PropertySchema, State2) of
{true, State4} ->
jesse_state:remove_last_from_path(State4);
_ ->
State
end.
8 changes: 4 additions & 4 deletions test/jesse_schema_validator_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ setter_test() ->
, {validator_options, [use_defaults]}
],

[ ?assertEqual({ok, Value}
,jesse_schema_validator:validate(Schema, Value, [])
[ ?assertEqual( {ok, Value}
, jesse_schema_validator:validate(Schema, Value, [])
)
, ?assertEqual({ok, Default}
,jesse_schema_validator:validate(Schema, Value, Options)
, ?assertEqual( {ok, Default}
, jesse_schema_validator:validate(Schema, Value, Options)
)
].

Expand Down

0 comments on commit d29dd1c

Please sign in to comment.