mongoose-unique-validator / mongoose-unique-validator

mongoose-unique-validator is a plugin which adds pre-save validation for unique fields within a Mongoose schema.
548 stars 74 forks source link

Issue while using "overwrite: true". #104

Open sweetpalma opened 5 years ago

sweetpalma commented 5 years ago

I have a following model (defined using @tsed/mongoose:

@Model()
@MongoosePlugin(UniqueValidator)
export class Phone {
  @Unique()
  @Required()
  @Property()
  serial!: string;
}

When I try to find and update it with the following code - validation works:

const car = await this.model.findOneAndUpdate(filter, props, {new: true, context: 'query', runValidators: true});

But when I try to overwrite it - it throws me validation error regarding unique constraint:

const car = await this.model.findOneAndUpdate(filter, props, {new: true, overwrite: true, context: 'query', runValidators: true});

Am I missing something? Or is that really a bug? I am using version 2.0.3.

viveleroi commented 5 years ago

It's probably a bug. My bet is that the document gets flagged as "new" internally so we need to explicitly look for overwrite.