Skip to content

Mongoose runs array-level validators on the array and its elements #2618

@ashaffer

Description

@ashaffer
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

mongoose.connect('localhost');

var A = new Schema({str: String});
var B = new Schema({a: [A]});
B.path('a').validate(function(val, next) {
  console.log('val', val);
  next();
});

var b = mongoose.model('b', B);

var p = new b;
p.a.push({str: 'asdf'});
p.save();

If you run this code, you'll see that the validator is called twice. Once with the entire array as val, and once for each element in the array.

I'm not sure which one is the intended behavior (it used to just run on the whole array), but it certainly shouldn't be both.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions