Skip to content

Commit

Permalink
fix: Fixes #36 | Allows for single value unions (#37)
Browse files Browse the repository at this point in the history
* Checks in failing test documenting behavior

* Fixes test by checking for single value unions.
  • Loading branch information
anglinb authored and fabien0102 committed Jul 29, 2021
1 parent d6ae0a1 commit 57a38b2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/core/generateZodSchema.test.ts
Expand Up @@ -168,6 +168,13 @@ describe("generateZodSchema", () => {
);
});

it("should generate a literal schema for a single union", () => {
const source = `export type Identity = | "superman";`;
expect(generate(source)).toMatchInlineSnapshot(
`"export const identitySchema = z.literal(\\"superman\\");"`
);
});

it("should generate two joined schemas", () => {
const source = `export type SupermanWithWeakness = Superman & { weakness: Kryptonite };`;
expect(generate(source)).toMatchInlineSnapshot(`
Expand Down
5 changes: 5 additions & 0 deletions src/core/generateZodSchema.ts
Expand Up @@ -394,6 +394,11 @@ function buildZodPrimitive({
getDependencyName,
})
);
// type A = | 'b' is a valid typescript definintion
// Zod does not allow `z.union(['b']), so we have to return just the value
if (values.length === 1) {
return values[0];
}
return buildZodSchema(
z,
"union",
Expand Down

0 comments on commit 57a38b2

Please sign in to comment.