Skip to content

Commit

Permalink
Merge pull request #895 from DCtheTall/master
Browse files Browse the repository at this point in the history
Have assertString.js provide the invalid type passed to validator.
  • Loading branch information
chriso committed Sep 24, 2018
2 parents 8474862 + d859be0 commit 3fe78c1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
16 changes: 15 additions & 1 deletion lib/util/assertString.js
Expand Up @@ -3,12 +3,26 @@
Object.defineProperty(exports, "__esModule", {
value: true
});

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

exports.default = assertString;
function assertString(input) {
var isString = typeof input === 'string' || input instanceof String;

if (!isString) {
throw new TypeError('This library (validator.js) validates strings only');
var invalidType = void 0;
if (input === null) {
invalidType = 'null';
} else {
invalidType = typeof input === 'undefined' ? 'undefined' : _typeof(input);
if (invalidType === 'object' && input.constructor && input.constructor.hasOwnProperty('name')) {
invalidType = input.constructor.name;
} else {
invalidType = 'a ' + invalidType;
}
}
throw new TypeError('Expected string but received ' + invalidType + '.');
}
}
module.exports = exports['default'];
13 changes: 12 additions & 1 deletion src/lib/util/assertString.js
Expand Up @@ -2,6 +2,17 @@ export default function assertString(input) {
const isString = (typeof input === 'string' || input instanceof String);

if (!isString) {
throw new TypeError('This library (validator.js) validates strings only');
let invalidType;
if (input === null) {
invalidType = 'null';
} else {
invalidType = typeof input;
if (invalidType === 'object' && input.constructor && input.constructor.hasOwnProperty('name')) {
invalidType = input.constructor.name;
} else {
invalidType = `a ${invalidType}`;
}
}
throw new TypeError(`Expected string but received ${invalidType}.`);
}
}
25 changes: 18 additions & 7 deletions validator.js
Expand Up @@ -26,11 +26,28 @@
(global.validator = factory());
}(this, (function () { 'use strict';

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};

function assertString(input) {
var isString = typeof input === 'string' || input instanceof String;

if (!isString) {
throw new TypeError('This library (validator.js) validates strings only');
var invalidType = void 0;
if (input === null) {
invalidType = 'null';
} else {
invalidType = typeof input === 'undefined' ? 'undefined' : _typeof(input);
if (invalidType === 'object' && input.constructor && input.constructor.hasOwnProperty('name')) {
invalidType = input.constructor.name;
} else {
invalidType = 'a ' + invalidType;
}
}
throw new TypeError('Expected string but received ' + invalidType + '.');
}
}

Expand Down Expand Up @@ -63,12 +80,6 @@ function equals(str, comparison) {
return str === comparison;
}

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};

function toString(input) {
if ((typeof input === 'undefined' ? 'undefined' : _typeof(input)) === 'object' && input !== null) {
if (typeof input.toString === 'function') {
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit 3fe78c1

Please sign in to comment.