Require unique ids for embedded items
This commit is contained in:
parent
43e11af942
commit
e1ecb4b95f
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue