不要怂,就是干,撸起袖子干!

Commit ab1088e5 by Jan Aagaard Meier

change(bulkcreate/validation): Throw AggregateError instead of an array. Closes #5019

1 parent 18da2d19
......@@ -2,11 +2,13 @@
- [CHANGED] Remove `hookValidate` in favor of `validate` with `hooks: true | false`.
- [REMOVED] Support for `referencesKey`
- [CHANGED] Throw if `dialect` is not provided to the constructor
- [CHANGED] Throw `bluebird.AggregateError` instead of array from `bulkCreate` when validation fails
## BC breaks:
- `hookValidate` removed in favor of `validate` with `hooks: true | false`. `validate` returns a promise which is rejected if validation fails
- Removed support for `referencesKey`, use a `references` object
- Remove default dialect
- When `bulkCreate` is rejected because of validation failure it throws a `bluebird.AggregateError` instead of an array. This object is an array-like so length and index access will still work, but `instanceof` array will not
# 3.23.2
- [FIXED] Type validation now works with non-strings due to updated validator@5.0.0 [#5861](https://github.com/sequelize/sequelize/pull/5861)
......
......@@ -2098,6 +2098,8 @@ Model.prototype.insertOrUpdate = Model.prototype.upsert;
* and SQLite do not make it easy to obtain back automatically generated IDs and other default values in a way that can be mapped to multiple records.
* To obtain Instances for the newly created values, you will need to query for them again.
*
* If validation fails, the promise is rejected with an array-like [AggregateError](http://bluebirdjs.com/docs/api/aggregateerror.html)
*
* @param {Array} records List of objects (key/value pairs) to create instances from
* @param {Object} [options]
* @param {Array} [options.fields] Fields to insert (defaults to all fields)
......@@ -2162,7 +2164,7 @@ Model.prototype.bulkCreate = function(records, options) {
}).then(function() {
// Validate
if (options.validate) {
var errors = []
var errors = new Promise.AggregateError()
, validateOptions = _.clone(options);
validateOptions.hooks = options.individualHooks;
......@@ -2173,7 +2175,7 @@ Model.prototype.bulkCreate = function(records, options) {
}).then(function() {
delete options.skip;
if (errors.length) {
return Promise.reject(errors);
throw errors;
}
});
}
......
......@@ -2738,13 +2738,10 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}
});
return user.bulkCreate(data, {
return expect(user.bulkCreate(data, {
validate: true,
individualHooks: true
})
.catch(function(errors) {
expect(errors).to.be.instanceof(Array);
});
})).to.be.rejectedWith(Promise.AggregateError);
});
});
});
......@@ -1570,8 +1570,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
{code: '1234'},
{name: 'bar', code: '1'}
], { validate: true }).catch(function(errors) {
expect(errors).to.not.be.null;
expect(errors).to.be.an('Array');
expect(errors).to.be.instanceof(Promise.AggregateError);
expect(errors).to.have.length(2);
expect(errors[0].record.code).to.equal('1234');
expect(errors[0].errors.get('name')[0].type).to.equal('notNull Violation');
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!