Skip to content

Commit dc25290

Browse files
committed
remove useless hasOwnProperty calls, closes gcanti#423
1 parent c8a6c15 commit dc25290

File tree

5 files changed

+60
-9
lines changed

5 files changed

+60
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
**Note**: Gaps between patch versions are faulty/broken releases. **Note**: A feature tagged as Experimental is in a
1515
high state of flux, you're at risk of it changing without notice.
1616

17+
# 2.1.2
18+
19+
- **Polish**
20+
- remove useless `hasOwnProperty` calls, closes #423 (@gcanti)
21+
1722
# 2.1.1
1823

1924
- **Bug Fix**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "io-ts",
3-
"version": "2.1.1",
3+
"version": "2.1.2",
44
"description": "TypeScript compatible runtime type system for IO validation",
55
"files": [
66
"lib",

src/index.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ export const type = <P extends Props>(props: P, name: string = getInterfaceTypeN
798798
if (UnknownRecord.is(u)) {
799799
for (let i = 0; i < len; i++) {
800800
const k = keys[i]
801-
if (!hasOwnProperty.call(u, k) || !types[i].is(u[k])) {
801+
if (!types[i].is(u[k])) {
802802
return false
803803
}
804804
}
@@ -812,20 +812,14 @@ export const type = <P extends Props>(props: P, name: string = getInterfaceTypeN
812812
const errors: Errors = []
813813
for (let i = 0; i < len; i++) {
814814
const k = keys[i]
815-
if (!hasOwnProperty.call(a, k)) {
816-
if (a === o) {
817-
a = { ...o }
818-
}
819-
a[k] = a[k]
820-
}
821815
const ak = a[k]
822816
const type = types[i]
823817
const result = type.validate(ak, appendContext(c, k, type, ak))
824818
if (isLeft(result)) {
825819
pushAll(errors, result.left)
826820
} else {
827821
const vak = result.right
828-
if (vak !== ak) {
822+
if (vak !== ak || (vak === undefined && !hasOwnProperty.call(a, k))) {
829823
/* istanbul ignore next */
830824
if (a === o) {
831825
a = { ...o }

test/strict.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ describe('strict', () => {
2929
assert.strictEqual(T.is({ a: 1, b: 1 }), true)
3030
assert.strictEqual(T.is(undefined), false)
3131
})
32+
33+
it('#423', () => {
34+
class A {
35+
get a() {
36+
return 'a'
37+
}
38+
get b() {
39+
return 'b'
40+
}
41+
}
42+
const T = t.strict({ a: t.string, b: t.string })
43+
assert.strictEqual(T.is(new A()), true)
44+
})
3245
})
3346

3447
describe('decode', () => {
@@ -57,6 +70,19 @@ describe('strict', () => {
5770
const T = t.strict({ foo: t.string })
5871
assertSuccess(T.decode({ foo: 'foo', bar: 1, baz: true }), { foo: 'foo' })
5972
})
73+
74+
it('#423', () => {
75+
class A {
76+
get a() {
77+
return 'a'
78+
}
79+
get b() {
80+
return 'b'
81+
}
82+
}
83+
const T = t.strict({ a: t.string, b: t.string })
84+
assertSuccess(T.decode(new A()))
85+
})
6086
})
6187

6288
describe('encode', () => {

test/type.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ describe('type', () => {
3535
const T = t.type({ a: t.string })
3636
assert.strictEqual(T.is({ a: 'a', b: 1 }), true)
3737
})
38+
39+
it('#423', () => {
40+
class A {
41+
get a() {
42+
return 'a'
43+
}
44+
get b() {
45+
return 'b'
46+
}
47+
}
48+
const T = t.type({ a: t.string, b: t.string })
49+
assert.strictEqual(T.is(new A()), true)
50+
})
3851
})
3952

4053
describe('decode', () => {
@@ -75,6 +88,19 @@ describe('type', () => {
7588
const T = t.interface({ a: t.string })
7689
assertSuccess(T.decode({ a: 'a' }))
7790
})
91+
92+
it('#423', () => {
93+
class A {
94+
get a() {
95+
return 'a'
96+
}
97+
get b() {
98+
return 'b'
99+
}
100+
}
101+
const T = t.type({ a: t.string, b: t.string })
102+
assertSuccess(T.decode(new A()))
103+
})
78104
})
79105

80106
describe('encode', () => {

0 commit comments

Comments
 (0)