Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a0cfb0c

Browse filesBrowse files
cjihrigcodebytere
authored andcommitted
lib: add validateInteger() validator
This allows validation of integers that are not int32 or uint32. PR-URL: #20851 Fixes: #20844 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Backport-PR-URL: #21171 Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
1 parent 6a098ad commit a0cfb0c
Copy full SHA for a0cfb0c

File tree

Expand file treeCollapse file tree

1 file changed

+16
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+16
-1
lines changed
Open diff view settings
Collapse file

‎lib/internal/validators.js‎

Copy file name to clipboardExpand all lines: lib/internal/validators.js
+16-1Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,21 @@ function isUint32(value) {
1313
return value === (value >>> 0);
1414
}
1515

16-
function validateInt32(value, name) {
16+
function validateInteger(value, name) {
17+
let err;
18+
19+
if (typeof value !== 'number')
20+
err = new ERR_INVALID_ARG_TYPE(name, 'number', value);
21+
else if (!Number.isSafeInteger(value))
22+
err = new ERR_OUT_OF_RANGE(name, 'an integer', value);
23+
24+
if (err) {
25+
Error.captureStackTrace(err, validateInteger);
26+
throw err;
27+
}
28+
}
29+
30+
function validateInt32(value, name, min = -2147483648, max = 2147483647) {
1731
if (!isInt32(value)) {
1832
let err;
1933
if (typeof value !== 'number') {
@@ -53,6 +67,7 @@ function validateUint32(value, name, positive) {
5367
module.exports = {
5468
isInt32,
5569
isUint32,
70+
validateInteger,
5671
validateInt32,
5772
validateUint32
5873
};

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.