Skip to content

Commit a57f3c8

Browse files
committed
Object.toString() is [object Object] not ''
1 parent 995cf7a commit a57f3c8

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

test/validators.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -2455,11 +2455,16 @@ describe('Validators', function () {
24552455
});
24562456

24572457
it('should convert non-string input', function () {
2458-
var empty = [undefined, null, [], NaN, Object.create(null)];
2458+
var empty = [undefined, null, [], NaN];
24592459
empty.forEach(function (item) {
24602460
assert.equal(validator.toString(item), '');
24612461
assert(validator.isNull(item));
24622462
});
2463+
2464+
var objects = [{}, Object.create(null)];
2465+
objects.forEach(function (item) {
2466+
assert.equal(validator.toString(item), '[object Object]');
2467+
});
24632468
});
24642469

24652470
});

validator.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@
127127
};
128128

129129
validator.toString = function (input) {
130-
if (typeof input === 'object') {
131-
if (input !== null && typeof input.toString === 'function') {
130+
if (typeof input === 'object' && input !== null) {
131+
if (typeof input.toString === 'function') {
132132
input = input.toString();
133133
} else {
134-
input = '';
134+
input = '[object Object]';
135135
}
136-
} else if (typeof input === 'undefined' || (isNaN(input) && !input.length)) {
136+
} else if (input === null || typeof input === 'undefined' || (isNaN(input) && !input.length)) {
137137
input = '';
138138
}
139139
return '' + input;

0 commit comments

Comments
 (0)