Array casting to String

32 views
Skip to first unread message

Cristian Douce

unread,
May 21, 2013, 9:06:43 AM5/21/13
to mongoo...@googlegroups.com
I noticed some rare behavior when creating a new object with this schema:

var mySchema = new Schema({
  hash
: { type: String, lowercase: true, required: true, trim: true },
  created
: { type: Date, default: Date.now }
});


var myModel = mongoose.model('myModel', mySchema);



and this data:

var myObject = new myModel({
  hash
: ['mome', 'meme']
});


myObject
.save(function(err) {
  console
.log(err);
});



I thought that this kind of action should result into a validation type error, given that my `hash` definition is of type String and I'm providing an Array. But, the rare thing is that I don't get any error at all... and also:

'mome,meme' === myObject.hash   // outputs true.

So... my question:

Is this the expected behavior?

Thanks!

Aaron Heckmann

unread,
May 22, 2013, 2:38:08 PM5/22/13
to mongoo...@googlegroups.com
Values are cast to their type. So in this case casting an array to a string produces 'mome,meme' ( same as calling array.toString() )

You could validate within a custom setter if need be: https://gist.github.com/aheckmann/5629845


--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
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


Cristian Douce

unread,
May 23, 2013, 4:19:52 PM5/23/13
to mongoo...@googlegroups.com
So, let me know if I understand this well:

The type field on my schema path definition is just for casting values before (or post ?) validation.

And if I'd like to validate the type of the value provided I should write some validation middleware for the path and check what I'm getting.

Am I right?

Thanks!

Aaron Heckmann

unread,
May 23, 2013, 5:22:09 PM5/23/13
to mongoo...@googlegroups.com
values are cast when they are set.

yes a custom setter allows you to run any logic you like before the value is cast.



--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
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.
 
 

Cristian Douce

unread,
May 24, 2013, 12:48:10 PM5/24/13
to mongoo...@googlegroups.com
Cool, thanks!
Reply all
Reply to author
Forward
0 new messages