Shema Interfaces
export interface MyCat {
name: string;
color: string;
}
export type Cat = MyCat & Document;
export const CatSchema = new Schema({
name: {
type: String,
required: true,
},
color: {
type: String,
required: true,
}
});
The data transfer object that the function is supposed to receive (without a color property)
export class CreateCatDto {
@IsString()
readonly name: string = 'Franco';
}
When the function is called, it does not show any error on new Cat(cat)
, but throws an error at runtime in Mongoose stating missing required attributes.
constructor(@InjectModel('Cat') private readonly catModel: Model<Cat>) {}
async create(cat: CreateCatDto) {
// I expect TypeScript to give me an error here :(
const createdCat = new this.catModel(cat);
return await createdCat.save();
}
My concern is, how can I ensure that the model functions understand what they need to receive accurately? It seems like they often just accept the any
type.