Skip to content

Commit 4a1e1be

Browse files
lazedoandreineculau
authored andcommitted
set default values for properties
persistence depends on setter_fun option
1 parent af06bb2 commit 4a1e1be

6 files changed

+339
-37
lines changed

src/jesse_lib.erl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
-export([ empty_if_not_found/1
2828
, is_array/1
2929
, is_json_object/1
30+
, is_json_object_empty/1
3031
, is_null/1
3132
]).
3233

@@ -86,3 +87,22 @@ is_null(null) ->
8687
is_null(_Value) ->
8788
false.
8889

90+
%% @doc check if json object is_empty.
91+
-spec is_json_object_empty(Value :: any()) -> boolean().
92+
is_json_object_empty({struct, Value})
93+
when is_list(Value) andalso Value =:= [] ->
94+
true;
95+
is_json_object_empty({Value})
96+
when is_list(Value)
97+
andalso Value =:= [] ->
98+
true;
99+
%% handle `jsx' empty objects
100+
is_json_object_empty([{}]) ->
101+
true;
102+
?IF_MAPS(
103+
is_json_object_empty(Map)
104+
when erlang:is_map(Map) ->
105+
maps:size(Map) =:= 0;
106+
)
107+
is_json_object_empty(_) ->
108+
false.

src/jesse_schema_validator.hrl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
-define(MULTIPLEOF, <<"multipleOf">>).
6363
-define(MAXPROPERTIES, <<"maxProperties">>).
6464
-define(MINPROPERTIES, <<"minProperties">>).
65+
-define(DEFAULT, <<"default">>).
6566

6667
%% Constant definitions for Json types
6768
-define(ANY, <<"any">>).

src/jesse_state.erl

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
, undo_resolve_ref/2
4545
, canonical_path/2
4646
, combine_id/2
47+
, validator_options/1
48+
, validator_option/2, validator_option/3
4749
]).
4850

4951
-export_type([ state/0
@@ -66,6 +68,7 @@
6668
, external_validator :: external_validator()
6769
, setter_fun :: setter_fun()
6870
, id :: schema_id()
71+
, validator_options :: options()
6972
}
7073
).
7174

@@ -154,6 +157,13 @@ new(JsonSchema, Options) ->
154157
SetterFun = proplists:get_value( setter_fun
155158
, Options
156159
),
160+
Value = proplists:get_value( with_value
161+
, Options
162+
),
163+
ValidatorOptions = proplists:get_value( validator_options
164+
, Options
165+
, []
166+
),
157167
NewState = #state{ root_schema = JsonSchema
158168
, current_path = []
159169
, allowed_errors = AllowedErrors
@@ -163,6 +173,8 @@ new(JsonSchema, Options) ->
163173
, schema_loader_fun = LoaderFun
164174
, external_validator = ExternalValidator
165175
, setter_fun = SetterFun
176+
, current_value = Value
177+
, validator_options = ValidatorOptions
166178
},
167179
set_current_schema(NewState, JsonSchema).
168180

@@ -212,14 +224,22 @@ resolve_ref(State, Reference) ->
212224
Path = jesse_json_path:parse(Pointer),
213225
case load_local_schema(State#state.root_schema, Path) of
214226
?not_found ->
215-
jesse_error:handle_schema_invalid({?schema_not_found, CanonicalReference}, State);
227+
jesse_error:handle_schema_invalid( { ?schema_not_found
228+
, CanonicalReference
229+
}
230+
, State
231+
);
216232
Schema ->
217233
set_current_schema(State, Schema)
218234
end;
219235
false ->
220236
case load_schema(State, BaseURI) of
221237
?not_found ->
222-
jesse_error:handle_schema_invalid({?schema_not_found, CanonicalReference}, State);
238+
jesse_error:handle_schema_invalid( { ?schema_not_found
239+
, CanonicalReference
240+
}
241+
, State
242+
);
223243
RemoteSchema ->
224244
SchemaVer =
225245
jesse_json_path:value(?SCHEMA, RemoteSchema, ?default_schema_ver),
@@ -230,7 +250,11 @@ resolve_ref(State, Reference) ->
230250
Path = jesse_json_path:parse(Pointer),
231251
case load_local_schema(RemoteSchema, Path) of
232252
?not_found ->
233-
jesse_error:handle_schema_invalid({?schema_not_found, CanonicalReference}, State);
253+
jesse_error:handle_schema_invalid( { ?schema_not_found
254+
, CanonicalReference
255+
}
256+
, State
257+
);
234258
Schema ->
235259
set_current_schema(NewState, Schema)
236260
end
@@ -405,9 +429,25 @@ get_external_validator(#state{external_validator = Fun}) ->
405429
get_current_value(#state{current_value = Value}) -> Value.
406430

407431
-spec set_value(State :: state(), jesse:path(), jesse:json_term()) -> state().
408-
set_value(#state{setter_fun=undefined}=State, _Path, _Value) -> State;
409-
set_value(#state{current_value=undefined}=State, _Path, _Value) -> State;
410-
set_value(#state{setter_fun=Setter
411-
,current_value=Value
412-
}=State, Path, NewValue) ->
432+
set_value(#state{setter_fun = undefined} = State, _Path, _Value) -> State;
433+
set_value(#state{current_value = undefined} = State, _Path, _Value) -> State;
434+
set_value( #state{setter_fun = Setter
435+
, current_value = Value
436+
} = State
437+
, Path
438+
, NewValue
439+
) ->
413440
State#state{current_value = Setter(Path, NewValue, Value)}.
441+
442+
-spec validator_options(State :: state()) -> options().
443+
validator_options(#state{validator_options = Options}) ->
444+
Options.
445+
446+
-spec validator_option(Option :: atom(), State :: state()) -> any().
447+
validator_option(Option, #state{validator_options = Options}) ->
448+
proplists:get_value(Option, Options).
449+
450+
-spec validator_option(Option :: atom(), State :: state(), Default :: any()) ->
451+
any().
452+
validator_option(Option, #state{validator_options = Options}, Default) ->
453+
proplists:get_value(Option, Options, Default).

src/jesse_validator_draft3.erl

Lines changed: 116 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
-include("jesse_schema_validator.hrl").
3232

3333
-type schema_error() :: ?wrong_type_dependency
34+
| ?schema_invalid
3435
| ?wrong_type_items.
3536

3637
-type schema_error_type() :: schema_error()
@@ -348,20 +349,19 @@ check_properties(Value, Properties, State) ->
348349
= lists:foldl( fun({PropertyName, PropertySchema}, CurrentState) ->
349350
case get_value(PropertyName, Value) of
350351
?not_found ->
351-
%% @doc 5.7. required
352-
%%
353-
%% This attribute indicates if the instance must have a value, and not
354-
%% be undefined. This is false by default, making the instance
355-
%% optional.
356-
%% @end
357-
case get_value(?REQUIRED, PropertySchema) of
358-
true ->
359-
handle_data_invalid( {?missing_required_property
360-
, PropertyName}
361-
, Value
362-
, CurrentState);
363-
_ ->
364-
CurrentState
352+
case get_value(?DEFAULT, PropertySchema) of
353+
?not_found ->
354+
check_required( PropertySchema
355+
, PropertyName
356+
, Value
357+
, CurrentState
358+
);
359+
Default ->
360+
check_default( PropertyName
361+
, PropertySchema
362+
, Default
363+
, CurrentState
364+
)
365365
end;
366366
Property ->
367367
NewState = set_current_schema( CurrentState
@@ -583,6 +583,24 @@ check_items_fun(Tuples, State) ->
583583
),
584584
set_current_schema(TmpState, get_current_schema(State)).
585585

586+
587+
%% @doc 5.7. required
588+
%%
589+
%% This attribute indicates if the instance must have a value, and not
590+
%% be undefined. This is false by default, making the instance
591+
%% optional.
592+
%% @private
593+
check_required(PropertySchema, PropertyName, Value, CurrentState) ->
594+
case get_value(?REQUIRED, PropertySchema) of
595+
true ->
596+
handle_data_invalid( {?missing_required_property
597+
, PropertyName}
598+
, Value
599+
, CurrentState);
600+
_ ->
601+
CurrentState
602+
end.
603+
586604
%% @doc 5.8. dependencies
587605
%%
588606
%% This attribute is an object that defines the requirements of a
@@ -904,7 +922,8 @@ validate_ref(Value, Reference, State) ->
904922
{error, NewState} ->
905923
undo_resolve_ref(NewState, State);
906924
{ok, NewState, Schema} ->
907-
ResultState = jesse_schema_validator:validate_with_state(Schema, Value, NewState),
925+
ResultState =
926+
jesse_schema_validator:validate_with_state(Schema, Value, NewState),
908927
undo_resolve_ref(ResultState, State)
909928
end.
910929

@@ -992,7 +1011,11 @@ compare_properties(Value1, Value2) ->
9921011
%% Wrappers
9931012
%% @private
9941013
get_value(Key, Schema) ->
995-
jesse_json_path:value(Key, Schema, ?not_found).
1014+
get_value(Key, Schema, ?not_found).
1015+
1016+
%% @private
1017+
get_value(Key, Schema, Default) ->
1018+
jesse_json_path:value(Key, Schema, Default).
9961019

9971020
%% @private
9981021
unwrap(Value) ->
@@ -1041,3 +1064,80 @@ maybe_external_check_value(Value, State) ->
10411064
Fun ->
10421065
Fun(Value, State)
10431066
end.
1067+
1068+
%% @private
1069+
validator_option(Option, State, Default) ->
1070+
jesse_state:validator_option(Option, State, Default).
1071+
1072+
%% @private
1073+
set_value(PropertyName, Value, State) ->
1074+
Path = lists:reverse([PropertyName] ++ jesse_state:get_current_path(State)),
1075+
jesse_state:set_value(State, Path, Value).
1076+
1077+
%% @private
1078+
check_default_for_type(Default, State) ->
1079+
validator_option('use_defaults', State, false)
1080+
andalso (not jesse_lib:is_json_object(Default)
1081+
orelse validator_option( 'apply_defaults_to_empty_objects'
1082+
, State
1083+
, false
1084+
)
1085+
orelse not jesse_lib:is_json_object_empty(Default)).
1086+
1087+
%% @private
1088+
check_default(PropertyName, PropertySchema, Default, State) ->
1089+
Type = get_value(?TYPE, PropertySchema, ?not_found),
1090+
case is_valid_default(Type, Default, State) of
1091+
true ->
1092+
set_default(PropertyName, PropertySchema, Default, State);
1093+
false ->
1094+
State
1095+
end.
1096+
1097+
%% @private
1098+
is_valid_default(?not_found, _Default, _State) ->
1099+
false;
1100+
is_valid_default(Type, Default, State)
1101+
when is_binary(Type) ->
1102+
check_default_for_type(Default, State)
1103+
andalso is_type_valid(Default, Type, State);
1104+
is_valid_default(Types, Default, State)
1105+
when is_list(Types) ->
1106+
check_default_for_type(Default, State)
1107+
andalso lists:any( fun(Type) ->
1108+
is_type_valid(Default, Type, State)
1109+
end
1110+
, Types
1111+
);
1112+
is_valid_default(_, _Default, _State) -> false.
1113+
1114+
%% @private
1115+
set_default(PropertyName, PropertySchema, Default, State) ->
1116+
State1 = set_value(PropertyName, Default, State),
1117+
State2 = add_to_path(State1, PropertyName),
1118+
case validate_schema(Default, PropertySchema, State2) of
1119+
{true, State4} ->
1120+
jesse_state:remove_last_from_path(State4);
1121+
_ ->
1122+
State
1123+
end.
1124+
1125+
%% @doc Validate a value against a schema in a given state.
1126+
%% Used by all combinators to run validation on a schema.
1127+
%% @private
1128+
validate_schema(Value, Schema, State0) ->
1129+
try
1130+
case jesse_lib:is_json_object(Schema) of
1131+
true ->
1132+
State1 = set_current_schema(State0, Schema),
1133+
State2 = jesse_schema_validator:validate_with_state( Schema
1134+
, Value
1135+
, State1
1136+
),
1137+
{true, State2};
1138+
false ->
1139+
handle_schema_invalid(?schema_invalid, State0)
1140+
end
1141+
catch
1142+
throw:Errors -> {false, Errors}
1143+
end.

0 commit comments

Comments
 (0)