{"record":{"id":"5e0ec00e6c189402","repo":"Automattic/mongoose","slug":"invalid-value-for-alias-option-on-prop-got-a","errorCode":null,"errorMessage":"Invalid value for alias option on ${prop}, got ${a}","messagePattern":"Invalid value for alias option on (.+?), got (.+?)","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/schema.js","lineNumber":210,"sourceCode":"      alias = paths[path];\n    } else {\n      const options = get(schema.paths[path], 'options');\n      if (options == null) {\n        continue;\n      }\n\n      alias = options.alias;\n    }\n\n    if (!alias) {\n      continue;\n    }\n\n    const prop = schema.paths[path].path;\n    if (Array.isArray(alias)) {\n      for (const a of alias) {\n        if (typeof a !== 'string') {\n          throw new MongooseError('Invalid value for alias option on ' + prop + ', got ' + a);\n        }\n\n        schema.aliases[a] = prop;\n\n        schema.\n          virtual(a).\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":192,"sourceCodeEnd":228,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/schema.js#L192-L228","documentation":"A schema path declared `alias: [...]` must contain only strings; when Mongoose builds aliases at schema-construction time it iterates the array and throws for any non-string entry, naming the path and the offending value. The alias mechanism creates a virtual per alias that reads/writes the real path, which only works for string virtual names.","triggerScenarios":"`new Schema({ name: { type: String, alias: ['n', 5] } })`; alias arrays containing null, booleans, or objects; building alias arrays from config or user input without type checks.","commonSituations":"Generating schemas from config files or JSON where a numeric alias slips in; typos like `alias: ['n', null]`; refactors that leave placeholder values in alias arrays.","solutions":["Use only string aliases: `alias: ['n', 'title']`.","Filter/validate config-driven aliases before schema creation: `aliases.filter(a => typeof a === 'string')`.","Fail fast in tests by instantiating every generated schema once during startup."],"exampleFix":"// before\nnew Schema({ name: { type: String, alias: ['n', 5] } }); // throws: Invalid value for alias option on name, got 5\n\n// after\nnew Schema({ name: { type: String, alias: ['n', 'title'] } });","handlingStrategy":"type-guard","validationCode":"function sanitizeAliases(aliases) {\n  if (aliases == null) return undefined;\n  const list = Array.isArray(aliases) ? aliases : [aliases];\n  const clean = list.filter(a => typeof a === 'string');\n  if (clean.length !== list.length) throw new TypeError('All aliases must be strings');\n  return Array.isArray(aliases) ? clean : clean[0];\n}","typeGuard":"const aliasesAreValid = (list) => Array.isArray(list) && list.every(a => typeof a === 'string');","tryCatchPattern":"try { return new Schema(def); } catch (err) { if (err instanceof mongoose.Error && /Invalid value for alias option/.test(err.message)) { /* fix the alias config entry and rebuild */ } throw err; }","preventionTips":["Validate config-driven alias values before schema creation.","Use only strings in alias arrays.","Instantiate generated schemas once in tests to fail fast."],"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"}