diff --git a/doc/api/fs.md b/doc/api/fs.md index 09b0d97c3de841..722df8d660ac1b 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -802,6 +802,10 @@ rejection only when `recursive` is false. ### `fsPromises.mkdtemp(prefix[, options])` * `prefix` {string} @@ -2574,6 +2578,9 @@ See the POSIX mkdir(2) documentation for more details. * `prefix` {string} diff --git a/lib/fs.js b/lib/fs.js index 46209a3f4d58c0..cf3c885b31cdf8 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -74,7 +74,6 @@ const { codes: { ERR_FS_FILE_TOO_LARGE, ERR_INVALID_ARG_VALUE, - ERR_INVALID_ARG_TYPE, ERR_FEATURE_UNAVAILABLE_ON_PLATFORM, }, AbortError, @@ -136,6 +135,7 @@ const { validateEncoding, validateFunction, validateInteger, + validateString, } = require('internal/validators'); const watchers = require('internal/fs/watchers'); @@ -2712,9 +2712,8 @@ realpath.native = (path, options, callback) => { function mkdtemp(prefix, options, callback) { callback = makeCallback(typeof options === 'function' ? options : callback); options = getOptions(options, {}); - if (!prefix || typeof prefix !== 'string') { - throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix); - } + + validateString(prefix, 'prefix'); nullCheck(prefix, 'prefix'); warnOnNonPortableTemplate(prefix); const req = new FSReqCallback(); @@ -2730,9 +2729,8 @@ function mkdtemp(prefix, options, callback) { */ function mkdtempSync(prefix, options) { options = getOptions(options, {}); - if (!prefix || typeof prefix !== 'string') { - throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix); - } + + validateString(prefix, 'prefix'); nullCheck(prefix, 'prefix'); warnOnNonPortableTemplate(prefix); const path = `${prefix}XXXXXX`; diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index b30af8fdf5514a..ddb533b218531e 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -29,7 +29,6 @@ const { Buffer } = require('buffer'); const { codes: { ERR_FS_FILE_TOO_LARGE, - ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, ERR_METHOD_NOT_IMPLEMENTED, }, @@ -74,6 +73,7 @@ const { validateBuffer, validateEncoding, validateInteger, + validateString, } = require('internal/validators'); const pathModule = require('path'); const { promisify } = require('internal/util'); @@ -708,9 +708,8 @@ async function realpath(path, options) { async function mkdtemp(prefix, options) { options = getOptions(options, {}); - if (!prefix || typeof prefix !== 'string') { - throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix); - } + + validateString(prefix, 'prefix'); nullCheck(prefix); warnOnNonPortableTemplate(prefix); return binding.mkdtemp(`${prefix}XXXXXX`, options.encoding, kUsePromises); diff --git a/test/parallel/test-fs-mkdtemp-prefix-check.js b/test/parallel/test-fs-mkdtemp-prefix-check.js index 1d9d88232a067e..33a06914a46e10 100644 --- a/test/parallel/test-fs-mkdtemp-prefix-check.js +++ b/test/parallel/test-fs-mkdtemp-prefix-check.js @@ -3,7 +3,7 @@ const common = require('../common'); const assert = require('assert'); const fs = require('fs'); -const prefixValues = [undefined, null, 0, true, false, 1, '']; +const prefixValues = [undefined, null, 0, true, false, 1]; function fail(value) { assert.throws(