Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended schema pre save middleware always runs first, even before validation #50

Open
ragulka opened this issue Aug 9, 2016 · 0 comments

Comments

@ragulka
Copy link

ragulka commented Aug 9, 2016

I have extended a schema. Both the parent and child schema have validators and pre/post save middleware. It seems that the child pre-save middleware is run even before the validators! This is a major issue, as in the pre-save one would expect the document to already be validated.

I just confirmed this with a clean install of mongoose and mongoose-schema-extend only.
The middleware & validation is run as follows:

CHILD - PRE SAVE
CHILD - PRE VALIDATE
PARENT - PRE VALIDATE
CHILD - VALIDATE
PARENT - VALIDATE
CHILD - POST VALIDATE
PARENT - POST VALIDATE
PARENT - PRE SAVE
CHILD - POST SAVE
PARENT - POST SAVE

Also, it seems that with all the other middleware, the child middleware is executed before the parent middleware. I'm not sure if this works in all cases, so the best would be to provide a toggle to change the order of these - if I wanted to run parent middleware first, and then child middleware, I should be able to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant