Skip to content

Commit 0ede0af

Browse files
lazedoandreineculau
authored andcommitted
set default values for properties
persistence depends on setter_fun option
1 parent 910f2d4 commit 0ede0af

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
@@ -67,6 +69,7 @@
6769
, external_validator :: external_validator()
6870
, setter_fun :: setter_fun()
6971
, id :: schema_id()
72+
, validator_options :: options()
7073
}
7174
).
7275

@@ -157,6 +160,13 @@ new(JsonSchema, Options) ->
157160
SetterFun = proplists:get_value( setter_fun
158161
, Options
159162
),
163+
Value = proplists:get_value( with_value
164+
, Options
165+
),
166+
ValidatorOptions = proplists:get_value( validator_options
167+
, Options
168+
, []
169+
),
160170
NewState = #state{ root_schema = JsonSchema
161171
, current_path = []
162172
, allowed_errors = AllowedErrors
@@ -166,6 +176,8 @@ new(JsonSchema, Options) ->
166176
, schema_loader_fun = LoaderFun
167177
, external_validator = ExternalValidator
168178
, setter_fun = SetterFun
179+
, current_value = Value
180+
, validator_options = ValidatorOptions
169181
},
170182
set_current_schema(NewState, JsonSchema).
171183

@@ -215,14 +227,22 @@ resolve_ref(State, Reference) ->
215227
Path = jesse_json_path:parse(Pointer),
216228
case load_local_schema(State#state.root_schema, Path) of
217229
?not_found ->
218-
jesse_error:handle_schema_invalid({?schema_not_found, CanonicalReference}, State);
230+
jesse_error:handle_schema_invalid( { ?schema_not_found
231+
, CanonicalReference
232+
}
233+
, State
234+
);
219235
Schema ->
220236
set_current_schema(State, Schema)
221237
end;
222238
false ->
223239
case load_schema(State, BaseURI) of
224240
?not_found ->
225-
jesse_error:handle_schema_invalid({?schema_not_found, CanonicalReference}, State);
241+
jesse_error:handle_schema_invalid( { ?schema_not_found
242+
, CanonicalReference
243+
}
244+
, State
245+
);
226246
RemoteSchema ->
227247
SchemaVer =
228248
jesse_json_path:value(?SCHEMA, RemoteSchema, ?default_schema_ver),
@@ -233,7 +253,11 @@ resolve_ref(State, Reference) ->
233253
Path = jesse_json_path:parse(Pointer),
234254
case load_local_schema(RemoteSchema, Path) of
235255
?not_found ->
236-
jesse_error:handle_schema_invalid({?schema_not_found, CanonicalReference}, State);
256+
jesse_error:handle_schema_invalid( { ?schema_not_found
257+
, CanonicalReference
258+
}
259+
, State
260+
);
237261
Schema ->
238262
set_current_schema(NewState, Schema)
239263
end
@@ -408,9 +432,25 @@ get_external_validator(#state{external_validator = Fun}) ->
408432
get_current_value(#state{current_value = Value}) -> Value.
409433

410434
-spec set_value(State :: state(), jesse:path(), jesse:json_term()) -> state().
411-
set_value(#state{setter_fun=undefined}=State, _Path, _Value) -> State;
412-
set_value(#state{current_value=undefined}=State, _Path, _Value) -> State;
413-
set_value(#state{setter_fun=Setter
414-
,current_value=Value
415-
}=State, Path, NewValue) ->
435+
set_value(#state{setter_fun = undefined} = State, _Path, _Value) -> State;
436+
set_value(#state{current_value = undefined} = State, _Path, _Value) -> State;
437+
set_value( #state{setter_fun = Setter
438+
, current_value = Value
439+
} = State
440+
, Path
441+
, NewValue
442+
) ->
416443
State#state{current_value = Setter(Path, NewValue, Value)}.
444+
445+
-spec validator_options(State :: state()) -> options().
446+
validator_options(#state{validator_options = Options}) ->
447+
Options.
448+
449+
-spec validator_option(Option :: atom(), State :: state()) -> any().
450+
validator_option(Option, #state{validator_options = Options}) ->
451+
proplists:get_value(Option, Options).
452+
453+
-spec validator_option(Option :: atom(), State :: state(), Default :: any()) ->
454+
any().
455+
validator_option(Option, #state{validator_options = Options}, Default) ->
456+
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)