-
Notifications
You must be signed in to change notification settings - Fork 461
/
crossbar_validator.erl
435 lines (373 loc) · 18.1 KB
/
crossbar_validator.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
%%%-------------------------------------------------------------------
%%% @author Karl Anderson <karl@2600hz.org>
%%% @copyright (C) 2011, Karl Anderson
%%% @doc
%%%
%%% @end
%%% Created : 19 Jan 2011 by Karl Anderson <karl@2600hz.org>
%%%-------------------------------------------------------------------
-module(crossbar_validator).
-include("../include/crossbar.hrl").
-export([validate/2, check/4]).
-export([required/1]).
-export([not_empty/1, is_type/2, is_format/2]).
-export([numeric_min/2, numeric_max/2, numeric_between/3]).
-export([width/2, width/3]).
-import(logger, [format_log/3]).
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Runs the check function on all fields defined in definitions, on
%% data
%% @end
%%--------------------------------------------------------------------
-spec(validate/2 :: (Schema :: couch_schema(), JObj :: mochijson()) -> json_objects()).
validate(Schema, JObj) ->
lists:foldl(fun({Path, Rules}, Results) ->
check(Rules, Path, JObj, Results)
end, [], Schema).
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Runs the validation rules on the data at the path, returns a list
%% of json objects (in mochiweb notation) of failed validators and
%% the parameters provided to each
%% @end
%%--------------------------------------------------------------------
-spec(check/4 :: (Rules :: validator_rules(), Path :: [binary()] | binary(), JObj :: mochijson(), Acc0 :: json_objects()) -> json_objects()).
check(Rules, Path, JObj, Acc0) ->
Value = wh_json:get_value(Path, JObj),
lists:foldl(fun({Validator, Params}, Results) ->
case apply(?MODULE, Validator, [Value] ++ Params) of
true ->
Results;
false ->
[{struct, [{<<"path">>, Path}
,{<<"validator">>, Validator}
,{<<"parameters">>, Params}]
} | Results]
end
end, Acc0, Rules).
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Returns true if the value is not undefined
%% @end
%%--------------------------------------------------------------------
-spec(required/1 :: (Value :: term()) -> boolean()).
required(Value) ->
if
Value =:= undefined -> false;
true -> true
end.
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Returns true if the value is not considered empty for its type
%% @end
%%--------------------------------------------------------------------
-spec(not_empty/1 :: (Value :: term()) -> boolean()).
not_empty(Value) ->
if
Value =:= undefined -> false;
is_atom(Value) -> true;
is_number(Value) -> true;
is_list(Value) -> length(Value) /= 0;
is_binary(Value) -> Value =/= <<"">>;
true -> false
end.
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Returns true if the value is of the type specified
%% @end
%%--------------------------------------------------------------------
-spec(is_type/2 :: (Value :: term(), Type :: number|float|integer|string|boolean|array|object) -> boolean()).
is_type(undefined, _Type) ->
false;
is_type(Value, Type) ->
case Type of
number -> is_number(Value);
float -> is_float(Value);
integer -> is_integer(Value);
string -> is_atom(Value) orelse is_format(Value, line);
boolean ->
if
is_boolean(Value) -> true;
Value =:= <<"true">> -> true;
Value =:= <<"false">> -> true;
true -> false
end;
array -> is_list(Value);
object -> try {struct, _} = Value, true catch _:_ -> false end;
_ -> false
end.
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Returns true if the value is in the format specified
%% Most of this function was drawn from: http://www.trapexit.org/List_Generators
%% @end
%%--------------------------------------------------------------------
-spec(is_format/2 :: (Value :: term(), Format :: ascii|graph|print|line|space|blank|punct|word|alpha|numeric|hex|phrase|alpha_numeric) -> boolean()).
is_format(Value, Format) when is_atom(Value) ->
Value =/= undefined andalso is_format(atom_to_list(Value), Format);
is_format(Value, Format) when is_binary(Value) ->
is_format(binary_to_list(Value), Format);
is_format(Value, Format) when is_list(Value) ->
case Format of
%% ASCII characters ([\x00-\x7F])
ascii ->
Chars = lists:seq(0, 127),
lists:all(fun(Elem) -> lists:member(Elem, Chars) end, Value);
%% Visible characters (i.e. anything except spaces, control
%% characters, etc.) ([\x21-\x7E])
graph ->
Chars = lists:seq(33, 126),
lists:all(fun(Elem) -> lists:member(Elem, Chars) end, Value);
%% Visible characters and spaces (i.e. anything except control
%% characters, etc.) ([\x20-\x7E])
print ->
Chars = lists:seq(32, 126) ++ [$\t],
lists:all(fun(Elem) -> lists:member(Elem, Chars) end, Value);
%% Visible characters, spaces, and select control characters
line ->
Chars = lists:seq(32, 126) ++ [$ , $\t, $\r, $\n],
lists:all(fun(Elem) -> lists:member(Elem, Chars) end, Value);
%% All whitespace characters, including line breaks ([ \t\r\n\v\f])
space ->
Chars = [$ , $\t, $\r, $\n, $\v, $\f],
lists:all(fun(Elem) -> lists:member(Elem, Chars) end, Value);
%% Space and tab ([ \t])
blank ->
Chars = [$ , $\t],
lists:all(fun(Elem) -> lists:member(Elem, Chars) end, Value);
%% Punctuation and symbols ([!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~])
punct ->
Chars = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}\~",
lists:all(fun(Elem) -> lists:member(Elem, Chars) end, Value);
%% Word characters (letters, numbers and underscores) ([A-Za-z0-9_])
word ->
Chars = lists:seq($a, $z) ++ lists:seq($A, $Z) ++ lists:seq($0, $9) ++ "_",
lists:all(fun(Elem) -> lists:member(Elem, Chars) end, Value);
%% Alphabetic characters ([a-zA-Z])
alpha ->
Chars = lists:seq($a, $z) ++ lists:seq($A, $Z),
lists:all(fun(Elem) -> lists:member(Elem, Chars) end, Value);
%% Digits ([0-9])
numeric ->
Chars = lists:seq($0, $9),
lists:all(fun(Elem) -> lists:member(Elem, Chars) end, Value);
%% Hexadecimal digits ([A-Fa-f0-9])
hex ->
Chars = lists:seq($A, $F) ++ lists:seq($a, $f) ++ lists:seq($0, $9),
lists:all(fun(Elem) -> lists:member(Elem, Chars) end, Value);
%% Letters, numbers, underscores and whitspace
phrase ->
Chars = [$ , $\t],
is_format(Value, word) orelse lists:all(fun(Elem) -> lists:member(Elem, Chars) end, Value);
%% Alphanumeric characters ([a-zA-Z0-9]).
alpha_numeric ->
is_format(Value, alpha) orelse is_format(Value, numeric);
_ -> false
end;
is_format(_Value, _Format) ->
false.
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Returns true if the numerical value is less than or equal to the limit
%% @end
%%--------------------------------------------------------------------
-spec(numeric_min/2 :: (Value :: term(), Limit :: number()) -> boolean()).
numeric_min(Value, Limit) ->
if
not is_number(Value) -> false;
not is_number(Limit) -> false;
Value >= Limit -> true;
true -> false
end.
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Returns true if the numerical value is greater than or equal to the limit
%% @end
%%--------------------------------------------------------------------
-spec(numeric_max/2 :: (Value :: term(), Limit :: number()) -> boolean()).
numeric_max(Value, Limit) ->
if
not is_number(Value) -> false;
not is_number(Limit) -> false;
Value =< Limit -> true;
true -> false
end.
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Returns true if the numerical is within the given range, inclusive
%% @end
%%--------------------------------------------------------------------
-spec(numeric_between/3 :: (Value :: term(), Min :: number(), Max :: number()) -> boolean()).
numeric_between(Value, Min, Max) ->
Min =< Max andalso numeric_min(Value, Min) andalso numeric_max(Value, Max).
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Returns true if the length (of a string or array) is greater than
%% or equal to the min
%% @end
%%--------------------------------------------------------------------
-spec(width/2 :: (Value :: term(), Min :: integer()) -> boolean()).
width(Value, Min) when is_atom(Value) ->
Value =/= undefined andalso width(atom_to_list(Value), Min);
width(Value, Min) when is_binary(Value) ->
width(binary_to_list(Value), Min);
width(Value, Min) when is_list(Value) ->
length(Value) >= Min;
width(_Value, _Min) ->
false.
%%--------------------------------------------------------------------
%% @public
%% @doc
%% Returns true if the length (of a string or array) is within the
%% size range, inclusive
%% @end
%%--------------------------------------------------------------------
-spec(width/3 :: (Value :: term(), Min :: integer(), Max :: integer()) -> boolean()).
width(Value, Min, Max) when is_atom(Value) ->
Value =/= undefined andalso width(atom_to_list(Value), Min, Max);
width(Value, Min, Max) when is_binary(Value) ->
width(binary_to_list(Value), Min, Max);
width(Value, Min, Max) when is_list(Value) ->
length(Value) >= Min andalso length(Value) =< Max;
width(_Value, _Min, _Max) ->
false.
%% EUNIT TESTING
-include_lib("eunit/include/eunit.hrl").
-ifdef(TEST).
%% required
required_test() ->
Succeed = [<<"foo">>, <<"">>, foo, 5, false, true, [], ["foo", "bar"]],
Fail = [undefined],
validate(Succeed, Fail, fun required/1).
%% not_empty
not_empty_test() ->
Succeed = [<<"foo">>, foo, 5, false, true, ["foo", "bar"]],
Fail = [undefined, <<"">>, "", []],
validate(Succeed, Fail, fun not_empty/1).
%% is_type
is_type_number_test() ->
Succeed = [-10, 0, 10, 2.71828183, 3.14159265],
Fail = [undefined, string, <<"">>, <<"Test">>, <<"true">>, <<"false">>, true, false, [], [foo, <<"bar">>], {struct, [{<<"key">>, []}]}],
validate(Succeed, Fail, fun(E) -> is_type(E, number) end).
is_type_float_test() ->
Succeed = [2.71828183, 3.14159265],
Fail = [undefined, -10, 0, 10, string, <<"">>, <<"Test">>, <<"true">>, <<"false">>, true, false, [], [foo, <<"bar">>], {struct, [{<<"key">>, []}]}],
validate(Succeed, Fail, fun(E) -> is_type(E, float) end).
is_type_integer_test() ->
Succeed = [-10, -1, 0, 1, 10],
Fail = [undefined, 2.71828183, 3.14159265, string, <<"">>, <<"Test">>, <<"true">>, <<"false">>, true, false, [], [foo, <<"bar">>], {struct, [{<<"key">>, []}]}],
validate(Succeed, Fail, fun(E) -> is_type(E, integer) end).
is_type_string_test() ->
Succeed = [string, <<"">>, <<"Test">>, <<"true">>, <<"false">>, true, false, []],
Fail = [undefined, -10, -1, 0, 1, 10, 2.71828183, 3.14159265, <<"bell", 7>>, [foo, <<"bar">>], {struct, [{<<"key">>, []}]}],
validate(Succeed, Fail, fun(E) -> is_type(E, string) end).
is_type_boolean_test() ->
Succeed = [<<"true">>, <<"false">>, true, false],
Fail = [undefined, -10, -1, 0, 1, 10, 2.71828183, 3.14159265, string, <<"">>, <<"Test">>, [], [foo, <<"bar">>], {struct, [{<<"key">>, []}]}],
validate(Succeed, Fail, fun(E) -> is_type(E, boolean) end).
is_type_array_test() ->
Succeed = [[], [foo, <<"bar">>]],
Fail = [undefined, -10, -1, 0, 1, 10, 2.71828183, 3.14159265, string, <<"">>, <<"Test">>, <<"true">>, <<"false">>, true, false, {struct, [{<<"key">>, []}]}],
validate(Succeed, Fail, fun(E) -> is_type(E, array) end).
is_type_object_test() ->
Succeed = [{struct, [{<<"key">>, []}]}],
Fail = [undefined, -10, -1, 0, 1, 10, 2.71828183, 3.14159265, string, <<"">>, <<"Test">>, <<"true">>, <<"false">>, true, false, [], [foo, <<"bar">>]],
validate(Succeed, Fail, fun(E) -> is_type(E, object) end).
%% is_format
is_format_ascii_test() ->
Succeed = lists:seq(0, 127),
Fail = [undefined, -10, -1, 3.14159265, 128, 200, {struct, [{<<"key">>, []}]}],
validate(Succeed, Fail, fun(E) -> is_format([E], ascii) end).
is_format_graph_test() ->
Succeed = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
Fail = [undefined, $ , $\t, $\r, $\n, $\v, $\f, -10, -1, 3.14159265, 128, 200, {struct, [{<<"key">>, []}]}],
validate(Succeed, Fail, fun(E) -> is_format([E], graph) end).
is_format_print_test() ->
Succeed = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ++ [$ , $\t],
Fail = [undefined, $\r, $\n, $\v, $\f, -10, -1, 3.14159265, 128, 200, {struct, [{<<"key">>, []}]}],
validate(Succeed, Fail, fun(E) -> is_format([E], print) end).
is_format_line_test() ->
Succeed = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ++ [$ , $\t, $\r, $\n],
Fail = [undefined, $\v, $\f, -10, -1, 3.14159265, 128, 200, {struct, [{<<"key">>, []}]}],
validate(Succeed, Fail, fun(E) -> is_format([E], line) end).
is_format_space_test() ->
Succeed = [$ , $\t, $\r, $\n, $\v, $\f],
Fail = [undefined, -10, -1, 3.14159265, 128, 200, {struct, [{<<"key">>, []}]}] ++ "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
validate(Succeed, Fail, fun(E) -> is_format([E], space) end).
is_format_blank_test() ->
Succeed = [$ , $\t],
Fail = [undefined, $\r, $\n, $\v, $\f, -10, -1, 3.14159265, 128, 200, {struct, [{<<"key">>, []}]}] ++ "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
validate(Succeed, Fail, fun(E) -> is_format([E], blank) end).
is_format_punct_test() ->
Succeed = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}\~",
Fail = [undefined, $ , $\t, $\r, $\n, $\v, $\f, -10, -1, 3.14159265, 128, 200, {struct, [{<<"key">>, []}]}] ++ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
validate(Succeed, Fail, fun(E) -> is_format([E], punct) end).
is_format_word_test() ->
Succeed = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz",
Fail = [undefined, $ , $\t, $\r, $\n, $\v, $\f, -10, -1, 3.14159265, 128, 200, {struct, [{<<"key">>, []}]}] ++ "!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~",
validate(Succeed, Fail, fun(E) -> is_format([E], word) end).
is_format_alpha_test() ->
Succeed = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
Fail = [undefined, $ , $\t, $\r, $\n, $\v, $\f, -10, -1, 3.14159265, 128, 200, {struct, [{<<"key">>, []}]}] ++ "!\"#$%&'()*+,-./0123456789:;<=>?@[\\]^_`{|}~",
validate(Succeed, Fail, fun(E) -> is_format([E], alpha) end).
is_format_numeric_test() ->
Succeed = "0123456789",
Fail = [undefined, $ , $\t, $\r, $\n, $\v, $\f, -10, -1, 3.14159265, 128, 200, {struct, [{<<"key">>, []}]}] ++ "!\"#$%&'()*+,-./:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
validate(Succeed, Fail, fun(E) -> is_format([E], numeric) end).
is_format_hex_test() ->
Succeed = "0123456789ABCDEFabcdef",
Fail = [undefined, $ , $\t, $\r, $\n, $\v, $\f, -10, -1, 3.14159265, 128, 200, {struct, [{<<"key">>, []}]}] ++ "!\"#$%&'()*+,-./:;<=>?@GHIJKLMNOPQRSTUVWXYZ[\\]^_`ghijklmnopqrstuvwxyz{|}~",
validate(Succeed, Fail, fun(E) -> is_format([E], hex) end).
is_format_phrase_test() ->
Succeed = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_" ++ [$ , $\t],
Fail = [undefined, $\r, $\n, $\v, $\f, -10, -1, 3.14159265, 128, 200, {struct, [{<<"key">>, []}]}] ++ "!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~",
validate(Succeed, Fail, fun(E) -> is_format([E], phrase) end).
is_format_alpha_numeric_test() ->
Succeed = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
Fail = [undefined, $ , $\t, $\r, $\n, $\v, $\f, -10, -1, 3.14159265, 128, 200, {struct, [{<<"key">>, []}]}] ++ "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
validate(Succeed, Fail, fun(E) -> is_format([E], alpha_numeric) end).
%% numeric_min
numeric_min_test() ->
Succeed = [0, 0.5, 1, 20, 30, 100],
Fail = [undefined, -100, -30, -20, -10, -0.01, string, <<"">>, <<"Test">>, false, [foo, <<"bar">>], {struct, [{<<"key">>, []}]}],
validate(Succeed, Fail, fun(E) -> numeric_min(E, 0) end).
%% numeric_max
numeric_max_test() ->
Succeed = [-100, -30, -20, -10, -0.1, 0],
Fail = [undefined, 0.5, 1, 20, 30, 100, string, <<"">>, <<"Test">>, false, [foo, <<"bar">>], {struct, [{<<"key">>, []}]}],
validate(Succeed, Fail, fun(E) -> numeric_max(E, 0) end).
%% numeric_between
numeric_between_test() ->
Succeed = [0, 0.375, 1, 5, 6, 10],
Fail = [undefined, -10, -1, 10.1, 20, 30, 50, string, <<"">>, <<"Test">>, false, [foo, <<"bar">>], {struct, [{<<"key">>, []}]}],
?assertEqual(false, numeric_between(10, 10, 0)),
validate(Succeed, Fail, fun(E) -> numeric_between(E, 0, 10) end).
%% width
width_test() ->
Succeed1 = [test, "test", <<"test">>],
Fail1 = [undefined, 0.5, 1, 20, 30, 100, foo, <<"">>, <<"bar">>, [foo, <<"bar">>], {struct, [{<<"key">>, []}]}],
validate(Succeed1, Fail1, fun(E) -> width(E, 4) end),
Succeed2 = [test, "test", "test2", <<"test2">>],
Fail2 = [undefined, 0.5, 1, 20, 30, 100, foo, <<"">>, <<"bar">>, test_this, <<"Hello World!">>, [foo, <<"bar">>], {struct, [{<<"key">>, []}]}],
validate(Succeed2, Fail2, fun(E) -> width(E, 4, 5) end).
validate(Succeed, Fail, Func) ->
lists:foreach(fun(Elem) ->
io:format("~p~n", [Elem]),
?assertEqual(true, Func(Elem))
end, Succeed),
lists:foreach(fun(Elem) ->
io:format("~p~n", [Elem]),
?assertEqual(false, Func(Elem))
end, Fail).
-endif.