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