Skip to content

Commit

Permalink
lib: add validateInteger() validator
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
2 people authored and targos committed Jun 13, 2018
1 parent 6a098ad commit a0cfb0c
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,21 @@ function isUint32(value) {
return value === (value >>> 0);
}

function validateInt32(value, name) {
function validateInteger(value, name) {
let err;

if (typeof value !== 'number')
err = new ERR_INVALID_ARG_TYPE(name, 'number', value);
else if (!Number.isSafeInteger(value))
err = new ERR_OUT_OF_RANGE(name, 'an integer', value);

if (err) {
Error.captureStackTrace(err, validateInteger);
throw err;
}
}

function validateInt32(value, name, min = -2147483648, max = 2147483647) {
if (!isInt32(value)) {
let err;
if (typeof value !== 'number') {
Expand Down Expand Up @@ -53,6 +67,7 @@ function validateUint32(value, name, positive) {
module.exports = {
isInt32,
isUint32,
validateInteger,
validateInt32,
validateUint32
};

0 comments on commit a0cfb0c

Please sign in to comment.