forked from iway1/trpc-panel
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support ZodUnions of ZodLiterals (#7)
Co-authored-by: Will Ruggiano <wmruggiano@gmail.com>
- Loading branch information
1 parent
d0dd78f
commit c534e39
Showing
7 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/trpc-panel/src/parse/input-mappers/__tests__/zod/union.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { defaultReferences } from "@src/parse/input-mappers/defaultReferences"; | ||
import { parseZodUnionDef } from "@src/parse/input-mappers/zod/parsers/parseZodUnionDef"; | ||
import { UnionNode } from "@src/parse/parseNodeTypes"; | ||
import { z } from "zod"; | ||
|
||
describe("Parse Zod Union", () => { | ||
it("should parse a union node", () => { | ||
const expected: UnionNode = { | ||
type: "union", | ||
path: [], | ||
values: [ | ||
{ | ||
type: "literal", | ||
value: "one", | ||
path: [], | ||
}, | ||
{ | ||
type: "literal", | ||
value: 2, | ||
path: [], | ||
}, | ||
], | ||
}; | ||
const zodSchema = z.union([z.literal("one"), z.literal(2)]); | ||
const parsedZod = parseZodUnionDef(zodSchema._def, defaultReferences()); | ||
expect(parsedZod).toStrictEqual(expected); | ||
}); | ||
}); |
18 changes: 18 additions & 0 deletions
18
packages/trpc-panel/src/parse/input-mappers/zod/parsers/parseZodUnionDef.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { nodePropertiesFromRef } from "@src/parse/utils"; | ||
import { ZodUnionDef } from "zod"; | ||
import { UnionNode, ParseFunction, LiteralNode } from "../../../parseNodeTypes"; | ||
import { zodSelectorFunction } from "../selector"; | ||
|
||
export const parseZodUnionDef: ParseFunction<ZodUnionDef, UnionNode> = ( | ||
def, | ||
refs | ||
) => { | ||
refs.addDataFunctions.addDescriptionIfExists(def, refs); | ||
return { | ||
type: "union", | ||
values: def.options.map( | ||
(o) => zodSelectorFunction(o._def, { ...refs, path: [] }) as LiteralNode | ||
), | ||
...nodePropertiesFromRef(refs), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/trpc-panel/src/react-app/components/form/fields/UnionField.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from "react"; | ||
import { Control, useController } from "react-hook-form"; | ||
import type { ParsedInputNode } from "@src/parse/parseNodeTypes"; | ||
import { BaseSelectField } from "./base/BaseSelectField"; | ||
|
||
export function UnionField({ | ||
name, | ||
label, | ||
control, | ||
node, | ||
}: { | ||
name: string; | ||
label: string; | ||
control: Control<any>; | ||
node: ParsedInputNode & { type: "union" }; | ||
}) { | ||
const { field, fieldState } = useController({ | ||
name, | ||
control, | ||
}); | ||
|
||
return ( | ||
<BaseSelectField | ||
options={node.values.map((n) => n.value as string)} | ||
value={field.value} | ||
onChange={field.onChange} | ||
errorMessage={fieldState.error?.message} | ||
label={label} | ||
/> | ||
); | ||
} |