• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2'use strict';
3
4require('../common');
5const assert = require('assert');
6const {
7  validateArray,
8  validateBoolean,
9  validateInteger,
10  validateNumber,
11  validateObject,
12  validateString,
13} = require('internal/validators');
14const { MAX_SAFE_INTEGER, MIN_SAFE_INTEGER } = Number;
15const outOfRangeError = {
16  code: 'ERR_OUT_OF_RANGE',
17  name: 'RangeError',
18};
19const invalidArgTypeError = {
20  code: 'ERR_INVALID_ARG_TYPE',
21  name: 'TypeError',
22};
23const invalidArgValueError = {
24  code: 'ERR_INVALID_ARG_VALUE',
25  name: 'TypeError',
26};
27
28{
29  // validateInteger tests.
30
31  // validateInteger() defaults to validating safe integers.
32  validateInteger(MAX_SAFE_INTEGER, 'foo');
33  validateInteger(MIN_SAFE_INTEGER, 'foo');
34  assert.throws(() => {
35    validateInteger(MAX_SAFE_INTEGER + 1, 'foo');
36  }, outOfRangeError);
37  assert.throws(() => {
38    validateInteger(MIN_SAFE_INTEGER - 1, 'foo');
39  }, outOfRangeError);
40
41  // validateInteger() works with unsafe integers.
42  validateInteger(MAX_SAFE_INTEGER + 1, 'foo', 0, MAX_SAFE_INTEGER + 1);
43  validateInteger(MIN_SAFE_INTEGER - 1, 'foo', MIN_SAFE_INTEGER - 1);
44}
45
46{
47  // validateArray tests.
48  validateArray([], 'foo');
49  validateArray([1, 2, 3], 'foo');
50
51  [undefined, null, true, false, 0, 0.0, 42, '', 'string', {}]
52    .forEach((val) => {
53      assert.throws(() => {
54        validateArray(val, 'foo');
55      }, invalidArgTypeError);
56    });
57
58  validateArray([1], 'foo', 1);
59  assert.throws(() => {
60    validateArray([], 'foo', 1);
61  }, invalidArgValueError);
62}
63
64{
65  // validateBoolean tests.
66  validateBoolean(true, 'foo');
67  validateBoolean(false, 'foo');
68
69  [undefined, null, 0, 0.0, 42, '', 'string', {}, []].forEach((val) => {
70    assert.throws(() => {
71      validateBoolean(val, 'foo');
72    }, invalidArgTypeError);
73  });
74}
75
76{
77  // validateObject tests.
78  validateObject({}, 'foo');
79  validateObject({ a: 42, b: 'foo' }, 'foo');
80
81  [undefined, null, true, false, 0, 0.0, 42, '', 'string', [], () => {}]
82    .forEach((val) => {
83      assert.throws(() => {
84        validateObject(val, 'foo');
85      }, invalidArgTypeError);
86    });
87
88  // validateObject options tests:
89  validateObject(null, 'foo', { nullable: true });
90  validateObject([], 'foo', { allowArray: true });
91  validateObject(() => {}, 'foo', { allowFunction: true });
92}
93
94{
95  // validateString type validation.
96  [
97    -1, {}, [], false, true,
98    1, Infinity, -Infinity, NaN,
99    undefined, null, 1.1,
100  ].forEach((i) => assert.throws(() => validateString(i, 'name'), {
101    code: 'ERR_INVALID_ARG_TYPE'
102  }));
103}
104{
105  // validateNumber type validation.
106  [
107    'a', {}, [], false, true,
108    undefined, null, '', ' ', '0x',
109    '-0x1', '-0o1', '-0b1', '0o', '0b',
110  ].forEach((i) => assert.throws(() => validateNumber(i, 'name'), {
111    code: 'ERR_INVALID_ARG_TYPE'
112  }));
113}
114