Skip to content

Commit d842928

Browse files
lazedoandreineculau
authored andcommitted
set default values for properties
persistence depends on setter_fun option
1 parent 79b87b9 commit d842928

6 files changed

+331
-32
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: 40 additions & 3 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
, root_schema :: jesse:schema()
6769
, schema_loader_fun :: jesse:schema_loader_fun()
6870
, setter_fun :: jesse:setter_fun()
71+
, validator_options :: jesse:options()
6972
}
7073
).
7174

@@ -153,6 +156,13 @@ new(JsonSchema, Options) ->
153156
SetterFun = proplists:get_value( setter_fun
154157
, Options
155158
),
159+
Value = proplists:get_value( with_value
160+
, Options
161+
),
162+
ValidatorOptions = proplists:get_value( validator_options
163+
, Options
164+
, []
165+
),
156166
NewState = #state{ root_schema = JsonSchema
157167
, current_path = []
158168
, allowed_errors = AllowedErrors
@@ -162,6 +172,8 @@ new(JsonSchema, Options) ->
162172
, schema_loader_fun = LoaderFun
163173
, external_validator = ExternalValidator
164174
, setter_fun = SetterFun
175+
, current_value = Value
176+
, validator_options = ValidatorOptions
165177
},
166178
set_current_schema(NewState, JsonSchema).
167179

@@ -213,14 +225,22 @@ resolve_ref(State, Reference) ->
213225
Path = jesse_json_path:parse(Pointer),
214226
case load_local_schema(State#state.root_schema, Path) of
215227
?not_found ->
216-
jesse_error:handle_schema_invalid({?schema_not_found, CanonicalReference}, State);
228+
jesse_error:handle_schema_invalid( { ?schema_not_found
229+
, CanonicalReference
230+
}
231+
, State
232+
);
217233
Schema ->
218234
set_current_schema(State, Schema)
219235
end;
220236
false ->
221237
case load_schema(State, BaseURI) of
222238
?not_found ->
223-
jesse_error:handle_schema_invalid({?schema_not_found, CanonicalReference}, State);
239+
jesse_error:handle_schema_invalid( { ?schema_not_found
240+
, CanonicalReference
241+
}
242+
, State
243+
);
224244
RemoteSchema ->
225245
SchemaVer =
226246
jesse_json_path:value(?SCHEMA, RemoteSchema, ?default_schema_ver),
@@ -231,7 +251,11 @@ resolve_ref(State, Reference) ->
231251
Path = jesse_json_path:parse(Pointer),
232252
case load_local_schema(RemoteSchema, Path) of
233253
?not_found ->
234-
jesse_error:handle_schema_invalid({?schema_not_found, CanonicalReference}, State);
254+
jesse_error:handle_schema_invalid( { ?schema_not_found
255+
, CanonicalReference
256+
}
257+
, State
258+
);
235259
Schema ->
236260
set_current_schema(NewState, Schema)
237261
end
@@ -416,3 +440,16 @@ set_value( #state{ setter_fun = Setter
416440
, NewValue
417441
) ->
418442
State#state{current_value = Setter(Path, NewValue, Value)}.
443+
444+
-spec validator_options(State :: state()) -> jesse:options().
445+
validator_options(#state{validator_options = Options}) ->
446+
Options.
447+
448+
-spec validator_option(Option :: atom(), State :: state()) -> any().
449+
validator_option(Option, #state{validator_options = Options}) ->
450+
proplists:get_value(Option, Options).
451+
452+
-spec validator_option(Option :: atom(), State :: state(), Default :: any()) ->
453+
any().
454+
validator_option(Option, #state{validator_options = Options}, Default) ->
455+
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)