{"record":{"id":"23a215d0b2dedc88","repo":"Automattic/mongoose","slug":"enum-can-only-be-set-on-an-array-of-strings-or-n","errorCode":null,"errorMessage":"`enum` can only be set on an array of strings or numbers , not ${instance}","messagePattern":"`enum` can only be set on an array of strings or numbers , not (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/schema/array.js","lineNumber":263,"sourceCode":"\n/**\n * Adds an enum validator if this is an array of strings or numbers. Equivalent to\n * `SchemaString.prototype.enum()` or `SchemaNumber.prototype.enum()`\n *\n * @param {...string|object} [args] enumeration values\n * @return {SchemaArray} this\n */\n\nSchemaArray.prototype.enum = function() {\n  let arr = this;\n  while (true) {\n    const instance = arr?.embeddedSchemaType?.instance;\n    if (instance === 'Array') {\n      arr = arr.embeddedSchemaType;\n      continue;\n    }\n    if (instance !== 'String' && instance !== 'Number') {\n      throw new Error('`enum` can only be set on an array of strings or numbers ' +\n        ', not ' + instance);\n    }\n    break;\n  }\n\n  let enumArray = arguments;\n  if (!Array.isArray(arguments) && utils.isObject(arguments)) {\n    enumArray = utils.object.vals(enumArray);\n  }\n\n  arr.embeddedSchemaType.enum.apply(arr.embeddedSchemaType, enumArray);\n  return this;\n};\n\n/**\n * Overrides the getters application for the population special-case\n *\n * @param {object} value","sourceCodeStart":245,"sourceCodeEnd":281,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/schema/array.js#L245-L281","documentation":"SchemaArray#enum walks through nested arrays to the innermost embedded schema type and requires its instance to be String or Number - enum is a value whitelist, which only makes sense for primitives. Any other element type (Mixed, ObjectId, Date, Boolean, subdocuments) is rejected when enum is set.","triggerScenarios":"`{ tags: { type: [], enum: ['a','b'] } }` (a bare [] means an array of Mixed); `{ ids: { type: [Schema.Types.ObjectId], enum: [...] } }`; an explicit `schema.path('when').enum(...)` on an array of Dates; enum on an array of subdocuments.","commonSituations":"Declaring arrays as bare [] and assuming string elements; intending allowed-values checks for ObjectId arrays; copying an enum option from a scalar field onto a non-string array.","solutions":["Give the array an explicit primitive element type: `{ type: [String], enum: ['a','b'] }` or `{ type: [Number], enum: [1,2,3] }`.","For ObjectId/Date arrays, replace enum with a custom validator on the element type.","Remember enum validates every element, not the array as a whole - use a custom array-level validator for whole-list rules."],"exampleFix":"// before\nconst s = new Schema({ tags: { type: [], enum: ['red', 'blue'] } }); // [] -> Mixed elements\n\n// after\nconst s = new Schema({ tags: { type: [String], enum: ['red', 'blue'] } });","handlingStrategy":"validation","validationCode":"const enumCompatible = (schema, pathName) => {\n  let t = schema.path(pathName);\n  while (t?.$isMongooseArray) t = t.embeddedSchemaType;\n  return t?.instance === 'String' || t?.instance === 'Number';\n};\nif (!enumCompatible(schema, 'tags')) throw new Error('enum needs [String] or [Number] elements');","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always declare element types for arrays that carry enum ([String], [Number]) - never bare [].","Use a custom validator when whitelisting non-primitive element types.","Remember enum applies per element, not to the array as a whole."],"tags":["enum","arrays","validators","schema-definition"],"backgroundTag":"enum-type-mismatch","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}