37
37
, validate_with_schema /3
38
38
]).
39
39
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
41
52
]).
42
53
43
54
% % Includes
44
55
-include (" jesse_schema_validator.hrl" ).
45
56
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
+
46
103
% %% API
47
104
48
105
% % @doc Run from CLI with arguments.
@@ -54,40 +111,43 @@ main(Args) ->
54
111
% % a key `Key'. It will overwrite an existing schema with the same key if
55
112
% % there is any.
56
113
-spec add_schema ( Key :: string ()
57
- , Schema :: json_term ()
58
- ) -> ok | jesse_error :error ().
114
+ , Schema :: schema ()
115
+ ) -> ok
116
+ | jesse_error :error ().
59
117
add_schema (Key , Schema ) ->
60
118
ValidationFun = fun jesse_lib :is_json_object /1 ,
61
119
jesse_database :add (Key , Schema , ValidationFun ).
62
120
63
121
% % @doc Equivalent to `add_schema/2', but `Schema' is a binary string, and
64
122
% % the third agument is a parse function to convert the binary string to
65
123
% % 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 ().
70
129
add_schema (Key , Schema , Options ) ->
71
130
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 ),
73
132
ParsedSchema = try_parse (schema , ParserFun , Schema ),
74
133
add_schema (Key , ParsedSchema )
75
134
catch
76
- throw :Error -> {error , Error }
135
+ throw :Error ->
136
+ {error , Error }
77
137
end .
78
138
79
139
80
140
% % @doc Deletes a schema definition from in-memory storage associated with
81
141
% % the key `Key'.
82
- -spec del_schema (Key :: any ()) -> ok .
142
+ -spec del_schema (Key :: string ()) -> ok .
83
143
del_schema (Key ) ->
84
144
jesse_database :delete (Key ).
85
145
86
146
% % @doc Loads schema definitions from filesystem to in-memory storage.
87
147
% %
88
148
% % Equivalent to `load_schemas(Path, ParserFun, ValidationFun)'
89
149
% % where `ValidationFun' is `fun jesse_json:is_json_object/1'.
90
- -spec load_schemas ( Path :: string ()
150
+ -spec load_schemas ( Path :: string ()
91
151
, ParserFun :: fun ((binary ()) -> json_term ())
92
152
) -> jesse_database :update_result ().
93
153
load_schemas (Path , ParserFun ) ->
@@ -111,16 +171,16 @@ load_schemas(Path, ParserFun) ->
111
171
% % NOTE: it's impossible to automatically update schema definitions added by
112
172
% % add_schema/2, the only way to update them is to use add_schema/2
113
173
% % 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 ( )
117
177
) -> jesse_database :update_result ().
118
178
load_schemas (Path , ParserFun , ValidationFun ) ->
119
179
jesse_database :add_path (Path , ParserFun , ValidationFun ).
120
180
121
181
% % @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 ()
124
184
) -> {ok , json_term ()}
125
185
| jesse_error :error ()
126
186
| jesse_database :error ().
@@ -135,26 +195,27 @@ validate(Schema, Data) ->
135
195
% % to convert the binary string to a supported internal representation of json.
136
196
% % If `parser_fun' is not provided, then `Data' is considered to already be a
137
197
% % 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 ()
141
201
) -> {ok , json_term ()}
142
202
| jesse_error :error ()
143
203
| jesse_database :error ().
144
204
validate (Schema , Data , Options ) ->
145
205
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 ),
147
207
ParsedData = try_parse (data , ParserFun , Data ),
148
208
JsonSchema = jesse_database :load (Schema ),
149
209
jesse_schema_validator :validate (JsonSchema , ParsedData , Options )
150
210
catch
151
- throw :Error -> {error , Error }
211
+ throw :Error ->
212
+ {error , Error }
152
213
end .
153
214
154
215
% % @doc Equivalent to {@link validate_with_schema/3} where `Options'
155
216
% % 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 ()
158
219
) -> {ok , json_term ()}
159
220
| jesse_error :error ().
160
221
validate_with_schema (Schema , Data ) ->
@@ -168,16 +229,16 @@ validate_with_schema(Schema, Data) ->
168
229
% % supported internal representation of json.
169
230
% % If `parser_fun' is not provided, then both `Schema' and `Data' are considered
170
231
% % 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 ()
174
235
) -> {ok , json_term ()}
175
236
| jesse_error :error ().
176
237
validate_with_schema (Schema , Data , Options ) ->
177
238
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 ),
179
240
ParsedSchema = try_parse (schema , ParserFun , Schema ),
180
- ParsedData = try_parse (data , ParserFun , Data ),
241
+ ParsedData = try_parse (data , ParserFun , Data ),
181
242
jesse_schema_validator :validate (ParsedSchema , ParsedData , Options )
182
243
catch
183
244
throw :Error -> {error , Error }
@@ -192,7 +253,9 @@ try_parse(Type, ParserFun, JsonBin) ->
192
253
catch
193
254
_ :Error ->
194
255
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 }})
197
260
end
198
261
end .
0 commit comments