Automattic/mongoose · error · TypeError

Cannot set both `validateAllPaths` and `pathsToValidate`

Error message

Cannot set both `validateAllPaths` and `pathsToValidate`

What it means

TypeError from Document#validate(): the call passed both an explicit pathsToValidate (first argument, string or array) and `validateAllPaths: true` in options. Selecting specific paths and selecting all paths are mutually exclusive, so validate() refuses the combination.

Source

Thrown at lib/document.js:2808

        ('validateModifiedOnly' in options);

    const pathsToSkip = options?.pathsToSkip || null;

    let shouldValidateModifiedOnly;
    if (hasValidateModifiedOnlyOption) {
      shouldValidateModifiedOnly = !!options.validateModifiedOnly;
    } else {
      shouldValidateModifiedOnly = this.$__schema.options.validateModifiedOnly;
    }
    this.$__.validateModifiedOnly = shouldValidateModifiedOnly;

    const validateAllPaths = options?.validateAllPaths;
    if (validateAllPaths) {
      if (pathsToSkip) {
        throw new TypeError('Cannot set both `validateAllPaths` and `pathsToSkip`');
      }
      if (pathsToValidate) {
        throw new TypeError('Cannot set both `validateAllPaths` and `pathsToValidate`');
      }
      if (hasValidateModifiedOnlyOption && shouldValidateModifiedOnly) {
        throw new TypeError('Cannot set both `validateAllPaths` and `validateModifiedOnly`');
      }
    }

    const _this = this;

    // only validate required fields when necessary
    let paths;
    let doValidateOptionsByPath;
    if (validateAllPaths) {
      paths = new Set(Object.keys(this.$__schema.paths));
      // gh-661: if a whole array is modified, make sure to run validation on all
      // the children as well
      for (const path of paths) {
        const schemaType = this.$__schema.path(path);
        if (!schemaType?.$isMongooseArray) {

View on GitHub (pinned to 49cdab0136)

Solutions

  1. Drop the first argument when using validateAllPaths
  2. Or drop validateAllPaths and keep the explicit path list
  3. Make wrapper functions normalize arguments so both are never set together

Example fix

// before
await doc.validate(['name', 'email'], { validateAllPaths: true });

// after
await doc.validate({ validateAllPaths: true });
// or: await doc.validate(['name', 'email']);
Defensive patterns

Strategy: validation

Validate before calling

function normalizeValidateArgs(paths, opts = {}) {
  if (opts.validateAllPaths && paths != null) {
    if (typeof paths !== 'object' || Array.isArray(paths)) return [null, opts]; // drop paths
  }
  return [paths, opts];
}
const [p, o] = normalizeValidateArgs(paths, opts);
await doc.validate(p, o);

Try / catch

try {
  await doc.validate(paths, opts);
} catch (err) {
  if (err instanceof TypeError && err.message.includes('validateAllPaths')) {
    // paths argument conflicts with validateAllPaths; call with options only
  } else { throw err; }
}

Prevention

When it happens

Trigger: `doc.validate(['name'], { validateAllPaths: true })` or `doc.validate('name', { validateAllPaths: true })`.

Common situations: A wrapper that always forwards a paths argument while the caller adds validateAllPaths; refactors from path-list validation to full validation that forget to clear the first argument.

Related errors


AI-assisted analysis of Automattic/mongoose@49cdab0136 (2026-08-21). Data as JSON: /api/errors/dda73fc3863f65b7. Report an issue: GitHub.