mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Custom validator fires multiple times on Mixed field

Open enrichz opened this issue 10 years ago • 3 comments

I'm trying to implement a custom validator on a Schema.Types.Mixed field. This is what I have:

var boardSchema = new Schema({
  name: {
    type: String,
    required: true
  },
  structure: {
    type: Schema.Types.Mixed,
    required: true,
    validate: {
      validator: function(v) {
        console.log("start validation on: ", v);
        var result = validateStructure(v);
        console.log("final result: ", result);
        return result;
      },
      message: 'The structure of the board is invalid'
    }
  }
});
mongoose.model('Board', boardSchema);

Then I perform the update like this:

Board
  .findOneAndUpdate({_id: boardId}, {structure: structure}, 
      {"new": true, upsert: false, passRawResult: false, "overwrite": false, 
        runValidators: true, setDefaultsOnInsert: true})
  .exec(function(err, result) {
...

The structure object is this:

"structure" : [
        {
            "capacity" : 0, 
            "size" : 0, 
            "category" : 0, 
            "isColumn" : true, 
            "title" : "Backlog", 
            "_id" : "backlog"
        }, 
        {
            "capacity" : 0, 
            "size" : 0, 
            "category" : 1, 
            "isColumn" : true, 
            "title" : "In Progress", 
            "_id" : "in_prog"
        }, 
        {
            "capacity" : 0, 
            "size" : 5, 
            "category" : 2, 
            "isColumn" : true, 
            "title" : "Archive", 
            "_id" : "archive"
        }
    ]

and the console prints this:

start validation on:  backlog
final result:  false
start validation on:  Backlog
final result:  false
start validation on:  true
final result:  false
start validation on:  0
final result:  false
start validation on:  0
final result:  false
start validation on:  0
final result:  false
start validation on:  in_prog
final result:  false
start validation on:  In Progress
final result:  false
start validation on:  true
final result:  false
start validation on:  1
final result:  false
start validation on:  0
final result:  false
start validation on:  0
final result:  false
start validation on:  archive
final result:  false
start validation on:  Archive
final result:  false
start validation on:  true
final result:  false
start validation on:  2
final result:  false
start validation on:  5
final result:  false
start validation on:  0
final result:  false
start validation on:  [ { _id: 'backlog',
    title: 'Backlog',
    isColumn: true,
    category: 0,
    size: 0,
    capacity: 0 },
  { _id: 'in_prog',
    title: 'In Progress',
    isColumn: true,
    category: 1,
    size: 0,
    capacity: 0 },
  { _id: 'archive',
    title: 'Archive',
    isColumn: true,
    category: 2,
    size: 5,
    capacity: 0 } ]
final result:  true

As you can see the validator is called multiple times, it seems once for each nested field. The last time it gets called on the correct field and it passes. Do you have any idea? I'm using mongoose 4.5.3 and mongodb 3.2

enrichz avatar Jul 07 '16 10:07 enrichz

No ideas off the top of my head, will investigate this week.

vkarpov15 avatar Jul 10 '16 20:07 vkarpov15

Thank you @vkarpov15 How can I get the fix in my project? Will this be in the next release?

enrichz avatar Jul 18 '16 08:07 enrichz

@enrichz upgrade to 4.5.5, was released a few days ago

vkarpov15 avatar Jul 22 '16 14:07 vkarpov15