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 4 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
18 changes: 17 additions & 1 deletion lib/util/assertString.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,28 @@
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;
}
}
var message = 'This library (validator.js) validates strings only, ';
message += 'instead it received ' + invalidType + '.';
throw new TypeError(message);
}
}
module.exports = exports['default'];
15 changes: 14 additions & 1 deletion src/lib/util/assertString.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ 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}`;
}
}
let message = 'This library (validator.js) validates strings only, ';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the message just be this, to make it less verbose:

`Expected string but received ${invalidType}`

message += `instead it received ${invalidType}.`;
throw new TypeError(message);
}
}
27 changes: 20 additions & 7 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,30 @@
(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;
}
}
var message = 'This library (validator.js) validates strings only, ';
message += 'instead it received ' + invalidType + '.';
throw new TypeError(message);
}
}

Expand Down Expand Up @@ -63,12 +82,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.