Skip to content

Counter increments even if document validation fails #10

@DrNightmare

Description

@DrNightmare

On trying to save a document that fails the Mongoose validation specified in the schema, the counter for the corresponding collection still gets incremented but the document does not get saved (as expected). This results in the auto incremented ids not being continuous.

Could we add a flag that checks if the validation fails for a document and not increment the counter in that case? I had a look at the pre hook on 'validate' and this could be a potential fix:

Replace:
if ((doc.isNew && !alreadyGetId) || settings.migrate) {

with:
if ((doc.isNew && !alreadyGetId && doc.validateSync === undefined) || settings.migrate) {

I'm not sure if this is the best way to achieve this, but this could be a start.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions