Skip to content

Commit

Permalink
Use a library to print deprecation notices, re #487
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Feb 5, 2016
1 parent 6e23933 commit 7bc5472
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#### HEAD

- Use [node-depd](https://github.com/dougwilson/nodejs-depd) to print deprecation notices
([#487](https://github.com/chriso/validator.js/issues/487))

#### 4.7.0

- Print a deprecation warning if validator input is not a string
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"type": "git",
"url": "http://github.com/chriso/validator.js.git"
},
"dependencies": {
"depd": "1.1.0"
},
"devDependencies": {
"mocha": "latest",
"istanbul": "latest",
Expand Down
23 changes: 16 additions & 7 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,27 @@
}
};

var depd = null;
validator.deprecation = function (msg) {
if (depd === null) {
if (typeof require !== 'function') {
return;
}
depd = require('depd')('validator');
}
depd(msg);
};

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') {
// 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 (!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.');
}
validator.deprecation('you tried to validate a ' + typeof input + ' but this library ' +
'(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 7bc5472

Please sign in to comment.