{"record":{"id":"266be6e07b52c0b7","repo":"Automattic/mongoose","slug":"path-this-path-may-not-have-allownull-speci","errorCode":null,"errorMessage":"Path \"${this.path}\" may not have `allowNull` specified when `required` is true","messagePattern":"Path \"(.+?)\" may not have `allowNull` specified when `required` is true","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/schemaType.js","lineNumber":1175,"sourceCode":"  }\n\n  if (required === false) {\n    this.validators = this.validators.filter(function(v) {\n      return v.validator !== this.requiredValidator;\n    }, this);\n\n    this.isRequired = false;\n    delete this.originalRequiredValue;\n    return this;\n  }\n\n  const _this = this;\n  this.isRequired = true;\n  this.originalRequiredValue = required;\n\n  if (typeof this.originalRequiredValue !== 'function' &&\n      (utils.hasUserDefinedProperty(this.options, 'allowNull') || this.allowNullValidator != null)) {\n    throw new MongooseError('Path \"' + this.path + '\" may not have `allowNull` specified when `required` is true');\n  }\n\n  this.requiredValidator = function(v) {\n    const cachedRequired = this?.$__?.cachedRequired;\n\n    // no validation when this path wasn't selected in the query.\n    if (cachedRequired != null && !this.$__isSelected(_this.path) && !this[documentIsModified](_this.path)) {\n      return true;\n    }\n\n    // `$cachedRequired` gets set in `_getPathsToValidate()` so we\n    // don't call required functions multiple times in one validate call\n    // See gh-6801\n    if (cachedRequired != null && _this.path in cachedRequired) {\n      const res = cachedRequired[_this.path] ?\n        _this.checkRequired(v, this) :\n        true;\n      delete cachedRequired[_this.path];","sourceCodeStart":1157,"sourceCodeEnd":1193,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/schemaType.js#L1157-L1193","documentation":"SchemaType.prototype.required() throws when a path is made required with a non-function value while an allowNull option (or a previously registered allowNull validator) is present. A non-function required value already rejects null, so allowNull alongside it is contradictory and Mongoose refuses the combination.","triggerScenarios":"Defining new Schema({ name: { type: String, required: true, allowNull: false } }); calling path.required(true) on a path where path.allowNull(false) was previously called; recompiling a schema that mixes required: true with a user-defined allowNull key in options.","commonSituations":"Turning an optional field into a required one and leaving allowNull: false behind; copying option blocks between fields during refactoring; generators that always emit allowNull.","solutions":["Remove allowNull from the field definition when required is true (required already rejects null)","If null must sometimes pass, make required a function: required: function() { return this.status === 'active'; } (function required values skip this check)","Drop the required flag and keep allowNull, enforcing presence with a custom validator"],"exampleFix":"// before\nnew Schema({ name: { type: String, required: true, allowNull: false } });\n// after\nnew Schema({ name: { type: String, required: true } });","handlingStrategy":"validation","validationCode":"function assertNoRequiredAllowNullConflict(def) {\n  const req = def.required;\n  const hasAllowNull = Object.prototype.hasOwnProperty.call(def, 'allowNull');\n  if (req === true && hasAllowNull) throw new Error(`Path conflicts: required=true with allowNull`);\n}\nassertNoRequiredAllowNullConflict(fieldDef);","typeGuard":"function hasConflictingRequired(def) {\n  return def.required === true &&\n    (Object.prototype.hasOwnProperty.call(def, 'allowNull') || def.allowNull !== undefined);\n}","tryCatchPattern":"try { new Schema(defs); } catch (err) { if (/may not have `allowNull`/.test(err.message)) failFastWithSchemaHint(err); throw err; }","preventionTips":["Lint schema definitions for required + allowNull combinations","Remember required: true already rejects null and undefined — allowNull is for optional paths","Use function required values when nullability depends on document state"],"tags":["mongoose","schema","required","allownull","schema-options"],"backgroundTag":"conflicting-schema-options","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}