Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: support min/max values in validateInteger() #28810

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
isArrayBufferView
} = require('internal/util/types');
const { signals } = internalBinding('constants').os;
const { MAX_SAFE_INTEGER, MIN_SAFE_INTEGER } = Number;

function isInt32(value) {
return value === (value | 0);
Expand Down Expand Up @@ -60,12 +61,16 @@ function parseMode(value, name, def) {
throw new ERR_INVALID_ARG_VALUE(name, value, modeDesc);
}

const validateInteger = hideStackFrames((value, name) => {
if (typeof value !== 'number')
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
if (!Number.isSafeInteger(value))
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
});
const validateInteger = hideStackFrames(
(value, name, min = MIN_SAFE_INTEGER, max = MAX_SAFE_INTEGER) => {
if (typeof value !== 'number')
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
if (!Number.isInteger(value))
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
if (value < min || value > max)
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
}
);

const validateInt32 = hideStackFrames(
(value, name, min = -2147483648, max = 2147483647) => {
Expand Down