nestjs/mongoose

SchemaFactory trigger an error when a schema doesn't have the @Schema decorator

Closed this issue · 6 comments

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

Instead of declaring my @Schema decorator in the class that I passed to SchemaFactory.createForClass, I accidentally declared this decorator in another class in my file. Although it was my mistake, I believe that if there had been a error exposed I would have had greater visibility into what I did wrong.

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';

@Schema({
  collection: 'collection',
})
class ObjectClass {
  @Prop({
    required: true,
  })
  id: string;
}

export class Entity {
  @Prop()
  object: ObjectClass;

  @Prop()
  name: string;
}

export const Schema = SchemaFactory.createForClass(Entity);

Until I realized that this @Schema was out of place, I couldn't use updateOne, although I was able to use findOne.

Describe the solution you'd like

Trigger an error if my schema is not successful.

Teachability, documentation, adoption, migration strategy

No response

What is the motivation / use case for changing the behavior?

As mentioned, I spent quite some time looking for solutions related to Mongoose or MongoDB, only to realize that the issue was simply a declaration error in the schema. I understand that it was my mistake, however, this can help people avoid going through the same debugging process if they accidentally have a wrong declaration in the schema.

Would you like to create a PR for this issue?

I really want to help, but I've never made a contribution to the Nest project. Do you think this issue would be a good starting point? I don't know what size it can be. @kamilmysliwiec

I think this one should be fairly simple so if you have some time, feel free to give it a shot :)

I will try!

@kamilmysliwiec Hi, I opened the PR!

Let's track this here #1703