Add caseInsensitive opt to validatesUniquenessOf

This commit is contained in:
Bram Borggreve 2015-12-21 12:38:11 +00:00
parent 6342a0387f
commit c3629633de
No known key found for this signature in database
GPG Key ID: 3E5E4F27F8D464C6
2 changed files with 111 additions and 1 deletions

View File

@ -240,6 +240,7 @@ Validatable.validateAsync = getConfigurator('custom', {async: true});
* @property {Array.<String>} scopedTo List of properties defining the scope. * @property {Array.<String>} scopedTo List of properties defining the scope.
* @property {String} message Optional error message if property is not valid. Default error message: "is not unique". * @property {String} message Optional error message if property is not valid. Default error message: "is not unique".
* @property {Boolean} allowNull Whether null values are allowed. * @property {Boolean} allowNull Whether null values are allowed.
* @property {String} ignoreCase Make the validation case insensitive
*/ */
Validatable.validatesUniquenessOf = getConfigurator('uniqueness', {async: true}); Validatable.validatesUniquenessOf = getConfigurator('uniqueness', {async: true});
@ -344,6 +345,14 @@ function validateCustom(attr, conf, err, options, done) {
conf.customValidator.call(this, err, done); conf.customValidator.call(this, err, done);
} }
function escapeStringRegexp(str) {
if (typeof str !== 'string') {
throw new TypeError('Expected a string');
}
var matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g;
return str.replace(matchOperatorsRe, '\\$&');
}
/*! /*!
* Uniqueness validator * Uniqueness validator
*/ */
@ -356,7 +365,12 @@ function validateUniqueness(attr, conf, err, options, done) {
return process.nextTick(done); return process.nextTick(done);
} }
var cond = {where: {}}; var cond = {where: {}};
cond.where[attr] = this[attr];
if (conf && conf.ignoreCase) {
cond.where[attr] = new RegExp('^' + escapeStringRegexp(this[attr]) + '$', 'i');
} else {
cond.where[attr] = this[attr];
}
if (conf && conf.scopedTo) { if (conf && conf.scopedTo) {
conf.scopedTo.forEach(function(k) { conf.scopedTo.forEach(function(k) {

View File

@ -517,6 +517,102 @@ describe('validations', function() {
})).should.be.false; })).should.be.false;
}); });
}); });
it('passes case insensitive validation', function(done) {
User.validatesUniquenessOf('email', {ignoreCase: true});
var u = new User({email: 'hey'});
Boolean(u.isValid(function(valid) {
valid.should.be.true();
u.save(function(err) {
if (err) return done(err);
var u2 = new User({email: 'HEY'});
u2.isValid(function(valid) {
valid.should.be.false();
done();
});
});
})).should.be.false();
});
it('passed case sensitive validation', function(done) {
User.validatesUniquenessOf('email', {ignoreCase: false});
var u = new User({email: 'hey'});
Boolean(u.isValid(function(valid) {
valid.should.be.true();
u.save(function(err) {
if (err) return done(err);
var u2 = new User({email: 'HEY'});
u2.isValid(function(valid) {
valid.should.be.true();
done();
});
});
})).should.be.false();
});
it('passes case insensitive validation with string that needs escaping', function(done) {
User.validatesUniquenessOf('email', {ignoreCase: true});
var u = new User({email: 'me+me@my.com'});
Boolean(u.isValid(function(valid) {
valid.should.be.true();
u.save(function(err) {
if (err) return done(err);
var u2 = new User({email: 'ME+ME@MY.COM'});
u2.isValid(function(valid) {
valid.should.be.false();
done();
});
});
})).should.be.false();
});
it('passed case sensitive validation with string that needs escaping', function(done) {
User.validatesUniquenessOf('email', {ignoreCase: false});
var u = new User({email: 'me+me@my.com'});
Boolean(u.isValid(function(valid) {
valid.should.be.true();
u.save(function(err) {
if (err) return done(err);
var u2 = new User({email: 'ME+ME@MY.COM'});
u2.isValid(function(valid) {
valid.should.be.true();
done();
});
});
})).should.be.false();
});
it('passes partial case insensitive validation with string that needs escaping', function(done) {
User.validatesUniquenessOf('email', {ignoreCase: true});
var u = new User({email: 'also+me@my.com'});
Boolean(u.isValid(function(valid) {
valid.should.be.true();
u.save(function(err) {
if (err) return done(err);
var u2 = new User({email: 'Me@My.com'});
u2.isValid(function(valid) {
valid.should.be.true();
done();
});
});
})).should.be.false();
});
it('passes partial case sensitive validation with string that needs escaping', function(done) {
User.validatesUniquenessOf('email', {ignoreCase: false});
var u = new User({email: 'also+me@my.com'});
Boolean(u.isValid(function(valid) {
valid.should.be.true();
u.save(function(err) {
if (err) return done(err);
var u2 = new User({email: 'Me@My.com'});
u2.isValid(function(valid) {
valid.should.be.true();
done();
});
});
})).should.be.false();
});
}); });
describe('format', function() { describe('format', function() {