Skip to content

Commit 1d3dcd3

Browse files
committed
cleanup types, wip
1 parent 2fefd9e commit 1d3dcd3

File tree

2 files changed

+92
-61
lines changed

2 files changed

+92
-61
lines changed

src/jesse_schema_validator.hrl

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
%% @end
2222
%%%=============================================================================
2323

24+
%% Maps conditional compilation
25+
-ifdef(erlang_deprecated_types).
26+
-define(IF_MAPS(Exp), ).
27+
-else.
28+
-define(IF_MAPS(Exp), Exp).
29+
-endif.
30+
2431
%% Constant definitions for Json schema keywords
2532
-define(SCHEMA, <<"$schema">>).
2633
-define(TYPE, <<"type">>).
@@ -120,9 +127,46 @@
120127
-define(not_found, 'not_found').
121128
-define(infinity, 'infinity').
122129

123-
%% Maps conditional compilation
124-
-ifdef(erlang_deprecated_types).
125-
-define(IF_MAPS(Exp), ).
126-
-else.
127-
-define(IF_MAPS(Exp), Exp).
128-
-endif.
130+
%%
131+
-type allowed_errors() :: non_neg_integer()
132+
| ?infinity.
133+
134+
%% current path in reversed order
135+
-type current_path() :: [current_path_item()].
136+
-type current_path_item() :: binary() | non_neg_integer().
137+
138+
-type error_handler() :: fun(( jesse_error:error_reason()
139+
, [jesse_error:error_reason()]
140+
, non_neg_integer()
141+
) -> list()
142+
| no_return()
143+
).
144+
145+
-type error_list() :: list().
146+
147+
%% -type external_validator() :: fun((jesse:json_term(), state()) -> state())
148+
-type external_validator() :: fun((jesse:json_term(), any()) -> any())
149+
| undefined.
150+
151+
-type schema() :: jesse:json_term().
152+
153+
-type schema_id() :: http_uri:uri() | undefined.
154+
155+
-type schema_ref() :: binary().
156+
157+
-type schema_ver() :: binary().
158+
159+
-type schema_loader_fun() :: fun((string()) -> {ok, schema()}
160+
| schema()
161+
| ?not_found
162+
).
163+
164+
-record( options
165+
, { allowed_errors :: allowed_errors()
166+
, default_schema_ver :: schema_ver()
167+
, error_handler :: error_handler()
168+
, external_validator :: external_validator()
169+
, meta_schema_ver :: schema_ver()
170+
, schema_loader_fun :: schema_loader_fun()
171+
}
172+
).

src/jesse_state.erl

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -46,105 +46,86 @@
4646

4747
-export_type([ state/0
4848
, allowed_errors/0
49+
, options/0
4950
]).
5051

5152
%% Includes
5253
-include("jesse_schema_validator.hrl").
5354

