From 529e40a099fdf21c753b3b0e87935ddcecdd65e2 Mon Sep 17 00:00:00 2001 From: paddymann Date: Tue, 17 Jan 2017 21:04:41 +0100 Subject: [PATCH 1/2] Pass through options to custom validators (Code only) --- lib/validations.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/validations.js b/lib/validations.js index ae3d523cd..5487256b3 100644 --- a/lib/validations.js +++ b/lib/validations.js @@ -340,7 +340,8 @@ function validateCustom(attr, conf, err, options, done) { done = options; options = {}; } - conf.customValidator.call(this, err, done); + + conf.customValidator.call(this, err, done, options); } /*! From 163e3a6c67c97ac6447205e806c475e509ca80c5 Mon Sep 17 00:00:00 2001 From: paddymann Date: Tue, 17 Jan 2017 21:05:07 +0100 Subject: [PATCH 2/2] Add support for custom validators to return promises --- lib/validations.js | 14 +++++++++++++- test/validations.test.js | 11 +++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/validations.js b/lib/validations.js index 5487256b3..879a74fe3 100644 --- a/lib/validations.js +++ b/lib/validations.js @@ -341,7 +341,19 @@ function validateCustom(attr, conf, err, options, done) { options = {}; } - conf.customValidator.call(this, err, done, options); + if (conf.customValidator.length <= 1) { + conf.customValidator.call(this, options) + .then((isValid) => { + if (!isValid) throw new Error(); + done(); + }) + .catch(() => { + err(); + done(); + }); + } else { + conf.customValidator.call(this, err, done, options); + } } /*! diff --git a/test/validations.test.js b/test/validations.test.js index 670830047..a4b6239a6 100644 --- a/test/validations.test.js +++ b/test/validations.test.js @@ -657,6 +657,17 @@ describe('validations', function() { done(); })).should.be.false; }); + + it.only('should validate using custom async validation using promises', function(done) { + User.validateAsync('email', function(options) { + return Promise.resolve(true); + }, {}); + var u = new User({email: 'hello'}); + Boolean(u.isValid(function(valid) { + valid.should.be.true; + done(); + })).should.be.false; + }); }); describe('invalid value formatting', function() {