Skip to content

Commit 459b932

Browse files
committed
cleanup types, wip
1 parent 5b07d26 commit 459b932

File tree

3 files changed

+124
-101
lines changed

3 files changed

+124
-101
lines changed

src/jesse.erl

Lines changed: 93 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,69 @@
3737
, validate_with_schema/3
3838
]).
3939

40-
-export_type([ json_term/0
40+
-export_type([ allowed_errors/0
41+
, error_handler/0
42+
, error_list/0
43+
, external_validator/0
44+
, json_term/0
45+
, schema/0
46+
, schema_id/0
47+
, schema_ref/0
48+
, schema_ver/0
49+
, schema_loader_fun/0
50+
, option/0
51+
, options/0
4152
]).
4253

4354
%% Includes
4455
-include("jesse_schema_validator.hrl").
4556

57+
%% Internal datastructures
58+
-type allowed_errors() :: non_neg_integer()
59+
| ?infinity.
60+
61+
-type error_handler() :: fun(( jesse_error:error_reason()
62+
, [jesse_error:error_reason()]
63+
, non_neg_integer()
64+
) -> list()
65+
| no_return()
66+
).
67+
68+
-type error_list() :: list().
69+
70+
%% -type external_validator() :: fun((json_term(), state()) -> state())
71+
-type external_validator() :: fun((json_term(), any()) -> any())
72+
| undefined.
73+
74+
-type json_term() :: term().
75+
76+
-type parser_fun() :: fun((json_term() | binary()) -> json_term()).
77+
78+
-type schema() :: json_term().
79+
80+
-type schema_id() :: http_uri:uri() | undefined.
81+
82+
-type schema_ref() :: binary().
83+
84+
-type schema_ver() :: binary().
85+
86+
-type schema_loader_fun() :: fun((string()) -> {ok, schema()}
87+
| schema()
88+
| ?not_found
89+
).
90+
91+
-type option() :: {allowed_errors, allowed_errors()}
92+
| {default_schema_ver, schema_ver()}
93+
| {error_handler, error_handler()}
94+
| {external_validator, external_validator()}
95+
| {meta_schema_ver, schema_ver()}
96+
| {parser_fun, parser_fun()}
97+
| {schema_loader_fun, schema_loader_fun()}.
98+
99+
-type options() :: [option()].
100+
101+
-type validation_fun() :: fun((any()) -> boolean()).
102+
46103
%%% API
47104

48105
%% @doc Run from CLI with arguments.
@@ -54,40 +111,43 @@ main(Args) ->
54111
%% a key `Key'. It will overwrite an existing schema with the same key if
55112
%% there is any.
56113
-spec add_schema( Key :: string()
57-
, Schema :: json_term()
58-
) -> ok | jesse_error:error().
114+
, Schema :: schema()
115+
) -> ok
116+
| jesse_error:error().
59117
add_schema(Key, Schema) ->
60118
ValidationFun = fun jesse_lib:is_json_object/1,
61119
jesse_database:add(Key, Schema, ValidationFun).
62120

63121
%% @doc Equivalent to `add_schema/2', but `Schema' is a binary string, and
64122
%% the third agument is a parse function to convert the binary string to
65123
%% a supported internal representation of json.
66-
-spec add_schema( Key :: string()
67-
, Schema :: binary()
68-
, Options :: [{K :: atom(), V :: any()}]
69-
) -> ok | jesse_error:error().
124+
-spec add_schema( Key :: string()
125+
, Schema :: binary()
126+
, Options :: options()
127+
) -> ok
128+
| jesse_error:error().
70129
add_schema(Key, Schema, Options) ->
71130
try
72-
ParserFun = proplists:get_value(parser_fun, Options, fun(X) -> X end),
131+
ParserFun = proplists:get_value(parser_fun, Options, fun(X) -> X end),
73132
ParsedSchema = try_parse(schema, ParserFun, Schema),
74133
add_schema(Key, ParsedSchema)
75134
catch
76-
throw:Error -> {error, Error}
135+
throw:Error ->
136+
{error, Error}
77137
end.
78138

79139

80140
%% @doc Deletes a schema definition from in-memory storage associated with
81141
%% the key `Key'.
82-
-spec del_schema(Key :: any()) -> ok.
142+
-spec del_schema(Key :: string()) -> ok.
83143
del_schema(Key) ->
84144
jesse_database:delete(Key).
85145

86146
%% @doc Loads schema definitions from filesystem to in-memory storage.
87147
%%
88148
%% Equivalent to `load_schemas(Path, ParserFun, ValidationFun)'
89149
%% where `ValidationFun' is `fun jesse_json:is_json_object/1'.
90-
-spec load_schemas( Path :: string()
150+
-spec load_schemas( Path :: string()
91151
, ParserFun :: fun((binary()) -> json_term())
92152
) -> jesse_database:update_result().
93153
load_schemas(Path, ParserFun) ->
@@ -111,16 +171,16 @@ load_schemas(Path, ParserFun) ->
111171
%% NOTE: it's impossible to automatically update schema definitions added by
112172
%% add_schema/2, the only way to update them is to use add_schema/2
113173
%% again with the new definition.
114-
-spec load_schemas( Path :: string()
115-
, ParserFun :: fun((binary()) -> json_term())
116-
, ValidationFun :: fun((any()) -> boolean())
174+
-spec load_schemas( Path :: string()
175+
, ParserFun :: parser_fun()
176+
, ValidationFun :: validation_fun()
117177
) -> jesse_database:update_result().
118178
load_schemas(Path, ParserFun, ValidationFun) ->
119179
jesse_database:add_path(Path, ParserFun, ValidationFun).
120180

121181
%% @doc Equivalent to {@link validate/3} where `Options' is an empty list.
122-
-spec validate( Schema :: schema()
123-
, Data :: json_term() | binary()
182+
-spec validate( Schema :: schema() | binary()
183+
, Data :: json_term() | binary()
124184
) -> {ok, json_term()}
125185
| jesse_error:error()
126186
| jesse_database:error().
@@ -135,26 +195,27 @@ validate(Schema, Data) ->
135195
%% to convert the binary string to a supported internal representation of json.
136196
%% If `parser_fun' is not provided, then `Data' is considered to already be a
137197
%% supported internal representation of json.
138-
-spec validate( Schema :: schema()
139-
, Data :: json_term() | binary()
140-
, Options :: options()
198+
-spec validate( Schema :: schema() | binary()
199+
, Data :: json_term() | binary()
200+
, Options :: options()
141201
) -> {ok, json_term()}
142202
| jesse_error:error()
143203
| jesse_database:error().
144204
validate(Schema, Data, Options) ->
145205
try
146-
ParserFun = proplists:get_value(parser_fun, Options, fun(X) -> X end),
206+
ParserFun = proplists:get_value(parser_fun, Options, fun(X) -> X end),
147207
ParsedData = try_parse(data, ParserFun, Data),
148208
JsonSchema = jesse_database:load(Schema),
149209
jesse_schema_validator:validate(JsonSchema, ParsedData, Options)
150210
catch
151-
throw:Error -> {error, Error}
211+
throw:Error ->
212+
{error, Error}
152213
end.
153214

154215
%% @doc Equivalent to {@link validate_with_schema/3} where `Options'
155216
%% is an empty list.
156-
-spec validate_with_schema( Schema :: json_term() | binary()
157-
, Data :: json_term() | binary()
217+
-spec validate_with_schema( Schema :: schema() | binary()
218+
, Data :: json_term() | binary()
158219
) -> {ok, json_term()}
159220
| jesse_error:error().
160221
validate_with_schema(Schema, Data) ->
@@ -168,16 +229,16 @@ validate_with_schema(Schema, Data) ->
168229
%% supported internal representation of json.
169230
%% If `parser_fun' is not provided, then both `Schema' and `Data' are considered
170231
%% to already be a supported internal representation of json.
171-
-spec validate_with_schema( Schema :: json_term() | binary()
172-
, Data :: json_term() | binary()
173-
, Options :: options()
232+
-spec validate_with_schema( Schema :: schema() | binary()
233+
, Data :: json_term() | binary()
234+
, Options :: options()
174235
) -> {ok, json_term()}
175236
| jesse_error:error().
176237
validate_with_schema(Schema, Data, Options) ->
177238
try
178-
ParserFun = proplists:get_value(parser_fun, Options, fun(X) -> X end),
239+
ParserFun = proplists:get_value(parser_fun, Options, fun(X) -> X end),
179240
ParsedSchema = try_parse(schema, ParserFun, Schema),
180-
ParsedData = try_parse(data, ParserFun, Data),
241+
ParsedData = try_parse(data, ParserFun, Data),
181242
jesse_schema_validator:validate(ParsedSchema, ParsedData, Options)
182243
catch
183244
throw:Error -> {error, Error}
@@ -192,7 +253,9 @@ try_parse(Type, ParserFun, JsonBin) ->
192253
catch
193254
_:Error ->
194255
case Type of
195-
data -> throw({data_error, {parse_error, Error}});
196-
schema -> throw({schema_error, {parse_error, Error}})
256+
data ->
257+
throw({data_error, {parse_error, Error}});
258+
schema ->
259+
throw({schema_error, {parse_error, Error}})
197260
end
198261
end.

src/jesse_schema_validator.hrl

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -126,48 +126,3 @@
126126
%%
127127
-define(not_found, 'not_found').
128128
-define(infinity, 'infinity').
129-
130-
%%
131-
-type json_term() :: term().
132-
133-
-type allowed_errors() :: non_neg_integer()
134-
| ?infinity.
135-
136-
%% current path in reversed order
137-
-type current_path() :: [current_path_item()].
138-
-type current_path_item() :: binary() | non_neg_integer().
139-
140-
-type error_handler() :: fun(( jesse_error:error_reason()
141-
, [jesse_error:error_reason()]
142-
, non_neg_integer()
143-
) -> list()
144-
| no_return()
145-
).
146-
147-
-type error_list() :: list().
148-
149-
%% -type external_validator() :: fun((jesse:json_term(), state()) -> state())
150-
-type external_validator() :: fun((jesse:json_term(), any()) -> any())
151-
| undefined.
152-
153-
-type schema() :: jesse:json_term().
154-
155-
-type schema_id() :: http_uri:uri() | undefined.
156-
157-
-type schema_ref() :: binary().
158-
159-
-type schema_ver() :: binary().
160-
161-
-type schema_loader_fun() :: fun((string()) -> {ok, schema()}
162-
| schema()
163-
| ?not_found
164-
).
165-
166-
-type option() :: {allowed_errors, allowed_errors()}
167-
| {default_schema_ver, schema_ver()}
168-
| {error_handler, error_handler()}
169-
| {external_validator, external_validator()}
170-
| {meta_schema_ver, schema_ver()}
171-
| {schema_loader_fun, schema_loader_fun()}.
172-
173-
-type options() :: [option()].

0 commit comments

Comments
 (0)