Skip to content
Closed
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
Next Next commit
lib: extract validateObject validator
  • Loading branch information
ZYSzys committed Dec 24, 2018
commit a45d663ac1713530f395ef2b5d3f8065f3f74f0f
10 changes: 7 additions & 3 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ const {
ERR_INVALID_OPT_VALUE,
ERR_OUT_OF_RANGE
} = require('internal/errors').codes;
const { validateString, isInt32 } = require('internal/validators');
const {
validateString,
isInt32,
validateObject
} = require('internal/validators');
const child_process = require('internal/child_process');
const {
_validateStdio,
Expand Down Expand Up @@ -417,8 +421,8 @@ function normalizeSpawnArguments(file, args, options) {

if (options === undefined)
options = {};
else if (options === null || typeof options !== 'object')
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
else
validateObject(options, 'options');

// Validate the cwd, if present.
if (options.cwd != null &&
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/crypto/keygen.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const {
PrivateKeyObject
} = require('internal/crypto/keys');
const { customPromisifyArgs } = require('internal/util');
const { isUint32, validateString } = require('internal/validators');
const { isUint32, validateObject, validateString } = require('internal/validators');

const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
Expand Down Expand Up @@ -115,8 +116,7 @@ function parseKeyEncoding(keyType, options) {

function check(type, options, callback) {
validateString(type, 'type');
if (options == null || typeof options !== 'object')
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
validateObject(options, 'options');

// These will be set after parsing the type and type-specific options to make
// the order a bit more intuitive.
Expand Down
9 changes: 8 additions & 1 deletion lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ function validateNumber(value, name) {
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
}

function validateObject(value, name) {
if (typeof value !== 'object' || value === null) {
throw new ERR_INVALID_ARG_TYPE(name, 'object', value);
}
}

module.exports = {
isInt32,
isUint32,
Expand All @@ -138,5 +144,6 @@ module.exports = {
validateInt32,
validateUint32,
validateString,
validateNumber
validateNumber,
validateObject
};
4 changes: 2 additions & 2 deletions lib/trace_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
ERR_TRACE_EVENTS_UNAVAILABLE,
ERR_INVALID_ARG_TYPE
} = require('internal/errors').codes;
const { validateObject } = require('internal/validators');

const { isMainThread } = require('internal/worker');
if (!hasTracing || !isMainThread)
Expand Down Expand Up @@ -70,8 +71,7 @@ class Tracing {
}

function createTracing(options) {
if (typeof options !== 'object' || options == null)
Comment thread
BridgeAR marked this conversation as resolved.
Outdated
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
validateObject(options, 'options');

if (!Array.isArray(options.categories)) {
throw new ERR_INVALID_ARG_TYPE('options.categories', 'string[]',
Expand Down