Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save doc with async validator on embedded doesn't work (Version 3.9.x) #2589

Closed
julien-maurel opened this issue Jan 12, 2015 · 2 comments
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@julien-maurel
Copy link

Hi all,
I found a new bug on async validator of embedded doc : callback of save method is called before all subdoc are checked.

The best is an example :

var mongoose=require('mongoose');
var schema = mongoose.Schema;
var db = mongoose.createConnection('mongodb://localhost/tests');

var v=function(value, cb){
    var self=this;
    console.log('IN VALID ' + value);
    setTimeout(function(){
    console.log('IN ASYNC VALID ' + value);
    cb(false);
    }, (value==='r'?1000:1500)); // First sub doc will be validate after 1s and second sub doc after 1.5s
    return true;
}

var s1 = new schema({
    title:String,
    validField:{
    type:String,
    validate:v
    }
});
var s2 = new schema({
    title:  String,
    s1:[s1]
});
var s3 = new schema({
    title:  String,
    s2:[s2]
});

var m3 = db.model('m3', s3);
var d = new m3({
    title:'test',
    s2:[
    {
        title:'test',
        s1:[
        {
            validField:'r'
        },
        {
            validField:'r2'
        }
        ]
    }
    ]
});
d.save(function(err,doc){
    console.log(JSON.stringify(err, null, 2));
    console.log(JSON.stringify(doc, null, 2));
});

On this example, normally output should be :

IN VALID r
IN VALID r2
IN ASYNC VALID r
IN ASYNC VALID r2
{
  "message": "Validation failed",
  "name": "ValidationError",
  "errors": {
    "s2.0.s1.0.validField": {
      "properties": {
        "type": "user defined",
        "message": "Validator failed for path `{PATH}` with value `{VALUE}`",
        "path": "validField",
        "value": "r"
      },
      "message": "Validator failed for path `validField` with value `r`",
      "name": "ValidatorError",
      "kind": "user defined",
      "path": "validField",
      "value": "r"
    },
    "s2.0.s1.1.validField": {
      "properties": {
        "type": "user defined",
        "message": "Validator failed for path `{PATH}` with value `{VALUE}`",
        "path": "validField",
        "value": "r2"
      },
      "message": "Validator failed for path `validField` with value `r2`",
      "name": "ValidatorError",
      "kind": "user defined",
      "path": "validField",
      "value": "r2"
    }
  },
  "key": "undefined.0.undefined.0.undefined"
}
undefined

but I have :

IN VALID r
IN VALID r2
IN ASYNC VALID r
{
  "message": "Validation failed",
  "name": "ValidationError",
  "errors": {
    "s2.0.s1.0.validField": {
      "properties": {
        "type": "user defined",
        "message": "Validator failed for path `{PATH}` with value `{VALUE}`",
        "path": "validField",
        "value": "r"
      },
      "message": "Validator failed for path `validField` with value `r`",
      "name": "ValidatorError",
      "kind": "user defined",
      "path": "validField",
      "value": "r"
    }
  },
  "key": "undefined.0.undefined.0.undefined"
}
undefined
IN ASYNC VALID r2

You can see that the save method callback is called immedialy after first sub doc validation, it doesn't wait the second sub doc validation.
This issue happend only if we call cb(false) to invalid a doc, but if I do cb(true), save callback is called at the end (but of course doc is valid ;)) :

IN VALID r
IN VALID r2
IN ASYNC VALID r
IN ASYNC VALID r2
null
{
  "__v": 0,
  "title": "test",
  "_id": "54b3a5975a6dcea50ef9b915",
  "s2": [
    {
      "title": "test",
      "_id": "54b3a5975a6dcea50ef9b916",
      "s1": [
        {
          "validField": "r",
          "_id": "54b3a5975a6dcea50ef9b918"
        },
        {
          "validField": "r2",
          "_id": "54b3a5975a6dcea50ef9b917"
        }
      ]
    }
  ]
}

Thanks

@vkarpov15 vkarpov15 added the bug? label Jan 12, 2015
@vkarpov15
Copy link
Collaborator

Thanks for pointing this out, I'll see if I can repro

@julien-maurel
Copy link
Author

Hi,
This issue is always present in 4.0.0-rc1. Do you think you can fix that quickly?

@vkarpov15 vkarpov15 added this to the 4.0.0-rc2 milestone Feb 3, 2015
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed bug? labels Feb 3, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

2 participants