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

Handle missing properties correctly #435

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,8 +861,7 @@ export const type = <P extends Props>(props: P, name: string = getInterfaceTypeN
if (UnknownRecord.is(u)) {
for (let i = 0; i < len; i++) {
const k = keys[i]
const uk = u[k]
if ((uk === undefined && !hasOwnProperty.call(u, k)) || !types[i].is(uk)) {
if (!(k in u) || !types[i].is(u[k])) {
return false
}
}
Expand All @@ -878,12 +877,15 @@ export const type = <P extends Props>(props: P, name: string = getInterfaceTypeN
const k = keys[i]
const ak = a[k]
const type = types[i]
const result = type.validate(ak, appendContext(c, k, type, ak))
const result =
ak === undefined && !(k in a)
? failure(ak, appendContext(c, k, type, ak))
: type.validate(ak, appendContext(c, k, type, ak))
if (isLeft(result)) {
pushAll(errors, result.left)
} else {
const vak = result.right
if (vak !== ak || (vak === undefined && !hasOwnProperty.call(a, k))) {
if (vak !== ak || (vak === undefined && !(k in a))) {
/* istanbul ignore next */
if (a === o) {
a = { ...o }
Expand Down
2 changes: 1 addition & 1 deletion test/exact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('exact', () => {

it('should succeed validating an undefined field', () => {
const T = t.exact(t.type({ foo: t.string, bar: t.union([t.string, t.undefined]) }))
assertSuccess(T.decode({ foo: 'foo' }))
assertSuccess(T.decode({ foo: 'foo', bar: undefined }))
})

it('should return the same reference if validation succeeded', () => {
Expand Down
12 changes: 10 additions & 2 deletions test/recursion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,19 @@ describe('recursion', () => {
assertStrictEqual(T.decode(value), value)
})

it('should fail validating an invalid value', () => {
it('should fail validating { a: number, b: ( self | undefined | null) } for value 1', () => {
assertFailure(T, 1, ['Invalid value 1 supplied to : T'])
assertFailure(T, {}, ['Invalid value undefined supplied to : T/a: number'])
})
it('should fail validating { a: number, b: ( self | undefined | null) } for value {}', () => {
assertFailure(T, {}, [
'Invalid value undefined supplied to : T/a: number',
'Invalid value undefined supplied to : T/b: (T | undefined | null)'
])
})
it('should fail validating { a: number, b: ( self | undefined | null) } for value { a: 1, b: {} }', () => {
assertFailure(T, { a: 1, b: {} }, [
'Invalid value undefined supplied to : T/b: (T | undefined | null)/0: T/a: number',
'Invalid value undefined supplied to : T/b: (T | undefined | null)/0: T/b: (T | undefined | null)',
'Invalid value {} supplied to : T/b: (T | undefined | null)/1: undefined',
'Invalid value {} supplied to : T/b: (T | undefined | null)/2: null'
])
Expand Down
44 changes: 42 additions & 2 deletions test/strict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('strict', () => {
assert.strictEqual(T.is(undefined), false)
})

it('#423', () => {
it('should allow properties to be satisified by getters - #423', () => {
class A {
get a() {
return 'a'
Expand All @@ -42,6 +42,26 @@ describe('strict', () => {
const T = t.strict({ a: t.string, b: t.string })
assert.strictEqual(T.is(new A()), true)
})

it('should return false for a missing undefined property ', () => {
const T = t.strict({ a: t.string, b: t.undefined })
assert.strictEqual(T.is({ a: 'a' }), false)
})

it('should return false for a missing unknown property ', () => {
const T = t.strict({ a: t.string, b: t.unknown })
assert.strictEqual(T.is({ a: 'a' }), false)
})

it('should return true for a missing undefined property that is optional', () => {
const T = t.intersection([t.type({ a: t.string }), t.partial({ b: t.undefined })])
assert.strictEqual(T.is({ a: 'a' }), true)
})

it('should return true for a missing unknown property that is optional', () => {
const T = t.intersection([t.type({ a: t.string }), t.partial({ b: t.unknown })])
assert.strictEqual(T.is({ a: 'a' }), true)
})
})

describe('decode', () => {
Expand All @@ -52,7 +72,7 @@ describe('strict', () => {

it('should succeed validating an undefined field', () => {
const T = t.strict({ foo: t.string, bar: t.union([t.string, t.undefined]) })
assertSuccess(T.decode({ foo: 'foo' }))
assertSuccess(T.decode({ foo: 'foo', bar: undefined }))
})

it('should return the same reference if validation succeeded', () => {
Expand All @@ -66,6 +86,26 @@ describe('strict', () => {
assertFailure(T, { foo: 1 }, ['Invalid value 1 supplied to : {| foo: string |}/foo: string'])
})

it('should fail validating a missing undefined value', () => {
const T = t.strict({ a: t.string, b: t.undefined })
assertFailure(T, { a: 'a' }, ['Invalid value undefined supplied to : {| a: string, b: undefined |}/b: undefined'])
})

it('should fail validating a missing unknown value', () => {
const T = t.strict({ a: t.string, b: t.unknown })
assertFailure(T, { a: 'a' }, ['Invalid value undefined supplied to : {| a: string, b: unknown |}/b: unknown'])
})

it('should succeed validating a missing undefined value that is optional', () => {
const T = t.intersection([t.type({ a: t.string }), t.partial({ b: t.unknown })])
assertSuccess(T.decode({ a: 'a' }))
})

it('should succeed validating a missing unknown value that is optional', () => {
const T = t.intersection([t.type({ a: t.string }), t.partial({ b: t.unknown })])
assertSuccess(T.decode({ a: 'a' }))
})

it('should strip additional properties', () => {
const T = t.strict({ foo: t.string })
assertSuccess(T.decode({ foo: 'foo', bar: 1, baz: true }), { foo: 'foo' })
Expand Down