Skip to content

Commit 4ea2416

Browse files
Pcc/support native enum type (#8)
Co-authored-by: pcc <[email protected]> Co-authored-by: Pierre Cardona <[email protected]>
1 parent c534e39 commit 4ea2416

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

README.MD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ The following are supported
169169
- Effects
170170
- Enum
171171
- Literal
172+
- NativeEnum
172173
- Nullable
173174
- Null
174175
- Nullish

packages/dev-app/src/router.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ export const appRouter = createTRPCRouter({
5151
.query(() => {
5252
return "It's an input";
5353
}),
54+
nativeEnumInput: procedure
55+
.input(z.object({ aEnumInput: z.nativeEnum({ONE: "one", TWO: "two"}) }))
56+
.query(() => {
57+
return "It's an input";
58+
}),
5459
stringArrayInput: procedure
5560
.input(z.object({ aStringArray: z.string().array() }))
5661
.query(() => {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { defaultReferences } from "@src/parse/input-mappers/defaultReferences";
2+
import { parseZodNativeEnumDef } from "@src/parse/input-mappers/zod/parsers/parseZodNativeEnumDef";
3+
import { EnumNode } from "@src/parse/parseNodeTypes";
4+
import { z } from "zod";
5+
6+
describe("Parse ZodNativeEnum", () => {
7+
it("should parse a zod native enum", () => {
8+
const expected: EnumNode = {
9+
type: "enum",
10+
enumValues: ["one", "two", "three"],
11+
path: [],
12+
};
13+
14+
enum ExampleEnum {
15+
ONE = "one",
16+
TWO = "two",
17+
THREE = "three",
18+
};
19+
20+
const parsed = parseZodNativeEnumDef(
21+
z.nativeEnum(ExampleEnum)._def,
22+
defaultReferences()
23+
);
24+
expect(expected).toStrictEqual(parsed);
25+
});
26+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { nodePropertiesFromRef } from "@src/parse/utils";
2+
import { ZodNativeEnumDef } from "zod";
3+
import { EnumNode, ParseFunction } from "../../../parseNodeTypes";
4+
5+
export const parseZodNativeEnumDef: ParseFunction<ZodNativeEnumDef, EnumNode> = (
6+
def,
7+
refs
8+
) => {
9+
const values = Object.values(def.values) as string[];
10+
refs.addDataFunctions.addDescriptionIfExists(def, refs);
11+
return { type: "enum", enumValues: values, ...nodePropertiesFromRef(refs) };
12+
};

packages/trpc-panel/src/parse/input-mappers/zod/selector.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
ZodDefaultDef,
77
ZodEffectsDef,
88
ZodEnumDef,
9+
ZodNativeEnumDef,
910
ZodFirstPartyTypeKind,
1011
ZodLiteralDef,
1112
ZodNullableDef,
@@ -29,6 +30,7 @@ import {
2930
ZodDiscriminatedUnionDefUnversioned,
3031
} from "./parsers/parseZodDiscriminatedUnionDef";
3132
import { parseZodEnumDef } from "./parsers/parseZodEnumDef";
33+
import { parseZodNativeEnumDef } from "./parsers/parseZodNativeEnumDef";
3234
import { parseZodLiteralDef } from "./parsers/parseZodLiteralDef";
3335
import { parseZodNumberDef } from "./parsers/parseZodNumberDef";
3436
import { parseZodObjectDef } from "./parsers/parseZodObjectDef";
@@ -65,6 +67,8 @@ export const zodSelectorFunction: ParserSelectorFunction<ZodDefWithType> = (
6567
);
6668
case ZodFirstPartyTypeKind.ZodEnum:
6769
return parseZodEnumDef(def as ZodEnumDef, references);
70+
case ZodFirstPartyTypeKind.ZodNativeEnum:
71+
return parseZodNativeEnumDef(def as ZodNativeEnumDef, references);
6872
case ZodFirstPartyTypeKind.ZodLiteral:
6973
return parseZodLiteralDef(def as ZodLiteralDef, references);
7074
case ZodFirstPartyTypeKind.ZodNumber:

0 commit comments

Comments
 (0)