-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add common validators: `validateArray`, `validateBoolean`, `validateObject` and appropriate tests. PR-URL: #31480 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gus Caplan <[email protected]>
- Loading branch information
1 parent
eaf6723
commit 607ac90
Showing
2 changed files
with
104 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,87 @@ | ||
// Flags: --expose-internals | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const { | ||
validateInteger | ||
validateArray, | ||
validateBoolean, | ||
validateInteger, | ||
validateObject, | ||
} = require('internal/validators'); | ||
const { MAX_SAFE_INTEGER, MIN_SAFE_INTEGER } = Number; | ||
const outOfRangeError = { | ||
code: 'ERR_OUT_OF_RANGE', | ||
name: 'RangeError' | ||
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); | ||
}); | ||
|
||
// 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); | ||
validateObject(null, 'foo', { nullable: true }); | ||
} |