{"record":{"id":"95140b27b35634c8","repo":"Automattic/mongoose","slug":"cannot-set-both-validateallpaths-and-validatemo","errorCode":null,"errorMessage":"Cannot set both `validateAllPaths` and `validateModifiedOnly`","messagePattern":"Cannot set both `validateAllPaths` and `validateModifiedOnly`","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/document.js","lineNumber":2811,"sourceCode":"\n    let shouldValidateModifiedOnly;\n    if (hasValidateModifiedOnlyOption) {\n      shouldValidateModifiedOnly = !!options.validateModifiedOnly;\n    } else {\n      shouldValidateModifiedOnly = this.$__schema.options.validateModifiedOnly;\n    }\n    this.$__.validateModifiedOnly = shouldValidateModifiedOnly;\n\n    const validateAllPaths = options?.validateAllPaths;\n    if (validateAllPaths) {\n      if (pathsToSkip) {\n        throw new TypeError('Cannot set both `validateAllPaths` and `pathsToSkip`');\n      }\n      if (pathsToValidate) {\n        throw new TypeError('Cannot set both `validateAllPaths` and `pathsToValidate`');\n      }\n      if (hasValidateModifiedOnlyOption && shouldValidateModifiedOnly) {\n        throw new TypeError('Cannot set both `validateAllPaths` and `validateModifiedOnly`');\n      }\n    }\n\n    const _this = this;\n\n    // only validate required fields when necessary\n    let paths;\n    let doValidateOptionsByPath;\n    if (validateAllPaths) {\n      paths = new Set(Object.keys(this.$__schema.paths));\n      // gh-661: if a whole array is modified, make sure to run validation on all\n      // the children as well\n      for (const path of paths) {\n        const schemaType = this.$__schema.path(path);\n        if (!schemaType?.$isMongooseArray) {\n          continue;\n        }\n        const val = this.$__getValue(path);","sourceCodeStart":2793,"sourceCodeEnd":2829,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/document.js#L2793-L2829","documentation":"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.","triggerScenarios":"`doc.validate({ validateAllPaths: true, validateModifiedOnly: true })` where validateModifiedOnly was passed explicitly in the same options object.","commonSituations":"Schema-level validateModifiedOnly combined with a per-call validateAllPaths without clearing the former; merged options objects carrying both flags.","solutions":["Remove `validateModifiedOnly` from the call when using validateAllPaths","Or drop validateAllPaths to keep validating only modified paths","When spreading defaults, explicitly delete the conflicting key"],"exampleFix":"// before\nawait doc.validate({ validateAllPaths: true, validateModifiedOnly: true });\n\n// after\nawait doc.validate({ validateAllPaths: true });\n// or: await doc.validate({ validateModifiedOnly: true });","handlingStrategy":"validation","validationCode":"if (opts?.validateAllPaths) {\n  delete opts.validateModifiedOnly; // or reject loudly:\n  // throw new TypeError('validateAllPaths excludes validateModifiedOnly');\n}\nawait doc.validate(opts);","typeGuard":null,"tryCatchPattern":"try {\n  await doc.validate(opts);\n} catch (err) {\n  if (err instanceof TypeError && err.message.includes('validateModifiedOnly')) {\n    // drop validateModifiedOnly when validating all paths\n  } else { throw err; }\n}","preventionTips":["validateAllPaths means every path, modified or not - never combine it with validateModifiedOnly","Keep schema-level validateModifiedOnly in mind when adding per-call validateAllPaths"],"tags":["mongoose","validate","options","typeerror"],"backgroundTag":"conflicting-validation-options","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}