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

Additional UnknownRecord.is check for undefined added #664

Merged
merged 1 commit into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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