Skip to content

Commit

Permalink
fix: loosen the is check for optionalized
Browse files Browse the repository at this point in the history
BREAKING CHANGE: explicit undefined properties are now accepted when encoding
  • Loading branch information
bitgopatmcl committed Apr 11, 2024
1 parent 9308b94 commit 7534874
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
3 changes: 1 addition & 2 deletions packages/io-ts-http/src/combinators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ const partialWithoutUndefined = <P extends t.Props>(
const partialCodec = t.partial(props, name);
return new t.PartialType(
partialCodec.name,
(i): i is DefinedValues<t.TypeOfPartialProps<P>> =>
partialCodec.is(i) && !Object.values(i).includes(void 0),
(i): i is DefinedValues<t.TypeOfPartialProps<P>> => partialCodec.is(i),
(i, ctx) => {
return pipe(
partialCodec.validate(i, ctx),
Expand Down
20 changes: 20 additions & 0 deletions packages/io-ts-http/test/combinators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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;
Expand Down

0 comments on commit 7534874

Please sign in to comment.