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
Milestone
Comments
I believe But if you pass 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);
});
}
|
@vkarpov15 I'm using May I know why the issue is closed? |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I have a similar schema like:
But when I am trying to update an existing one like
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
I am using mongoose 4.8.1
node 5.4.1
MongoDB shell version: 3.2.1
The text was updated successfully, but these errors were encountered: