mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

nested object values are not validated with save() when to insert model

Open sharkboy1976 opened this issue 12 years ago • 8 comments

i have an unexpected behavier when validating an object with nested objects. the validation function gets not called when to insert new record. values in first level get validated in both cases.

(using mongoose 3.8.6)

var blogSchema = Schema({
  title: String,
  meta: {
    votes: Number,
    favs: Number,
    abc: String
  }
});

var Blog = mong1.model('Blog', blogSchema);

Blog.schema.path('title').validate(function (value) {
  return value.length < 100;
}, 'Invalid title');
Blog.schema.path('meta.votes').validate(function (value) {
  return value > 100;
}, 'Invalid meta.votes');

// insert
var blog = new Blog();
blog.title = 'title';
blog.meta = {votes: 22, favs: 444, abc: 'foo'};
blog.save(); // no validation error on meta.votes, but expected

// update
Blog.findById(data._id, function (err, blog) {
blog.meta.votes = 22;
blog.save(); // validation error on meta.votes

sharkboy1976 avatar Feb 06 '14 07:02 sharkboy1976

has anybody looked into this issue yet?

the issue is that "title" gets validated both times, whereas "meta.votes" only gets validated in update.

sharkboy1976 avatar Feb 20 '14 14:02 sharkboy1976

I get this issue as well. Any workaround?

refaelos avatar Oct 03 '14 12:10 refaelos

Not sure yet, I'll investigate when I get a chance.

vkarpov15 avatar Oct 06 '14 16:10 vkarpov15

I believe I have also noticed this problem, it would at least explain why some fields are passing validation despite the fact they should fail.

c1moore avatar Oct 09 '14 00:10 c1moore

Having exact same problem. What's the recommended workaround?

valentinkostadinov avatar Nov 18 '14 22:11 valentinkostadinov

Workaround for now is to use either

var blog = new Blog({ title: 'title', meta: {votes: 22, favs: 444, abc: 'foo'} });

Or

blog.meta.votes = 22;

vkarpov15 avatar Dec 05 '14 22:12 vkarpov15

Has this bug still not been fixed? I have just hit the same issue.

ra4king avatar Apr 08 '17 09:04 ra4king

Looking more closely into it, this was fixed with #4111 in 4.4.16. Below script errors out correctly since that release:

var mongoose = require('mongoose');

mongoose.set('debug', true);
mongoose.connect('mongodb://localhost:27017/test');

var blogSchema = mongoose.Schema({
  title: String,
  meta: {
    votes: Number,
    favs: Number,
    abc: String
  }
});

var Blog = mongoose.model('Blog', blogSchema);

Blog.schema.path('title').validate(function (value) {
  return value.length < 100;
}, 'Invalid title');
Blog.schema.path('meta.votes').validate(function (value) {
  return value > 100;
}, 'Invalid meta.votes');

// insert
var blog = new Blog();
blog.title = 'title';
blog.meta = {votes: 22, favs: 444, abc: 'foo'};
blog.save(function(error) {
  console.log('error', error);
  process.exit(0);
}); 

vkarpov15 avatar Apr 20 '17 01:04 vkarpov15