Validation of individual elements in an array

1,754 views
Skip to first unread message

Venkat Malladi

unread,
May 2, 2013, 5:23:55 AM5/2/13
to mongoo...@googlegroups.com
Hello all,

I have a schema like this:

    var customValidator = function(value) {
        console.log(value);
        return true;
    };

    var TestSchema = new mongoose.Schema({
        object_of_strings: {
            first: {type: String, validate: customValidator},
            second: {type: String, validate: customValidator}
        },
        array_of_strings: [{type: String, validate: customValidator}]
    });

    var TestModel = db.model('Test', TestSchema);
    var testObject = new TestModel();

    testObject.object_of_strings.first = 'object first';
    testObject.object_of_strings.second = 'object second';
    testObject.array_of_strings.push('array first');
    testObject.array_of_strings.push('array second');

    testObject.save(function(err) {
        if (err) {
            console.log('failure');
        } else {
            console.log('success');
        }
    });

When I save a document of this type, it prints "object first" and "object second", but not "array first" and "array second".

Why is validation not being called on the individual elements of the array?

Hylke Bron

unread,
May 2, 2013, 5:40:28 AM5/2/13
to mongoo...@googlegroups.com
Hi,
 
I am not sure why it behaves like this, but it looks like you try to validate the contents of this array, instead of the array itself.
 
I would suggest you try the following after you defined your schema:
TestSchema.path("array_of_strings").validate(customValidator);
 
and define array_of_strings as the following: array_of_strings: [{type: String}]
 
The results would be:
object first
object second
[ array first, array second ]
success
In this case the value that will be validated is the array that is included, once you know this you can easily loop through the items to validate them individually.
 
I hope this helps you a bit, but the initial question remains.
 
Hylke Bron

Venkat Malladi

unread,
May 2, 2013, 8:22:48 AM5/2/13
to mongoo...@googlegroups.com
Thanks for the reply!

So it looks like the custom validator is the way to go. But it also means that I won't be able to use the built in String validators and setters like 'enum', 'trim', 'uppercase' etc.?


--
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 Heckmann

unread,
May 2, 2013, 12:33:40 PM5/2/13
to mongoo...@googlegroups.com
There is an issue open to provide better support for arrays.

Another way to write that schema is

    new Schema({ array: { type: [String], validate: customValidator }})
Reply all
Reply to author
Forward
0 new messages