mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Update validators checking for required fields [subdocuments]

Open enrichz opened this issue 9 years ago • 9 comments

I'm using findOneAndUpdate ($set and $unset) with the option runValidators: true. I see from the documentation that it does not check for required fields if not explicitely using $unset. Does anyone know a workaround for this? How likely will this behaviour change in the future? Thanks

enrichz avatar May 18 '17 15:05 enrichz

What exactly are you trying to achieve? required doesn't really make sense with update validators because mongoose doesn't know whether the field is set or not when it is running validation.

vkarpov15 avatar May 18 '17 19:05 vkarpov15

This is the scenario:

var dSchema = new mongoose.Schema({
  d1: {
    type: String,
    required: true
  },
  d2: {
    type: String
  }
}, {_id: false});

var toastSchema = new Schema({
  _id: {
    type: String,
    required: true
  },
  a: {
    type: String
  },
  b: {
    type: Number,
    required: true,
    max: 2,
    min: 1
  },
  c: {
    c1: {
      type: Number,
      required: true,
      max: 2,
      min: 1
    },
    c2: {
      type: String
    }
  },
  d: dSchema
});
mongoose.model("Toast", toastSchema);

When I update a document, I could insert an object without d1, which is required. Let's say I have this document in the db:

{
      _id: "aaa",
      a: "hi",
      b: 1,
      c: {
        c1: "how",
        c2: "are"
      },
      d: {
        d1: "you",
        d2: "doing"
      }
}

Then I can perform the following code without errors:

Toast.findOneAndUpdate({_id: "aaa"}, 
      {$set: {d: {d2: "today"}}},
      { runValidators: true }, 
  function(err) {
    if (err) {
      // no errors here
    }
    ...
});

I find this to be a odd behaviour, am I missing something?

enrichz avatar May 19 '17 12:05 enrichz

yeah, i would use document.save() in this case. an update op won't validate or required fields unless you use $unset

sobafuchs avatar May 22 '17 05:05 sobafuchs

Also, c1 gets validated correctly:

Toast.findOneAndUpdate({_id: "aaa"}, 
      {$set: {c: {c1: 5}}},
      { runValidators: true }, 
  function(err) {
    if (err) {
      // error here!
    }
    ...
});

This code throws an error as expected: ValidatorError: Path c.c1 (5) is more than maximum allowed value (2).

enrichz avatar May 22 '17 07:05 enrichz

It isnt validating for the required, but for max. Can you try using .save? Or does that not meet your use case?

sobafuchs avatar May 22 '17 13:05 sobafuchs

Yeah I know it's for max, I was just wondering why validate max and not required, that's all. Unfortunately I can't use save. Is this considered a bug or a wanted behaviour? I just need to know if I have to implement some application logic to fix this or wait for a future release.

enrichz avatar May 22 '17 14:05 enrichz

When you $set, mongoose has no way of knowing what fields are already set on the document in MongoDB, so update validators don't run unless you explicitly set or unset. It's an inconsistent behavior, but update validation doesn't really work right without this inconsistency

vkarpov15 avatar May 24 '17 05:05 vkarpov15

I understand that mongoose doesn't know what's on MongoDB, and the fact that it validates only the fields that are being set makes perfect sense. The problem is with the nested documents. From my example, I would expect that when I $set the field d of the document (so d is being $set), mongoose would validate the entire thing that I'm setting ( {$set: {d: {d2: "today"}}} ). At the time of the operation, mongoose does know which fields are present (ie: d2) and which are not (ie: d1), and it doens't need to know what's on the db. I hope I explained myself.

enrichz avatar May 24 '17 07:05 enrichz

I made a gist for this. Let me know if there's any problem with it.

enrichz avatar May 31 '17 12:05 enrichz