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

Validation for elements in array not happening on update #4953

Closed
erickcsh opened this issue Feb 3, 2017 · 2 comments
Closed

Validation for elements in array not happening on update #4953

erickcsh opened this issue Feb 3, 2017 · 2 comments
Milestone

Comments

@erickcsh
Copy link

erickcsh commented Feb 3, 2017

I have a similar schema like:

var childSchema = new Schema({
  info: {
    name: {
       type: String,
       required: true
    },
    lastName: {
       type: String,
       required: true
    }
  }
})

var parentSchema = new Schema({
  name: {
    type: String,
    required: true
  },
  children: [childSchema]
})

model = mongoose.model('Parent', parentSchema) 

But when I am trying to update an existing one like

Parent.findByIdAndUpdate('id', {
 $addToSet: { children: { info: {  name: 'Test' } }
}, { new: true })

The children is being added, and the required validation for the lastName property is not happening. I can actually see the object in the database now being

{ _id: 'id',
  name: 'SomeName',
  children: [{ name: 'Test' }]

I am using mongoose 4.8.1
node 5.4.1
MongoDB shell version: 3.2.1

@sobafuchs
Copy link
Contributor

sobafuchs commented Feb 3, 2017

I believe required validators only run in $unset operations.

But if you pass runValidators: true to your findByIdAndUpdate() options, it looks like the subdoc still doesn't get validated:

const co = require('co');
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;

const GITHUB_ISSUE = `gh-49531`;

var childSchema = new mongoose.Schema({
  info: {
    name: {
      type: String,
      required: true
    },
    lastName: {
      type: String,
      required: true
    }
  }
})


childSchema.path('info.name').validate(function(val) {
  return val && val.length;
});

childSchema.path('info.lastName').validate(function(val) {
  return val && val.length;
});

var parentSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  children: [childSchema]
})

mongoose.connect(`mongodb://localhost:27017/${ GITHUB_ISSUE }`);

const Model = mongoose.model('Parent', parentSchema);

exec()
  .then(() => {
    console.log('successful');
    process.exit(0);
  })
  .catch(error => {
    console.error(`Error: ${ error }\n${ error.stack }`);
    process.exit(2);
  })
function exec() {
  return co(function* () {
    const parent = yield Model.create({ name: 'Bob' });

    // parent.children.push({ info: { name: 'Test' } });
    // yield parent.save();
    const update = yield Model.findByIdAndUpdate(parent._id, {
      $addToSet: { children: { info: { name: 'Test' } } }
    }, { new: true, runValidators: true })

    console.log('parent', update); 
  });

}

@sobafuchs sobafuchs added the bug? label Feb 3, 2017
@sobafuchs sobafuchs added this to the 4.8.2 milestone Feb 3, 2017
vkarpov15 added a commit that referenced this issue Feb 7, 2017
@Saravanan90
Copy link

@vkarpov15 I'm using v4.11.3, the issue still persists as reported https://stackoverflow.com/questions/43467316/addtoset-in-mongoose-does-not-run-custom-update-validation

May I know why the issue is closed?

@Automattic Automattic locked as resolved and limited conversation to collaborators Jan 17, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants