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

Loosen the is check for optionalized #737

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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