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

Have assertString.js provide the invalid type passed to validator. #895

Merged
merged 7 commits into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from 6 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
16 changes: 15 additions & 1 deletion lib/util/assertString.js
Original file line number Diff line number Diff line change
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; };
profnandaa marked this conversation as resolved.
Show resolved Hide resolved

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.