Automattic/mongoose · error · TypeError

Cannot set both `validateAllPaths` and `validateModifiedOnly

Error message

Cannot set both `validateAllPaths` and `validateModifiedOnly`

What it means

TypeError from Document#validate(): options combined `validateAllPaths: true` with `validateModifiedOnly: true` (explicitly in the call, not inherited from schema defaults). Validating all paths already includes unmodified ones, so the combination is contradictory and rejected.

Source

Thrown at lib/document.js:2811

    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) {
          continue;
        }
        const val = this.$__getValue(path);

View on GitHub (pinned to 49cdab0136)

Solutions

  1. Remove `validateModifiedOnly` from the call when using validateAllPaths
  2. Or drop validateAllPaths to keep validating only modified paths
  3. When spreading defaults, explicitly delete the conflicting key

Example fix

// before
await doc.validate({ validateAllPaths: true, validateModifiedOnly: true });

// after
await doc.validate({ validateAllPaths: true });
// or: await doc.validate({ validateModifiedOnly: true });
Defensive patterns

Strategy: validation

Validate before calling

if (opts?.validateAllPaths) {
  delete opts.validateModifiedOnly; // or reject loudly:
  // throw new TypeError('validateAllPaths excludes validateModifiedOnly');
}
await doc.validate(opts);

Try / catch

try {
  await doc.validate(opts);
} catch (err) {
  if (err instanceof TypeError && err.message.includes('validateModifiedOnly')) {
    // drop validateModifiedOnly when validating all paths
  } else { throw err; }
}

Prevention

When it happens

Trigger: `doc.validate({ validateAllPaths: true, validateModifiedOnly: true })` where validateModifiedOnly was passed explicitly in the same options object.

Common situations: Schema-level validateModifiedOnly combined with a per-call validateAllPaths without clearing the former; merged options objects carrying both flags.

Related errors


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