From 2813cc5666832bac917a51fccbe6c75381c35b0e Mon Sep 17 00:00:00 2001 From: gcanti Date: Tue, 31 Mar 2020 16:32:59 +0200 Subject: [PATCH] fix regression in v2.1.2 --- CHANGELOG.md | 2 +- package.json | 2 +- src/index.ts | 3 ++- test/type.ts | 6 ++++++ 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4806bfc51..d520e5890 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ **Note**: Gaps between patch versions are faulty/broken releases. **Note**: A feature tagged as Experimental is in a high state of flux, you're at risk of it changing without notice. -# 2.1.2 +# 2.1.3 - **Polish** - remove useless `hasOwnProperty` calls, closes #423 (@gcanti) diff --git a/package.json b/package.json index 89b8b3966..6860f3857 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "io-ts", - "version": "2.1.2", + "version": "2.1.3", "description": "TypeScript compatible runtime type system for IO validation", "files": [ "lib", diff --git a/src/index.ts b/src/index.ts index 2f17b9e80..875709457 100644 --- a/src/index.ts +++ b/src/index.ts @@ -798,7 +798,8 @@ export const type =

(props: P, name: string = getInterfaceTypeN if (UnknownRecord.is(u)) { for (let i = 0; i < len; i++) { const k = keys[i] - if (!types[i].is(u[k])) { + const uk = u[k] + if ((uk === undefined && !hasOwnProperty.call(u, k)) || !types[i].is(uk)) { return false } } diff --git a/test/type.ts b/test/type.ts index 3666391f5..fe06b5b8a 100644 --- a/test/type.ts +++ b/test/type.ts @@ -31,6 +31,12 @@ describe('type', () => { assert.strictEqual(T.is([]), false) }) + // #434 + it('should return `false` on missing fields', () => { + const T = t.type({ a: t.unknown }) + assert.strictEqual(T.is({}), false) + }) + it('should allow additional properties', () => { const T = t.type({ a: t.string }) assert.strictEqual(T.is({ a: 'a', b: 1 }), true)