1 import { z } from "zod"
2 import { FormFieldType } from "./types"
3
4 export function getFieldLabel(name: string, label?: string) {
5 if (label) {
6 return label
7 }
8
9 return name
10 .split("_")
11 .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
12 .join(" ")
13 }
14
15 export function getFieldType(type: z.ZodType): FormFieldType {
16 if (type instanceof z.ZodString) {
17 return "text"
18 }
19
20 if (type instanceof z.ZodNumber) {
21 return "number"
22 }
23
24 if (type instanceof z.ZodBoolean) {
25 return "boolean"
26 }
27
28 if (type instanceof z.ZodNullable) {
29 const innerType = type.unwrap() as z.ZodType
30 return getFieldType(innerType)
31 }
32
33 if (type instanceof z.ZodOptional) {
34 const innerType = type.unwrap() as z.ZodType
35 return getFieldType(innerType)
36 }
37
38 if (type instanceof z.ZodPipe) {
39 const innerType = type.def.in as z.ZodType
40 return getFieldType(innerType)
41 }
42
43 return "unsupported"
44 }
45
46 export function getIsFieldOptional(type: z.ZodType) {
47 return (
48 type instanceof z.ZodOptional ||
49 type instanceof z.ZodNull ||
50 type instanceof z.ZodUndefined ||
51 type instanceof z.ZodNullable
52 )
53 }