Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow default format validator override #330

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,17 @@ 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);
}
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;
};
Expand Down
10 changes: 10 additions & 0 deletions test/formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down