{"record":{"id":"a149041af06be003","repo":"Automattic/mongoose","slug":"invalid-value-for-alias-option-on-prop-got-a-a14904","errorCode":null,"errorMessage":"Invalid value for alias option on ${prop}, got ${alias}","messagePattern":"Invalid value for alias option on (.+?), got (.+?)","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/schema.js","lineNumber":236,"sourceCode":"            return function() {\n              if (typeof this.get === 'function') {\n                return this.get(p);\n              }\n              return this[p];\n            };\n          })(prop)).\n          set((function(p) {\n            return function(v) {\n              return this.$set(p, v);\n            };\n          })(prop));\n      }\n\n      continue;\n    }\n\n    if (typeof alias !== 'string') {\n      throw new MongooseError('Invalid value for alias option on ' + prop + ', got ' + alias);\n    }\n\n    schema.aliases[alias] = prop;\n\n    schema.\n      virtual(alias).\n      get((function(p) {\n        return function() {\n          if (typeof this.get === 'function') {\n            return this.get(p);\n          }\n          return this[p];\n        };\n      })(prop)).\n      set((function(p) {\n        return function(v) {\n          return this.$set(p, v);\n        };","sourceCodeStart":218,"sourceCodeEnd":254,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/schema.js#L218-L254","documentation":"When a path's `alias` option is not an array, Mongoose requires it to be a single string; any other truthy type (number, boolean, object) throws at schema construction with the path and the bad value in the message. Aliases are implemented as virtuals, and virtual names must be strings, so Mongoose rejects the schema immediately.","triggerScenarios":"`new Schema({ name: { type: String, alias: 5 } })`; `alias: true`; `alias: { displayName: 'name' }` (object used as a reverse map); alias values sourced from unvalidated env/config.","commonSituations":"Config-driven schemas where the alias field comes from YAML/JSON and arrives as a number or boolean; misunderstanding alias direction (it maps alias → real path, not the reverse).","solutions":["Pass a string: `alias: 'title'` (or an array of strings for multiple aliases).","Validate config values before building the schema: `if (typeof aliasValue === 'string') opts.alias = aliasValue;`.","Remember alias points from the virtual name to the real path: put the new name in alias, the stored name in the schema key."],"exampleFix":"// before\nnew Schema({ name: { type: String, alias: { displayName: 'name' } } }); // throws\n\n// after\nnew Schema({ name: { type: String, alias: 'displayName' } }); // doc.displayName reads/writes doc.name","handlingStrategy":"type-guard","validationCode":"function assertAlias(alias) {\n  if (alias == null) return;\n  const ok = typeof alias === 'string' || (Array.isArray(alias) && alias.every(a => typeof a === 'string'));\n  if (!ok) throw new TypeError(`alias must be a string or string[], got ${typeof alias}`);\n}","typeGuard":"const isValidAlias = (a) => a == null || typeof a === 'string' || (Array.isArray(a) && a.every(x => typeof x === 'string'));","tryCatchPattern":"try { return new Schema(def); } catch (err) { if (err instanceof mongoose.Error && /Invalid value for alias option/.test(err.message)) { /* coerce or drop the bad alias and rebuild */ } throw err; }","preventionTips":["alias maps from the virtual name to the real path — keep it a string.","Type-check alias values coming from YAML/JSON config.","Add a startup schema smoke test."],"tags":["mongoose","schema","alias","invalid-argument"],"backgroundTag":"mongoose-schema-alias-invalid","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}