Require unique ids for embedded items

This commit is contained in:
Fabien Franzen 2014-07-27 17:30:10 +02:00
parent 43e11af942
commit e1ecb4b95f
1 changed files with 13 additions and 15 deletions

View File

@ -1406,21 +1406,19 @@ RelationDefinition.embedsMany = function hasMany(modelFrom, modelTo, params) {
type: [modelTo], default: function() { return []; }
});
// require explicit/unique ids unless autoId === true
if (definition.options.autoId === false) {
modelTo.validatesPresenceOf(idName);
modelFrom.validate(relationName, function(err) {
var embeddedList = this[relationName] || [];
var ids = embeddedList.map(function(m) { return m[idName]; });
var uniqueIds = ids.filter(function(id, pos) {
return ids.indexOf(id) === pos;
});
if (ids.length !== uniqueIds.length) {
this.errors.add(relationName, 'Contains duplicate `' + idName + '`', 'uniqueness');
err(false);
}
}, { code: 'uniqueness' })
}
// unique id is required
modelTo.validatesPresenceOf(idName);
modelFrom.validate(relationName, function(err) {
var embeddedList = this[relationName] || [];
var ids = embeddedList.map(function(m) { return m[idName]; });
var uniqueIds = ids.filter(function(id, pos) {
return ids.indexOf(id) === pos;
});
if (ids.length !== uniqueIds.length) {
this.errors.add(relationName, 'Contains duplicate `' + idName + '`', 'uniqueness');
err(false);
}
}, { code: 'uniqueness' })
// validate all embedded items
if (definition.options.validate) {