Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add some tests
  • Loading branch information
mawaregetsuka committed Mar 23, 2022
commit 82888c0bedf054efda4d94ed9dac6e2a5da18a40
10 changes: 2 additions & 8 deletions lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,10 @@ const validateInteger = hideStackFrames(
const validateInt32 = hideStackFrames(
(value, name, min = -2147483648, max = 2147483647) => {
// The defaults for min and max correspond to the limits of 32-bit integers.
if (typeof value == 'symbol' || typeof value == 'bigint') {
if (typeof value !== 'number') {
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
}
if (!isInt32(value)) {
if (typeof value !== 'number') {
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
}
if (!NumberIsInteger(value)) {
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
}
Expand All @@ -101,13 +98,10 @@ const validateInt32 = hideStackFrames(
);

const validateUint32 = hideStackFrames((value, name, positive) => {
if (typeof value == 'symbol' || typeof value == 'bigint') {
if (typeof value !== 'number') {
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
}
if (!isUint32(value)) {
if (typeof value !== 'number') {
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
}
if (!NumberIsInteger(value)) {
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
}
Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const {
validateNumber,
validateObject,
validateString,
validateInt32,
validateUint32,
} = require('internal/validators');
const { MAX_SAFE_INTEGER, MIN_SAFE_INTEGER } = Number;
const outOfRangeError = {
Expand Down Expand Up @@ -41,6 +43,28 @@ const invalidArgValueError = {
// 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);

// validateInt32() and validateUint32()
[
Symbol(), 1n, {}, [], false, true, undefined, null,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Symbol(), 1n, {}, [], false, true, undefined, null,
Symbol(), 1n, {}, [], false, true, undefined, null, () => {}, '', '1',

].forEach((val) => assert.throws(() => validateInt32(val, 'name'), {
code: 'ERR_INVALID_ARG_TYPE'
}));
[
2147483647 + 1, -2147483648 - 1, NaN,
].forEach((val) => assert.throws(() => validateInt32(val, 'name'), {
code: 'ERR_OUT_OF_RANGE'
}));
[
Symbol(), 1n, {}, [], false, true, undefined, null,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Symbol(), 1n, {}, [], false, true, undefined, null,
Symbol(), 1n, {}, [], false, true, undefined, null, () => {}, '', '1',

].forEach((val) => assert.throws(() => validateUint32(val, 'name'), {
code: 'ERR_INVALID_ARG_TYPE'
}));
[
4294967296, -1, NaN,
].forEach((val) => assert.throws(() => validateUint32(val, 'name'), {
code: 'ERR_OUT_OF_RANGE'
}));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}));
}));
// Ensure it doesn't throw on acceptable values:
validateInt32(0, 'name');
validateUint32(0, 'name');
validateInt32(1, 'name');
validateUint32(1, 'name');
validateInt32(-1, 'name');

}

{
Expand Down