Skip to content

Commit

Permalink
lib: improve value validation utils
Browse files Browse the repository at this point in the history
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
lundibundi authored and MylesBorins committed Mar 10, 2020
1 parent eaf6723 commit 607ac90
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 15 deletions.
28 changes: 28 additions & 0 deletions lib/internal/validators.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {
ArrayIsArray,
NumberIsInteger,
NumberMAX_SAFE_INTEGER,
NumberMIN_SAFE_INTEGER,
Expand Down Expand Up @@ -123,6 +124,30 @@ function validateNumber(value, name) {
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
}

function validateBoolean(value, name) {
if (typeof value !== 'boolean')
throw new ERR_INVALID_ARG_TYPE(name, 'boolean', value);
}

const validateObject = hideStackFrames(
(value, name, { nullable = false } = {}) => {
if ((!nullable && value === null) ||
ArrayIsArray(value) ||
typeof value !== 'object') {
throw new ERR_INVALID_ARG_TYPE(name, 'Object', value);
}
});

const validateArray = hideStackFrames((value, name, { minLength = 0 } = {}) => {
if (!ArrayIsArray(value)) {
throw new ERR_INVALID_ARG_TYPE(name, 'Array', value);
}
if (value.length < minLength) {
const reason = `must be longer than ${minLength}`;
throw new ERR_INVALID_ARG_VALUE(name, value, reason);
}
});

function validateSignalName(signal, name = 'signal') {
if (typeof signal !== 'string')
throw new ERR_INVALID_ARG_TYPE(name, 'string', signal);
Expand Down Expand Up @@ -159,8 +184,11 @@ module.exports = {
isInt32,
isUint32,
parseMode,
validateArray,
validateBoolean,
validateBuffer,
validateEncoding,
validateObject,
validateInteger,
validateInt32,
validateUint32,
Expand Down
91 changes: 76 additions & 15 deletions test/parallel/test-validators.js
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 });
}

0 comments on commit 607ac90

Please sign in to comment.