diff --git a/lib/helpers.js b/lib/helpers.js index 508432f..a83b02b 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -186,6 +186,10 @@ FORMAT_REGEXPS.pattern = FORMAT_REGEXPS.regex; FORMAT_REGEXPS.ipv4 = FORMAT_REGEXPS['ip-address']; exports.isFormat = function isFormat (input, format, validator) { + if (validator && validator.customFormats && + typeof validator.customFormats[format] === 'function') { + return validator.customFormats[format](input); + } if (typeof input === 'string' && FORMAT_REGEXPS[format] !== undefined) { if (FORMAT_REGEXPS[format] instanceof RegExp) { return FORMAT_REGEXPS[format].test(input); @@ -193,9 +197,6 @@ exports.isFormat = function isFormat (input, format, validator) { if (typeof FORMAT_REGEXPS[format] === 'function') { return FORMAT_REGEXPS[format](input); } - } else if (validator && validator.customFormats && - typeof validator.customFormats[format] === 'function') { - return validator.customFormats[format](input); } return true; }; diff --git a/test/formats.js b/test/formats.js index 830465a..7db6c36 100644 --- a/test/formats.js +++ b/test/formats.js @@ -328,6 +328,16 @@ describe('Formats', function () { ((new Validator()).customFormats.boo).should.be.a('function'); }); }); + + describe('override default format validation', function () { + beforeEach(function () { + this.validator.customFormats.date = input => false + }) + + it('should fail any date validation', function () { + this.validator.validate('2021-02-01', {type: 'string', format: 'date'}).valid.should.be.false + }) + }) }); describe('with options.disableFormat === true', function() {