UNPKG

@kubb/plugin-zod

Version:

Zod schema generator plugin for Kubb, creating type-safe validation schemas from OpenAPI specifications for runtime data validation.

35 lines (31 loc) 1.42 kB
import { z } from 'zod'; /** * See https://github.com/colinhacks/tozod/blob/master/src/index.ts * Adapted based on https://github.com/colinhacks/zod/issues/372 */ type isAny<T> = [any extends T ? 'true' : 'false'] extends ['true'] ? true : false; type nonoptional<T> = T extends undefined ? never : T; type nonnullable<T> = T extends null ? never : T; type equals<X, Y> = [X] extends [Y] ? ([Y] extends [X] ? true : false) : false; type zodKey<T> = isAny<T> extends true ? 'any' : equals<T, boolean> extends true ? 'boolean' : [undefined] extends [T] ? 'optional' : [null] extends [T] ? 'nullable' : T extends any[] ? 'array' : equals<T, string> extends true ? 'string' : equals<T, bigint> extends true ? 'bigint' : equals<T, number> extends true ? 'number' : equals<T, Date> extends true ? 'date' : T extends { [k: string]: any; } ? 'object' : 'rest'; type ToZod<T> = { any: z.ZodAny; optional: z.ZodOptional<ToZod<nonoptional<T>>>; nullable: z.ZodNullable<ToZod<nonnullable<T>>>; array: T extends Array<infer U> ? z.ZodArray<ToZod<U>> : never; string: z.ZodString; bigint: z.ZodBigInt; number: z.ZodNumber; boolean: z.ZodBoolean; date: z.ZodDate; object: z.ZodObject<{ [K in keyof T]: T[K]; }, 'passthrough', unknown, T>; rest: z.ZodType<T>; }[zodKey<T>]; type ZodShape<T> = { [key in keyof T]-?: ToZod<T[key]>; }; export type { ToZod, ZodShape };