From c3e502032edb49fd422e8d2547363ee6e35a399e Mon Sep 17 00:00:00 2001 From: bmatson Date: Wed, 11 Oct 2017 15:53:13 -0400 Subject: [PATCH] Pass options argument to custom validators --- lib/validations.js | 15 +++++++++++-- test/validations.test.js | 48 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/lib/validations.js b/lib/validations.js index 41c7cd9e..929911ce 100644 --- a/lib/validations.js +++ b/lib/validations.js @@ -379,7 +379,18 @@ function validateCustom(attr, conf, err, options, done) { done = options; options = {}; } - conf.customValidator.call(this, err, done); + if (!done) { + // called from a sync validator, stick options on end + conf.customValidator.call(this, err, options); + } else { + if (conf.customValidator.length === 3) { + // if they declared the validator with 3 args, they are expecting options + conf.customValidator.call(this, err, options, done); + } else { + // otherwise just pass the expected two (no context) + conf.customValidator.call(this, err, done); + } + } } function escapeStringRegexp(str) { @@ -543,7 +554,7 @@ Validatable.prototype.isValid = function(callback, data, options) { validationFailed(inst, attr, v, options, done); }); } else { - if (validationFailed(inst, attr, v)) { + if (validationFailed(inst, attr, v, options)) { valid = false; } } diff --git a/test/validations.test.js b/test/validations.test.js index 30796cf3..3608848a 100644 --- a/test/validations.test.js +++ b/test/validations.test.js @@ -314,8 +314,52 @@ describe('validations', function() { should.exist(e); e.should.be.instanceOf(Error); e.should.be.instanceOf(ValidationError); - d.updateAttribute('name', 'Vasiliy', {options: 'options'}, function(e) { - should.not.exist(e); + d.updateAttribute('name', 'Vasiliy', {options: 'options'}, err => { + if (err) return done(err); + // test passed + done(); + }); + }); + }); + }); + + it('passes options to custom sync validator', done => { + delete User.validations; + User.validate('name', function(err, options) { + if (options.testFlag !== 'someValue') err(); + }); + User.create({name: 'Valid'}, {testFlag: 'someValue'}, function(e, d) { + d.updateAttribute('name', null, {testFlag: 'otherValue'}, function(e) { + should.exist(e); + e.should.be.instanceOf(ValidationError); + d.updateAttribute('name', 'Vasiliy', {testFlag: 'someValue'}, err => { + if (err) return done(err); + // test passed + done(); + }); + }); + }); + }); + + it('passes options to async validator', done => { + delete User.validations; + User.validateAsync('name', function(err, options, done) { + if (options.testFlag !== 'someValue') { + console.error( + 'Unexpected validation options: %j Expected %j', + options, {testFlag: 'someValue'}); + err(); + } + process.nextTick(function() { done(); }); + }); + User.create({name: 'Valid'}, {testFlag: 'someValue'}, function(e, d) { + if (e) return done(e); + d.updateAttribute('name', null, {testFlag: 'otherValue'}, function(e) { + should.exist(e); + e.should.be.instanceOf(ValidationError); + d.updateAttribute('name', 'Vasiliy', {testFlag: 'someValue'}, err => { + if (err) return done(err); + // test passed done(); }); });