5455
%% Internal datastructures
5556
-record( state
56-
, { root_schema :: jesse:json_term()
57-
, current_schema :: jesse:json_term()
58-
, current_path :: [binary() | non_neg_integer()]
59-
%% current path in reversed order
57+
, { root_schema :: schema()
58+
, current_schema :: schema()
59+
, current_path :: current_path()
6060
, allowed_errors :: allowed_errors()
61-
, error_list :: list()
62-
, error_handler :: fun(( jesse_error:error_reason()
63-
, [jesse_error:error_reason()]
64-
, non_neg_integer()
65-
) -> list()
66-
| no_return()
67-
)
68-
, default_schema_ver :: binary()
69-
, schema_loader_fun :: fun((string()) -> {ok, jesse:json_term()}
70-
| jesse:json_term()
71-
| ?not_found
72-
)
61+
, error_list :: error_list()
62+
, error_handler :: error_handler()
63+
, default_schema_ver :: schema_ver()
64+
, schema_loader_fun :: schema_loader_fun()
7365
, external_validator :: external_validator()
74-
, id :: http_uri:uri() | undefined
66+
, id :: schema_id()
7567
}
7668
).
7769

78-
-type allowed_errors() :: non_neg_integer()
79-
| ?infinity.
80-
81-
-type external_validator() :: fun((jesse:json_term(), state()) -> state())
82-
| undefined.
83-
70+
-opaque options() :: #options{}.
8471
-opaque state() :: #state{}.
8572

8673
%%% API
8774
%% @doc Adds `Property' to the `current_path' in `State'.
88-
-spec add_to_path(State :: state(),
89-
Property :: binary() | non_neg_integer()) -> state().
90-
add_to_path(State, Property) ->
75+
-spec add_to_path( State :: state()
76+
, Item :: current_path_item()
77+
) -> state().
78+
add_to_path(State, Item) ->
9179
CurrentPath = State#state.current_path,
92-
State#state{current_path = [Property | CurrentPath]}.
80+
State#state{current_path = [Item | CurrentPath]}.
9381

9482
%% @doc Getter for `allowed_errors'.
9583
-spec get_allowed_errors(State :: state()) -> allowed_errors().
9684
get_allowed_errors(#state{allowed_errors = AllowedErrors}) ->
9785
AllowedErrors.
9886

9987
%% @doc Getter for `current_path'.
100-
-spec get_current_path(State :: state()) -> [binary() | non_neg_integer()].
88+
-spec get_current_path(State :: state()) -> current_path().
10189
get_current_path(#state{current_path = CurrentPath}) ->
10290
CurrentPath.
10391

10492
%% @doc Getter for `current_schema'.
105-
-spec get_current_schema(State :: state()) -> jesse:json_term().
93+
-spec get_current_schema(State :: state()) -> schema().
10694
get_current_schema(#state{current_schema = CurrentSchema}) ->
10795
CurrentSchema.
10896

10997
%% @doc Getter for `current_schema_id'.
110-
-spec get_current_schema_id(State :: state()) -> binary() | undefined.
98+
-spec get_current_schema_id(State :: state()) -> schema_id().
11199
get_current_schema_id(#state{ current_schema = CurrentSchema
112100
, root_schema = RootSchema
113101
}) ->
114102
Default = jesse_json_path:value(?ID, RootSchema, ?not_found),
115103
jesse_json_path:value(?ID, CurrentSchema, Default).
116104

117105
%% @doc Getter for `default_schema_ver'.
118-
-spec get_default_schema_ver(State :: state()) -> binary().
106+
-spec get_default_schema_ver(State :: state()) -> schema_ver().
119107
get_default_schema_ver(#state{default_schema_ver = SchemaVer}) ->
120108
SchemaVer.
121109

122110
%% @doc Getter for `error_handler'.
123-
-spec get_error_handler(State :: state()) -> fun(( jesse_error:error_reason()
124-
, [jesse_error:error_reason()]
125-
, non_neg_integer()
126-
) -> list() | no_return()).
111+
-spec get_error_handler(State :: state()) -> error_handler().
127112
get_error_handler(#state{error_handler = ErrorHandler}) ->
128113
ErrorHandler.
129114

130115
%% @doc Getter for `error_list'.
131-
-spec get_error_list(State :: state()) -> list().
116+
-spec get_error_list(State :: state()) -> error_list().
132117
get_error_list(#state{error_list = ErrorList}) ->
133118
ErrorList.
134119

135120
%% @doc Returns newly created state.
136-
-spec new( JsonSchema :: jesse:json_term()
137-
, Options :: [{Key :: atom(), Data :: any()}]
121+
-spec new( JsonSchema :: schema()
122+
, Options :: options()
138123
) -> state().
139124
new(JsonSchema, Options) ->
140-
ErrorHandler = proplists:get_value( error_handler
141-
, Options
142-
, ?default_error_handler_fun
143-
),
144-
AllowedErrors = proplists:get_value( allowed_errors
145-
, Options
146-
, 0
147-
),
125+
AllowedErrors = proplists:get_value( allowed_errors
126+
, Options
127+
, 0
128+
),
148129
MetaSchemaVer = jesse_json_path:value( ?SCHEMA
149130
, JsonSchema
150131
, ?default_schema_ver
@@ -153,13 +134,17 @@ new(JsonSchema, Options) ->
153134
, Options
154135
, MetaSchemaVer
155136
),
137+
ErrorHandler = proplists:get_value( error_handler
138+
, Options
139+
, ?default_error_handler_fun
140+
),
141+
ExternalValidator = proplists:get_value( external_validator
142+
, Options
143+
),
156144
LoaderFun = proplists:get_value( schema_loader_fun
157145
, Options
158146
, ?default_schema_loader_fun
159147
),
160-
ExternalValidator = proplists:get_value( external_validator
161-
, Options
162-
),
163148
NewState = #state{ root_schema = JsonSchema
164149
, current_path = []
165150
, allowed_errors = AllowedErrors
@@ -185,19 +170,19 @@ set_allowed_errors(#state{} = State, AllowedErrors) ->
185170

186171
%% @doc Setter for `current_schema'.
187172
-spec set_current_schema( State :: state()
188-
, NewSchema :: jesse:json_term()
173+
, NewSchema :: schema()
189174
) -> state().
190175
set_current_schema(#state{id = Id} = State, NewSchema) ->
191176
NewId = combine_id(Id, jesse_json_path:value(?ID, NewSchema, undefined)),
192177
State#state{current_schema = NewSchema, id = NewId}.
193178

194179
%% @doc Setter for `error_list'.
195-
-spec set_error_list(State :: state(), ErrorList :: list()) -> state().
180+
-spec set_error_list(State :: state(), ErrorList :: error_list()) -> state().
196181
set_error_list(State, ErrorList) ->
197182
State#state{error_list = ErrorList}.
198183

199184
%% @doc Resolve a reference.
200-
-spec resolve_ref(State :: state(), Reference :: binary()) -> state().
185+
-spec resolve_ref(State :: state(), Reference :: schema_ref()) -> state().
201186
resolve_ref(State, Reference) ->
202187
Id = State#state.id,
203188
CanonicalReference = combine_id(Id, Reference),
@@ -253,7 +238,7 @@ undo_resolve_ref(RefState, OriginalState) ->
253238

254239
%% @doc Retrieve a specific part of a schema
255240
%% @private
256-
-spec load_local_schema( Schema :: not_found | jesse:json_term()
241+
-spec load_local_schema( Schema :: ?not_found | schema()
257242
, Path :: [binary()]
258243
) -> not_found | jesse:json_term().
259244
load_local_schema(?not_found, _Path) ->
@@ -378,8 +363,10 @@ raw_canonical_path2([H|T], Acc) ->
378363
end.
379364

380365
%% @doc Load a schema based on URI
381-
-spec load_schema(State :: state(), SchemaURI :: string() | binary()) ->
382-
jesse:json_term() | ?not_found.
366+
-spec load_schema( State :: state()
367+
, SchemaURI :: string() | binary()
368+
) -> schema()
369+
| ?not_found.
383370
load_schema(State, SchemaURI) when is_binary(SchemaURI) ->
384371
load_schema(State, unicode:characters_to_list(SchemaURI));
385372
load_schema(#state{schema_loader_fun = LoaderFun}, SchemaURI) ->

0 commit comments

Comments
 (0)