Validation functions from node.js source code /lib/internal/validators.js

/**

/**

/**

/** @type {validateInteger} */ const validateInteger = hideStackFrames( (value, name, min = NumberMIN_SAFE_INTEGER, max = NumberMAX_SAFE_INTEGER) => { 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); if (value < min || value > max) throw new ERR_OUT_OF_RANGE(name, >= ${min} && <= ${max}, value); }, );

/**

/** @type {validateInt32} */ 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 !== ‘number’) { throw new ERR_INVALID_ARG_TYPE(name, ‘number’, value); } if (!NumberIsInteger(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); } }, );

/**

/** @type {validateUint32} */ const validateUint32 = hideStackFrames((value, name, positive = false) => { 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); } const min = positive ? 1 : 0; // 2 ** 32 === 4294967296 const max = 4_294_967_295; if (value < min || value > max) { throw new ERR_OUT_OF_RANGE(name, >= ${min} && <= ${max}, value); } });

/**

/** @type {validateString} */ const validateString = hideStackFrames((value, name) => { if (typeof value !== ‘string’) throw new ERR_INVALID_ARG_TYPE(name, ‘string’, value); });

/**

/** @type {validateNumber} */ const validateNumber = hideStackFrames((value, name, min = undefined, max) => { if (typeof value !== ‘number’) throw new ERR_INVALID_ARG_TYPE(name, ‘number’, value);

if ((min != null && value < min) || (max != null && value > max) || ((min != null || max != null) && NumberIsNaN(value))) { throw new ERR_OUT_OF_RANGE( name, ${min != null ? >= ${min}: ''}${min != null && max != null ? ' && ' : ''}${max != null ?<= ${max} : ''}, value); } });

/**

/** @type {validateBoolean} */ const validateBoolean = hideStackFrames((value, name) => { if (typeof value !== ‘boolean’) throw new ERR_INVALID_ARG_TYPE(name, ‘boolean’, value); });

const kValidateObjectNone = 0; const kValidateObjectAllowNullable = 1 << 0; const kValidateObjectAllowArray = 1 << 1; const kValidateObjectAllowFunction = 1 << 2; const kValidateObjectAllowObjects = kValidateObjectAllowArray | kValidateObjectAllowFunction; const kValidateObjectAllowObjectsAndNull = kValidateObjectAllowNullable | kValidateObjectAllowArray | kValidateObjectAllowFunction;

/**

/** @type {validateObject} */ const validateObject = hideStackFrames( (value, name, options = kValidateObjectNone) => { if (options === kValidateObjectNone) { if (value === null || ArrayIsArray(value)) { throw new ERR_INVALID_ARG_TYPE(name, ‘Object’, value); }

  if (typeof value !== 'object') {
    throw new ERR_INVALID_ARG_TYPE(name, 'Object', value);
  }
} else {
  const throwOnNullable = (kValidateObjectAllowNullable & options) === 0;

  if (throwOnNullable && value === null) {
    throw new ERR_INVALID_ARG_TYPE(name, 'Object', value);
  }

  const throwOnArray = (kValidateObjectAllowArray & options) === 0;

  if (throwOnArray && ArrayIsArray(value)) {
    throw new ERR_INVALID_ARG_TYPE(name, 'Object', value);
  }

  const throwOnFunction = (kValidateObjectAllowFunction & options) === 0;
  const typeofValue = typeof value;

  if (typeofValue !== 'object' && (throwOnFunction || typeofValue !== 'function')) {
    throw new ERR_INVALID_ARG_TYPE(name, 'Object', value);
  }
}

});

/**

/** @type {validateArray} */ 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); } });

/**

/** @type {validateStringArray} */ const validateStringArray = hideStackFrames((value, name) => { validateArray(value, name); for (let i = 0; i < value.length; ++i) { // Don’t use validateString here for performance reasons, as // we would generate intermediate strings for the name. if (typeof value[i] !== ‘string’) { throw new ERR_INVALID_ARG_TYPE(${name}[${i}], ‘string’, value[i]); } } });

/**

/** @type {validateBooleanArray} */ const validateBooleanArray = hideStackFrames((value, name) => { validateArray(value, name); for (let i = 0; i < value.length; ++i) { // Don’t use validateBoolean here for performance reasons, as // we would generate intermediate strings for the name. if (value[i] !== true && value[i] !== false) { throw new ERR_INVALID_ARG_TYPE(${name}[${i}], ‘boolean’, value[i]); } } });

/**

/** @type {validateFunction} */ const validateFunction = hideStackFrames((value, name) => { if (typeof value !== ‘function’) throw new ERR_INVALID_ARG_TYPE(name, ‘Function’, value); });

/**

/** @type {validatePlainFunction} */ const validatePlainFunction = hideStackFrames((value, name) => { if (typeof value !== ‘function’ || isAsyncFunction(value)) throw new ERR_INVALID_ARG_TYPE(name, ‘Function’, value); });

/**

/** @type {validateUndefined} */ const validateUndefined = hideStackFrames((value, name) => { if (value !== undefined) throw new ERR_INVALID_ARG_TYPE(name, ‘undefined’, value); });