I'm having trouble properly configuring my enum in my appealSchema. I see the following error:
Type '{ type: StringConstructor; enum: (string | Category)[]; required: true; }' is not assignable to type 'SchemaDefinitionProperty<Category, Appeal> | undefined'.
appealModel:
import mongoose, { Schema } from 'mongoose';
import { Appeal, Category } from './appealTypes'
const appealSchema = new mongoose.Schema<Appeal>(
{
category: {
type: Schema.Types.String,
enum: Object.values(Category),
required: true,
},
},
{ timestamps: true }
);
export default mongoose.model<Appeal>('Appeal', appealSchema)
appealTypes:
import { Types } from "mongoose";
export enum Category {
"Sadaqah",
"Zakat",
}
export interface Appeal {
_id: string;
category: Category;
}
By default, enums in TypeScript are numeric unless otherwise specified, so Mongoose cannot interpret the enum values as strings directly.
Your schema expects the enum option to be an array of strings matching the values of Category, but your Category enum currently lacks explicit string values and is represented as a numeric value by default.
You have to type enum string values explicitly:
export enum Category {
Sadaqah = "Sadaqah",
Zakat = "Zakat",
}