Skip to content

Commit

Permalink
Object.prototype.toString removed from UnknownRecord.is
Browse files Browse the repository at this point in the history
  • Loading branch information
mlegenhausen authored and gcanti committed Oct 6, 2022
1 parent 7069ed0 commit 4edbeb3
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 8 deletions.
5 changes: 1 addition & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -992,10 +992,7 @@ export class AnyDictionaryType extends Type<{ [key: string]: unknown }> {
constructor() {
super(
'UnknownRecord',
(u): u is { [key: string]: unknown } => {
const s = Object.prototype.toString.call(u)
return s === '[object Object]' || s === '[object Window]'
},
(u): u is { [key: string]: unknown } => u !== null && typeof u === 'object' && !Array.isArray(u),
(u, c) => (this.is(u) ? success(u) : failure(u, c)),
identity
)
Expand Down
4 changes: 2 additions & 2 deletions test/2.1.x/default-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ describe('UnknownRecord', () => {
const T = t.UnknownRecord
assert.strictEqual(T.is({}), true)
assert.strictEqual(T.is({ a: 1 }), true)
assert.strictEqual(T.is(new Number()), true)
})

it('should return `false` for invalid objects', () => {
const T = t.UnknownRecord
assert.strictEqual(T.is(undefined), false)
assert.strictEqual(T.is(new Number()), false)
// #407
assert.strictEqual(T.is([]), false)
})
Expand All @@ -24,6 +24,7 @@ describe('UnknownRecord', () => {
const T = t.UnknownRecord
assertSuccess(T.decode({}))
assertSuccess(T.decode({ a: 1 }))
assertSuccess(T.decode(new Number()))
})

it('should fail validating an invalid value', () => {
Expand All @@ -33,7 +34,6 @@ describe('UnknownRecord', () => {
assertFailure(T, true, ['Invalid value true supplied to : UnknownRecord'])
assertFailure(T, null, ['Invalid value null supplied to : UnknownRecord'])
assertFailure(T, undefined, ['Invalid value undefined supplied to : UnknownRecord'])
assertFailure(T, new Number(), ['Invalid value 0 supplied to : UnknownRecord'])
// #407
assertFailure(T, [], ['Invalid value [] supplied to : UnknownRecord'])
})
Expand Down
4 changes: 2 additions & 2 deletions test/2.1.x/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('record', () => {
const T1 = t.record(t.string, t.number)
assert.strictEqual(T1.is({}), true)
assert.strictEqual(T1.is({ a: 1 }), true)
assert.strictEqual(T1.is(new Number()), true)

const T2 = t.record(t.string, NumberFromString)
assert.strictEqual(T2.is({}), true)
Expand All @@ -42,7 +43,6 @@ describe('record', () => {
const T1 = t.record(t.string, t.number)
assert.strictEqual(T1.is({ a: 'a' }), false)
assert.strictEqual(T1.is(null), false)
assert.strictEqual(T1.is(new Number()), false)
// #407
assert.strictEqual(T1.is([]), false)

Expand Down Expand Up @@ -73,6 +73,7 @@ describe('record', () => {
const T = t.record(t.string, t.number)
assertSuccess(T.decode({}))
assertSuccess(T.decode({ a: 1 }))
assertSuccess(T.decode(new Number()))
})

it('should return the same reference while decoding isomorphic values', () => {
Expand Down Expand Up @@ -106,7 +107,6 @@ describe('record', () => {
const T1 = t.record(t.string, t.number)
assertFailure(T1, 1, ['Invalid value 1 supplied to : { [K in string]: number }'])
assertFailure(T1, { aa: 's' }, ['Invalid value "s" supplied to : { [K in string]: number }/aa: number'])
assertFailure(T1, new Number(), ['Invalid value 0 supplied to : { [K in string]: number }'])
// #407
assertFailure(T1, [], ['Invalid value [] supplied to : { [K in string]: number }'])
// #407
Expand Down

0 comments on commit 4edbeb3

Please sign in to comment.