forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-validators.js
87 lines (75 loc) · 2.1 KB
/
test-validators.js
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
// Flags: --expose-internals
'use strict';
require('../common');
const assert = require('assert');
const {
validateArray,
validateBoolean,
validateInteger,
validateObject,
} = require('internal/validators');
const { MAX_SAFE_INTEGER, MIN_SAFE_INTEGER } = Number;
const outOfRangeError = {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
};
const invalidArgTypeError = {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
};
const invalidArgValueError = {
code: 'ERR_INVALID_ARG_VALUE',
name: 'TypeError',
};
{
// validateInteger tests.
// validateInteger() defaults to validating safe integers.
validateInteger(MAX_SAFE_INTEGER, 'foo');
validateInteger(MIN_SAFE_INTEGER, 'foo');
assert.throws(() => {
validateInteger(MAX_SAFE_INTEGER + 1, 'foo');
}, outOfRangeError);
assert.throws(() => {
validateInteger(MIN_SAFE_INTEGER - 1, 'foo');
}, outOfRangeError);
// validateInteger() works with unsafe integers.
validateInteger(MAX_SAFE_INTEGER + 1, 'foo', 0, MAX_SAFE_INTEGER + 1);
validateInteger(MIN_SAFE_INTEGER - 1, 'foo', MIN_SAFE_INTEGER - 1);
}
{
// validateArray tests.
validateArray([], 'foo');
validateArray([1, 2, 3], 'foo');
[undefined, null, true, false, 0, 0.0, 42, '', 'string', {}]
.forEach((val) => {
assert.throws(() => {
validateArray(val, 'foo');
}, invalidArgTypeError);
});
validateArray([1], 'foo', { minLength: 1 });
assert.throws(() => {
validateArray([], 'foo', { minLength: 1 });
}, invalidArgValueError);
}
{
// validateBoolean tests.
validateBoolean(true, 'foo');
validateBoolean(false, 'foo');
[undefined, null, 0, 0.0, 42, '', 'string', {}, []].forEach((val) => {
assert.throws(() => {
validateBoolean(val, 'foo');
}, invalidArgTypeError);
});
}
{
// validateObject tests.
validateObject({}, 'foo');
validateObject({ a: 42, b: 'foo' }, 'foo');
[undefined, null, true, false, 0, 0.0, 42, '', 'string', []]
.forEach((val) => {
assert.throws(() => {
validateObject(val, 'foo');
}, invalidArgTypeError);
});
validateObject(null, 'foo', { nullable: true });
}