Skip to content

Commit

Permalink
Renaming errorsForPath to get
Browse files Browse the repository at this point in the history
  • Loading branch information
bromanko committed Jul 30, 2014
1 parent c752108 commit e988463
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 67 deletions.
4 changes: 2 additions & 2 deletions lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ error.ValidationError = function(message, errors) {
util.inherits(error.ValidationError, error.BaseError);

/**
* Finds all validation error items for the path specified.
* Gets all validation error items for the path specified.
*
* @param {string} path The path to be checked for error items
* @returns {Array} Validation error items for the specified path
*/
error.ValidationError.prototype.errorsForPath = function(path) {
error.ValidationError.prototype.get = function(path) {
return this.errors.reduce(function(reduced, error) {
if (error.path === path) {
reduced.push(error);
Expand Down
14 changes: 7 additions & 7 deletions test/dao-factory/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,16 +425,16 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
UserNull.create({ username: 'foo2', smth: null }).error(function(err) {
expect(err).to.exist

expect(err.errorsForPath('smth')[0].path).to.equal('smth');
expect(err.get('smth')[0].path).to.equal('smth');
if (Support.dialectIsMySQL()) {
// We need to allow two different errors for MySQL, see:
// http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html#sqlmode_strict_trans_tables
expect(err.errorsForPath('smth')[0].type).to.match(/notNull Violation/)
expect(err.get('smth')[0].type).to.match(/notNull Violation/)
}
else if (dialect === "sqlite") {
expect(err.errorsForPath('smth')[0].type).to.match(/notNull Violation/)
expect(err.get('smth')[0].type).to.match(/notNull Violation/)
} else {
expect(err.errorsForPath('smth')[0].type).to.match(/notNull Violation/)
expect(err.get('smth')[0].type).to.match(/notNull Violation/)
}
done()
})
Expand Down Expand Up @@ -480,7 +480,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
expect(str2.str).to.equal('http://sequelizejs.org')
StringIsNullOrUrl.create({ str: '' }).error(function(err) {
expect(err).to.exist
expect(err.errorsForPath('str')[0].message).to.match(/Validation isURL failed/)
expect(err.get('str')[0].message).to.match(/Validation isURL failed/)

done()
})
Expand Down Expand Up @@ -1054,10 +1054,10 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
expect(errors).to.be.an('Array')
expect(errors).to.have.length(2)
expect(errors[0].record.code).to.equal('1234')
expect(errors[0].errors.errorsForPath('name')[0].type).to.equal('notNull Violation')
expect(errors[0].errors.get('name')[0].type).to.equal('notNull Violation')
expect(errors[1].record.name).to.equal('bar')
expect(errors[1].record.code).to.equal('1')
expect(errors[1].errors.errorsForPath('code')[0].message).to.equal('Validation len failed')
expect(errors[1].errors.get('code')[0].message).to.equal('Validation len failed')
done()
})
})
Expand Down
22 changes: 11 additions & 11 deletions test/dao.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -941,9 +941,9 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
this.User.create({aNumber: 0, validateTest: 'hello'}).error(function(err){
expect(err).to.exist
expect(err).to.be.instanceof(Object)
expect(err.errorsForPath('validateTest')).to.be.instanceof(Array)
expect(err.errorsForPath('validateTest')[0]).to.exist
expect(err.errorsForPath('validateTest')[0].message).to.equal('Validation isInt failed')
expect(err.get('validateTest')).to.be.instanceof(Array)
expect(err.get('validateTest')[0]).to.exist
expect(err.get('validateTest')[0].message).to.equal('Validation isInt failed')
done()
})
})
Expand All @@ -953,10 +953,10 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
.error(function(err){
expect(err).to.exist
expect(err).to.be.instanceof(Object)
expect(err.errorsForPath('validateCustom')).to.exist
expect(err.errorsForPath('validateCustom')).to.be.instanceof(Array)
expect(err.errorsForPath('validateCustom')[0]).to.exist
expect(err.errorsForPath('validateCustom')[0].message).to.equal('Length failed.')
expect(err.get('validateCustom')).to.exist
expect(err.get('validateCustom')).to.be.instanceof(Array)
expect(err.get('validateCustom')[0]).to.exist
expect(err.get('validateCustom')[0].message).to.equal('Length failed.')
done()
})
})
Expand All @@ -966,10 +966,10 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
user.updateAttributes({validateTest: 'hello'}).error(function(err){
expect(err).to.exist
expect(err).to.be.instanceof(Object)
expect(err.errorsForPath('validateTest')).to.exist
expect(err.errorsForPath('validateTest')).to.be.instanceof(Array)
expect(err.errorsForPath('validateTest')[0]).to.exist
expect(err.errorsForPath('validateTest')[0].message).to.equal('Validation isInt failed')
expect(err.get('validateTest')).to.exist
expect(err.get('validateTest')).to.be.instanceof(Array)
expect(err.get('validateTest')[0]).to.exist
expect(err.get('validateTest')[0].message).to.equal('Validation isInt failed')
done()
})
})
Expand Down
36 changes: 18 additions & 18 deletions test/dao.validations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
failingUser.validate().done( function(err, _errors) {
expect(_errors).not.to.be.null;
expect(_errors).to.be.an.instanceOf(Error);
expect(_errors.errorsForPath('name')[0].message).to.equal(message);
expect(_errors.get('name')[0].message).to.equal(message);
done();
});
});
Expand Down Expand Up @@ -296,7 +296,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
Model.create({name: 'World'}).success(function(model) {
model.updateAttributes({name: ''}).error(function(err) {
expect(err).to.be.an.instanceOf(Error)
expect(err.errorsForPath('name')[0].message).to.equal('Validation notEmpty failed');
expect(err.get('name')[0].message).to.equal('Validation notEmpty failed');
done()
})
})
Expand All @@ -318,7 +318,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
Model.create({name: 'World'}).success(function() {
Model.update({name: ''}, {id: 1}).error(function(err) {
expect(err).to.be.an.instanceOf(Error)
expect(err.errorsForPath('name')[0].message).to.equal('Validation notEmpty failed')
expect(err.get('name')[0].message).to.equal('Validation notEmpty failed')
done()
})
})
Expand Down Expand Up @@ -395,7 +395,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
User.sync({ force: true }).success(function() {
User.create({id: 'helloworld'}).error(function(err) {
expect(err).to.be.an.instanceOf(Error)
expect(err.errorsForPath('id')[0].message).to.equal('Validation isInt failed')
expect(err.get('id')[0].message).to.equal('Validation isInt failed')
done()
})
})
Expand All @@ -416,7 +416,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
User.sync({ force: true }).success(function() {
User.create({username: 'helloworldhelloworld'}).error(function(err) {
expect(err).to.be.an.instanceOf(Error)
expect(err.errorsForPath('username')[0].message).to.equal('Username must be an integer!')
expect(err.get('username')[0].message).to.equal('Username must be an integer!')
done()
})
})
Expand All @@ -443,7 +443,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
it('should emit an error when we try to enter in a string for the id key with validation arguments', function(done) {
this.User.create({id: 'helloworld'}).error(function(err) {
expect(err).to.be.an.instanceOf(Error)
expect(err.errorsForPath('id')[0].message).to.equal('ID must be an integer!')
expect(err.get('id')[0].message).to.equal('ID must be an integer!')
done()
})
})
Expand All @@ -453,7 +453,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {

user.validate().success(function(err) {
expect(err).to.be.an.instanceOf(Error)
expect(err.errorsForPath('id')[0].message).to.equal('ID must be an integer!')
expect(err.get('id')[0].message).to.equal('ID must be an integer!')
done()
})
})
Expand All @@ -462,7 +462,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
var user = this.User.build({id: 'helloworld'})
user.save().error(function(err) {
expect(err).to.be.an.instanceOf(Error)
expect(err.errorsForPath('id')[0].message).to.equal('ID must be an integer!')
expect(err.get('id')[0].message).to.equal('ID must be an integer!')
done()
})
})
Expand Down Expand Up @@ -538,7 +538,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
failingUser.validate().success(function(error) {
expect(error).to.be.an.instanceOf(Error)

expect(error.errorsForPath('name')[0].message).to.equal("name should equal '2'")
expect(error.get('name')[0].message).to.equal("name should equal '2'")

var successfulUser = User.build({ name : "2" })
successfulUser.validate().success(function(err) {
Expand Down Expand Up @@ -569,7 +569,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
User.sync().success(function () {
User.build({ name : "error" }).validate().success(function(error) {
expect(error).to.be.an.instanceOf(self.sequelize.ValidationError)
expect(error.errorsForPath('name')[0].message).to.equal("Invalid username")
expect(error.get('name')[0].message).to.equal("Invalid username")

User.build({ name : "no error" }).validate().success(function(errors) {
expect(errors).not.to.be.defined
Expand All @@ -596,7 +596,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
.success(function(error) {
expect(error).not.to.be.null
expect(error).to.be.an.instanceOf(Error)
expect(error.errorsForPath('age')[0].message).to.equal("must be positive")
expect(error.get('age')[0].message).to.equal("must be positive")

User.build({ age: null }).validate().success(function() {
User.build({ age: 1 }).validate().success(function() {
Expand Down Expand Up @@ -634,7 +634,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
.success(function(error) {
expect(error).not.to.be.null
expect(error).to.be.an.instanceOf(Error)
expect(error.errorsForPath('xnor')[0].message).to.equal('xnor failed');
expect(error.get('xnor')[0].message).to.equal('xnor failed');
Foo
.build({ field1: 33, field2: null })
.validate()
Expand Down Expand Up @@ -671,7 +671,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
.success(function(error) {
expect(error).not.to.be.null
expect(error).to.be.an.instanceOf(Error)
expect(error.errorsForPath('xnor')[0].message).to.equal('xnor failed')
expect(error.get('xnor')[0].message).to.equal('xnor failed')

Foo
.build({ field1: 33, field2: null })
Expand Down Expand Up @@ -720,8 +720,8 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {

failingBar.validate().success(function(errors) {
expect(errors).not.to.be.null
expect(errors.errorsForPath('field')).to.have.length(1)
expect(errors.errorsForPath('field')[0].message).to.equal("Validation isIn failed")
expect(errors.get('field')).to.have.length(1)
expect(errors.get('field')[0].message).to.equal("Validation isIn failed")
})
})

Expand Down Expand Up @@ -764,7 +764,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
.done(function(errors){
expect(errors).to.not.be.null
expect(errors).to.be.an.instanceOf(Error)
expect(errors.errorsForPath('name')[0].message).to.eql('Validation isImmutable failed')
expect(errors.get('name')[0].message).to.eql('Validation isImmutable failed')
done()
})
})
Expand Down Expand Up @@ -862,7 +862,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
salt: '42'
}).validate().then(function (errors) {
expect(errors).not.to.be.undefined
expect(errors.errorsForPath('password')[0].message).to.equal('Please choose a longer password')
expect(errors.get('password')[0].message).to.equal('Please choose a longer password')
}),
User.build({
password: 'loooooooong',
Expand Down Expand Up @@ -896,7 +896,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
name: 'a'
}).validate()
}).then(function (errors) {
expect(errors.errorsForPath('name')[0].message).to.equal('Validation isExactly7Characters failed')
expect(errors.get('name')[0].message).to.equal('Validation isExactly7Characters failed')
})
})
})
Expand Down
6 changes: 3 additions & 3 deletions test/error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ describe(Support.getTestDialectTeaser("Sequelize Errors"), function () {
new Sequelize.ValidationErrorItem('invalid', 'type', 'last_name', null)
];
var validationError = new Sequelize.ValidationError('Validation error', errorItems);
expect(validationError).to.have.property('errorsForPath');
expect(validationError.errorsForPath).to.be.a('function');
expect(validationError).to.have.property('get');
expect(validationError.get).to.be.a('function');

var matches = validationError.errorsForPath('first_name');
var matches = validationError.get('first_name');
expect(matches).to.be.instanceOf(Array);
expect(matches).to.have.lengthOf(1);
expect(matches[0]).to.have.property('message', 'invalid')
Expand Down
Loading

0 comments on commit e988463

Please sign in to comment.