diff --git a/src/jesse_lib.erl b/src/jesse_lib.erl index ff73ab01..e5563724 100644 --- a/src/jesse_lib.erl +++ b/src/jesse_lib.erl @@ -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. diff --git a/src/jesse_state.erl b/src/jesse_state.erl index 010f11a3..a34b50d2 100644 --- a/src/jesse_state.erl +++ b/src/jesse_state.erl @@ -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), @@ -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 @@ -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). diff --git a/src/jesse_validator_draft3.erl b/src/jesse_validator_draft3.erl index 1e05e375..43f6b9a8 100644 --- a/src/jesse_validator_draft3.erl +++ b/src/jesse_validator_draft3.erl @@ -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 @@ -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 %% @@ -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. @@ -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. diff --git a/src/jesse_validator_draft4.erl b/src/jesse_validator_draft4.erl index a593c889..4cc4aa90 100644 --- a/src/jesse_validator_draft4.erl +++ b/src/jesse_validator_draft4.erl @@ -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 @@ -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. @@ -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 @@ -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. diff --git a/test/jesse_schema_validator_tests.erl b/test/jesse_schema_validator_tests.erl index 1562ca9c..3a5ce9c2 100644 --- a/test/jesse_schema_validator_tests.erl +++ b/test/jesse_schema_validator_tests.erl @@ -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) ) ].