Skip to content

Commit

Permalink
Merge pull request #737 from bitgopatmcl/looser-exact-optional-check
Browse files Browse the repository at this point in the history
Loosen the `is` check for `optionalized`
  • Loading branch information
bitgopatmcl authored Apr 12, 2024
2 parents 9308b94 + 416bca2 commit 4d3e641
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('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 4d3e641

Please sign in to comment.