Are custom validators not supposed to work on document creation?

22 views
Skip to first unread message

Ethan Garofolo

unread,
Feb 14, 2013, 8:49:52 AM2/14/13
to mongoo...@googlegroups.com
Gist

Some googling turned up that validators (least in the past) aren't always called.  Is it the case that they aren't called on document creation?

While in this particular case I'm essentially reinventing the wheel on validating that a field is present, I'm not looking for a workaround on that particular validation case.  I understand that I can use "require: true".  I have other things I want to validate, and if the machinery isn't invoked, then that's a problem for my current approach.

Thanks in advance for any help.

I'm really enjoying using Mongoose, btw.  Very nice library.

Ethan Garofolo

unread,
Feb 14, 2013, 9:19:12 AM2/14/13
to mongoo...@googlegroups.com
Update:

I tried also using the catSchema.path form, and the results are the same.

Adding Mongoose.set('debug', true) gave the following additional output:

Mongoose: cats.insert({ __v: 0, _id: ObjectId("511cf213d4cfb44978000001") }) {}

Aaron Heckmann

unread,
Feb 14, 2013, 6:57:35 PM2/14/13
to mongoo...@googlegroups.com
"Validation occurs when a document attempts to be saved, after defaults have been applied"

http://mongoosejs.com/docs/validation.html


--
--
http://mongoosejs.com - docs
http://plugins.mongoosejs.com - plugins search
http://github.com/learnboost/mongoose - source code
 
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Aaron


Ethan Garofolo

unread,
Feb 14, 2013, 9:02:04 PM2/14/13
to mongoo...@googlegroups.com
Looking at the code, it looked like create calls into the same code path as save. Am I understanding that incorrectly?  Create is like new + save, right?

I'll try breaking it out using new and save and see if that does the trick.

I also upgraded to today's release for kicks, and there is no difference.

Ethan Garofolo

unread,
Feb 15, 2013, 7:28:34 AM2/15/13
to mongoo...@googlegroups.com
So, create never worked, but new + save did.  For it to engage the validation, I had to actually include an empty string for the attribute listed in the gist that I posted.  Passing in an object that didn't have the attribute I was validating never called the validation.  Adding required: true did flag it down as required, but it was surprising that the validator was not invoked unless I had an empty attribute passed in.

Aaron Heckmann

unread,
Feb 15, 2013, 1:25:07 PM2/15/13
to mongoo...@googlegroups.com
This is correct. If the value is not required and does not exist, why run any validation?
Reply all
Reply to author
Forward
0 new messages