Skip to content

Commit

Permalink
Pcc/support native enum type (#8)
Browse files Browse the repository at this point in the history
Co-authored-by: pcc <[email protected]>
Co-authored-by: Pierre Cardona <[email protected]>
  • Loading branch information
3 people authored Nov 8, 2024
1 parent c534e39 commit 4ea2416
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,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 @@ -29,6 +30,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 @@ -65,6 +67,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

0 comments on commit 4ea2416

Please sign in to comment.