Skip to content

Commit

Permalink
Control coercion with a flag
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Feb 4, 2016
1 parent ef4f1b6 commit bae6dc9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
18 changes: 18 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2523,4 +2523,22 @@ describe('Validators', function () {
});
});

it('should error on non-string input when the coerce flag is false', function () {
validator.coerce = false;

try {
validator.toString({});
assert(false);
} catch (err) {
}

validator.coerce = true;

try {
validator.toString({});
} catch (err) {
assert(false);
}
});

});
16 changes: 10 additions & 6 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

'use strict';

validator = { version: '4.6.1' };
validator = { version: '4.6.1', coerce: true };

var emailUserPart = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~]+$/i;
var quotedEmailUser = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f]))*$/i;
Expand Down Expand Up @@ -136,11 +136,15 @@
validator.toString = function (input) {
// The library validates strings only. Currently it coerces all input to a string, but this
// will go away in an upcoming major version change. Print a deprecation notice for now
if (typeof input !== 'string' && typeof console === 'object' && console
&& typeof console.warn === 'function') {
console.warn('warning: you tried to validate a ' + typeof input + ' but this library ' +
'(github.com/chriso/validator.js) validates strings only. Please update your code ' +
'as this will be an error soon.')
if (typeof input !== 'string') {
if (!validator.coerce) {
throw new Error('this library validates strings only');
}
if (typeof console === 'object' && console && typeof console.warn === 'function') {
console.warn('warning: you tried to validate a ' + typeof input + ' but this library ' +
'(github.com/chriso/validator.js) validates strings only. Please update your code ' +
'as this will be an error soon.');
}
}
if (typeof input === 'object' && input !== null) {
if (typeof input.toString === 'function') {
Expand Down
Loading

0 comments on commit bae6dc9

Please sign in to comment.