Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Open
sharkboy1976 opened this Issue · 6 comments

5 participants

sharkboy1976 Refael Dakar Valeri Karpov Calvin Moore Valentin Kostadinov
sharkboy1976

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

has anybody looked into this issue yet?

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

Refael Dakar

I get this issue as well. Any workaround?

Valeri Karpov vkarpov15 added the bug? label
Valeri Karpov
Owner

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

Calvin Moore

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.

Valentin Kostadinov

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

Valeri Karpov vkarpov15 added this to the 3.8.21 milestone
Valeri Karpov
Owner

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;
Valeri Karpov vkarpov15 added confirmed-bug and removed bug? labels
Valeri Karpov vkarpov15 removed this from the 3.8.21 milestone
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.