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

feat: Support zod nativeEnum #84

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ The following are supported
- Effects
- Enum
- Literal
- NativeEnum
- Nullable
- Null
- Nullish
Expand Down
5 changes: 5 additions & 0 deletions packages/dev-app/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ export const appRouter = createTRPCRouter({
.query(() => {
return "It's an input";
}),
nativeEnumInput: procedure
.input(z.object({ aEnumInput: z.nativeEnum({ONE: "one", TWO: "two"}) }))
.query(() => {
return "It's an input";
}),
stringArrayInput: procedure
.input(z.object({ aStringArray: z.string().array() }))
.query(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { defaultReferences } from "@src/parse/input-mappers/defaultReferences";
import { parseZodNativeEnumDef } from "@src/parse/input-mappers/zod/parsers/parseZodNativeEnumDef";
import { EnumNode } from "@src/parse/parseNodeTypes";
import { z } from "zod";

describe("Parse ZodNativeEnum", () => {
it("should parse a zod native enum", () => {
const expected: EnumNode = {
type: "enum",
enumValues: ["one", "two", "three"],
path: [],
};

enum ExampleEnum {
ONE = "one",
TWO = "two",
THREE = "three",
};

const parsed = parseZodNativeEnumDef(
z.nativeEnum(ExampleEnum)._def,
defaultReferences()
);
expect(expected).toStrictEqual(parsed);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { nodePropertiesFromRef } from "@src/parse/utils";
import { ZodNativeEnumDef } from "zod";
import { EnumNode, ParseFunction } from "../../../parseNodeTypes";

export const parseZodNativeEnumDef: ParseFunction<ZodNativeEnumDef, EnumNode> = (
def,
refs
) => {
const values = Object.values(def.values) as string[];
refs.addDataFunctions.addDescriptionIfExists(def, refs);
return { type: "enum", enumValues: values, ...nodePropertiesFromRef(refs) };
};
4 changes: 4 additions & 0 deletions packages/trpc-panel/src/parse/input-mappers/zod/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ZodDefaultDef,
ZodEffectsDef,
ZodEnumDef,
ZodNativeEnumDef,
ZodFirstPartyTypeKind,
ZodLiteralDef,
ZodNullableDef,
Expand All @@ -28,6 +29,7 @@ import {
ZodDiscriminatedUnionDefUnversioned,
} from "./parsers/parseZodDiscriminatedUnionDef";
import { parseZodEnumDef } from "./parsers/parseZodEnumDef";
import { parseZodNativeEnumDef } from "./parsers/parseZodNativeEnumDef";
import { parseZodLiteralDef } from "./parsers/parseZodLiteralDef";
import { parseZodNumberDef } from "./parsers/parseZodNumberDef";
import { parseZodObjectDef } from "./parsers/parseZodObjectDef";
Expand Down Expand Up @@ -63,6 +65,8 @@ export const zodSelectorFunction: ParserSelectorFunction<ZodDefWithType> = (
);
case ZodFirstPartyTypeKind.ZodEnum:
return parseZodEnumDef(def as ZodEnumDef, references);
case ZodFirstPartyTypeKind.ZodNativeEnum:
return parseZodNativeEnumDef(def as ZodNativeEnumDef, references);
case ZodFirstPartyTypeKind.ZodLiteral:
return parseZodLiteralDef(def as ZodLiteralDef, references);
case ZodFirstPartyTypeKind.ZodNumber:
Expand Down