Skip to content

Commit

Permalink
lib,test: improve validateNumber and validators coverage
Browse files Browse the repository at this point in the history
Adds a min/max predicate to validateNumber (which will be used
by the QUIC impl, extracted from that PR) ... and improves test
coverage in test-validators.js

Signed-off-by: James M Snell <jasnell@gmail.com>
  • Loading branch information
jasnell committed May 7, 2020
1 parent e454e9b commit 9c44e2e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

const {
ArrayIsArray,
NumberPOSITIVE_INFINITY,
NumberIsInteger,
NumberMAX_SAFE_INTEGER,
NumberMIN_SAFE_INTEGER,
NumberNEGATIVE_INFINITY,
} = primordials;

const {
Expand Down Expand Up @@ -121,9 +123,13 @@ function validateString(value, name) {
throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
}

function validateNumber(value, name) {
function validateNumber(value, name,
min = NumberNEGATIVE_INFINITY,
max = NumberPOSITIVE_INFINITY) {
if (typeof value !== 'number')
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
if (value < min || value > max)
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
}

function validateBoolean(value, name) {
Expand Down
43 changes: 42 additions & 1 deletion test/parallel/test-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@

require('../common');
const assert = require('assert');
const { Buffer } = require('buffer');
const {
validateArray,
validateBoolean,
validateBuffer,
validateInteger,
validateNumber,
validateObject,
validateString,
} = require('internal/validators');
const { MAX_SAFE_INTEGER, MIN_SAFE_INTEGER } = Number;
const {
MAX_SAFE_INTEGER,
MIN_SAFE_INTEGER,
NEGATIVE_INFINITY,
POSITIVE_INFINITY,
} = Number;
const outOfRangeError = {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
Expand Down Expand Up @@ -85,3 +94,35 @@ const invalidArgValueError = {

validateObject(null, 'foo', { nullable: true });
}

{
// validateString tests
validateString('hi', 'foo');

[1, false, [], {}, NaN, 1n, null, undefined].forEach((i) => {
assert.throws(() => validateString(i, 'foo'), invalidArgTypeError);
});
}

{
// validateBuffer tests
validateBuffer(Buffer.from('hi'), 'foo');
validateBuffer(Buffer.alloc(10), 'foo');
validateBuffer(new Uint8Array(10), 'foo');
validateBuffer(new DataView((new Uint8Array(10)).buffer));

[1, false, '', {}, [], 1n, null, undefined].forEach((i) => {
assert.throws(() => validateBuffer(i, 'foo'), invalidArgTypeError);
});
}

{
// validateNumber tests

['', false, [], {}, 1n, null, undefined].forEach((i) => {
assert.throws(() => validateNumber(i, 'foo'), invalidArgTypeError);
});

assert.throws(() => validateNumber(1, 'foo', 2), outOfRangeError);
assert.throws(() => validateNumber(3, 'foo', 0, 2), outOfRangeError);
}

0 comments on commit 9c44e2e

Please sign in to comment.