|
| 1 | +import { ZodBigInt, ZodBoolean, ZodEnum, ZodNull, ZodNullable, ZodNumber, ZodObject, ZodOptional, ZodString, type ZodType, type ZodTypeDef, ZodUndefined } from "zod"; |
| 2 | + |
| 3 | +export default function generateExample<I, O>( |
| 4 | + schema: ZodType<O, ZodTypeDef, I>, |
| 5 | + ignoreOptionals: boolean, |
| 6 | + keyName?: string, |
| 7 | + nullable: boolean = false, |
| 8 | +): O { |
| 9 | + if ("defaultValue" in schema._def && typeof schema._def.defaultValue === "function") { |
| 10 | + return schema._def.defaultValue() as unknown as O; |
| 11 | + } |
| 12 | + if (schema instanceof ZodOptional) { |
| 13 | + const innerSchema = schema._def.innerType; |
| 14 | + if (ignoreOptionals) { |
| 15 | + return undefined as unknown as O; |
| 16 | + } |
| 17 | + return generateExample(innerSchema, ignoreOptionals, keyName); |
| 18 | + } |
| 19 | + if (schema instanceof ZodNullable) { |
| 20 | + const innerSchema = schema._def.innerType; |
| 21 | + return (generateExample(innerSchema, ignoreOptionals, keyName, true) ?? null) as unknown as O; |
| 22 | + } |
| 23 | + if (schema instanceof ZodObject) { |
| 24 | + const entries = Object.entries(schema.shape).map(([key, childSchema]) => { |
| 25 | + return [key, generateExample(childSchema as ZodType, ignoreOptionals, key)] as const; |
| 26 | + }).filter(([_, value]) => typeof value !== "undefined"); |
| 27 | + return Object.fromEntries(entries) as unknown as O; |
| 28 | + } |
| 29 | + if (schema instanceof ZodEnum) { |
| 30 | + const values = schema._def.values; |
| 31 | + if (values.length) { |
| 32 | + return values[0] as unknown as O; |
| 33 | + } |
| 34 | + } |
| 35 | + if (schema instanceof ZodNumber) { |
| 36 | + if (schema.minValue) { |
| 37 | + return schema.minValue as unknown as O; |
| 38 | + } |
| 39 | + if (schema._def.checks.length) { |
| 40 | + for (const check of schema._def.checks) { |
| 41 | + if (check.kind === "max" && check.value < 1) { |
| 42 | + return -1 as unknown as O; |
| 43 | + } |
| 44 | + if (check.kind === "multipleOf") { |
| 45 | + return check.value as unknown as O; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + return 1 as unknown as O; |
| 50 | + } |
| 51 | + if (schema instanceof ZodBigInt) { |
| 52 | + return 1n as unknown as O; |
| 53 | + } |
| 54 | + if (schema instanceof ZodBoolean) { |
| 55 | + return false as unknown as O; |
| 56 | + } |
| 57 | + if (schema instanceof ZodUndefined) { |
| 58 | + return undefined as unknown as O; |
| 59 | + } |
| 60 | + if (schema instanceof ZodNull) { |
| 61 | + return null as unknown as O; |
| 62 | + } |
| 63 | + if (schema instanceof ZodString) { |
| 64 | + if (schema.isEmail) { |
| 65 | + return "[email protected]" as unknown as O; |
| 66 | + } |
| 67 | + if (schema.isURL) { |
| 68 | + return "https://example.com" as unknown as O; |
| 69 | + } |
| 70 | + if (schema.isEmoji) { |
| 71 | + return "😀" as unknown as O; |
| 72 | + } |
| 73 | + if (schema.isUUID) { |
| 74 | + return "aa35d4a1-8f23-4cee-847c-2bfe89dc87c1" as unknown as O; |
| 75 | + } |
| 76 | + if (schema.isIP) { |
| 77 | + return "192.168.1.1" as unknown as O; |
| 78 | + } |
| 79 | + if (schema.isDatetime) { |
| 80 | + return "2024-11-06T07:00:00.137Z" as unknown as O; |
| 81 | + } |
| 82 | + if (schema._def.checks.length) { |
| 83 | + const example = ["example"]; |
| 84 | + for (const check of schema._def.checks) { |
| 85 | + if (check.kind === "startsWith") { |
| 86 | + example.unshift(check.value); |
| 87 | + } |
| 88 | + if (check.kind === "endsWith") { |
| 89 | + example.push(check.value); |
| 90 | + } |
| 91 | + } |
| 92 | + return example.join("") as unknown as O; |
| 93 | + } |
| 94 | + if (keyName) { |
| 95 | + return `example ${keyName}` as unknown as O; |
| 96 | + } |
| 97 | + if (nullable) { |
| 98 | + return "example nullable string" as unknown as O; |
| 99 | + } |
| 100 | + return "example string" as unknown as O; |
| 101 | + } |
| 102 | + throw new Error("Unknown zod schema"); |
| 103 | +} |
0 commit comments