diff --git a/packages/io-ts-http/src/combinators.ts b/packages/io-ts-http/src/combinators.ts index 4c67aa28..180fb927 100644 --- a/packages/io-ts-http/src/combinators.ts +++ b/packages/io-ts-http/src/combinators.ts @@ -26,8 +26,7 @@ const partialWithoutUndefined =

( const partialCodec = t.partial(props, name); return new t.PartialType( partialCodec.name, - (i): i is DefinedValues> => - partialCodec.is(i) && !Object.values(i).includes(void 0), + (i): i is DefinedValues> => partialCodec.is(i), (i, ctx) => { return pipe( partialCodec.validate(i, ctx), diff --git a/packages/io-ts-http/test/combinators.test.ts b/packages/io-ts-http/test/combinators.test.ts index af67f106..d29a62b1 100644 --- a/packages/io-ts-http/test/combinators.test.ts +++ b/packages/io-ts-http/test/combinators.test.ts @@ -104,6 +104,14 @@ describe('optionalized', () => { assertDecodes(optionalCodec, { a: undefined, b: 'foo' }, expected); }); + it('returns `true` for `is` when there actually is an explicit undefined', () => { + const optionalCodec = c.optionalized({ + a: c.optional(t.number), + b: t.string, + }); + assert(optionalCodec.is({ a: undefined, b: 'foo' })); + }); + it('strips explicit undefined properties when encoding', () => { const optionalCodec = c.optionalized({ a: c.optional(t.number), @@ -113,6 +121,18 @@ describe('optionalized', () => { assertEncodes(optionalCodec, { a: undefined, b: 'foo' }, expected); }); + it.only('successfully encodes when in a union and passed explicitly undefined properties', () => { + const unionCodec = t.union([ + c.optionalized({ + a: c.optional(t.number), + key: t.literal('foo'), + }), + t.undefined, + ]); + const expected = { key: 'foo' }; + assertEncodes(unionCodec, { a: undefined, key: 'foo' }, expected); + }); + it('returns undefined when encoding undefined', () => { const optionalCodec = c.optionalized({}); const expected = undefined;