validations: use new regex per evaluation (#1479)

The RegExp is cloned before executing the test. Fixing issue #1475.
This commit is contained in:
Joost de Bruijn 2017-09-01 18:48:29 +02:00 committed by Kevin Delisle
parent 94a602d17e
commit f18d3487c6
2 changed files with 36 additions and 1 deletions

View File

@ -362,7 +362,8 @@ function validateFormat(attr, conf, err, options) {
if (nullCheck.call(this, attr, conf, err)) return;
if (typeof this[attr] === 'string' || typeof this[attr] === 'number') {
if (!conf['with'].test(this[attr])) {
let regex = new RegExp(conf['with']);
if (!regex.test(this[attr])) {
err();
}
} else {

View File

@ -836,6 +836,40 @@ describe('validations', function() {
u.isValid().should.be.false();
});
describe('validate format correctly on bulk creation with global flag enabled in RegExp', function() {
before(function(done) {
Employee.destroyAll(function(err) {
should.not.exist(err);
delete Employee.validations;
db.automigrate('Employee', function(err) {
should.not.exist(err);
Employee.create(empData, function(err, inst) {
should.not.exist(err);
should.exist(inst);
Employee.validatesFormatOf('name', {with: /^[a-z]+$/g, allowNull: false});
done();
});
});
});
});
it('succeeds when validate condition is met for all items', function(done) {
Employee.create([
{name: 'test'},
{name: 'test'},
{name: 'test'},
{name: 'test'},
{name: 'test'},
{name: 'test'},
], (err, instances) => {
should.not.exist(err);
should.exist(instances);
instances.should.have.lengthOf(6);
done();
});
});
});
describe('validate format on update', function() {
before(function(done) {
Employee.destroyAll(function(err) {