zod-compat.d.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import type * as z3 from 'zod/v3';
  2. import type * as z4 from 'zod/v4/core';
  3. export type AnySchema = z3.ZodTypeAny | z4.$ZodType;
  4. export type AnyObjectSchema = z3.AnyZodObject | z4.$ZodObject | AnySchema;
  5. export type ZodRawShapeCompat = Record<string, AnySchema>;
  6. export interface ZodV3Internal {
  7. _def?: {
  8. typeName?: string;
  9. value?: unknown;
  10. values?: unknown[];
  11. shape?: Record<string, AnySchema> | (() => Record<string, AnySchema>);
  12. description?: string;
  13. };
  14. shape?: Record<string, AnySchema> | (() => Record<string, AnySchema>);
  15. value?: unknown;
  16. }
  17. export interface ZodV4Internal {
  18. _zod?: {
  19. def?: {
  20. type?: string;
  21. value?: unknown;
  22. values?: unknown[];
  23. shape?: Record<string, AnySchema> | (() => Record<string, AnySchema>);
  24. };
  25. };
  26. value?: unknown;
  27. }
  28. export type SchemaOutput<S> = S extends z3.ZodTypeAny ? z3.infer<S> : S extends z4.$ZodType ? z4.output<S> : never;
  29. export type SchemaInput<S> = S extends z3.ZodTypeAny ? z3.input<S> : S extends z4.$ZodType ? z4.input<S> : never;
  30. /**
  31. * Infers the output type from a ZodRawShapeCompat (raw shape object).
  32. * Maps over each key in the shape and infers the output type from each schema.
  33. */
  34. export type ShapeOutput<Shape extends ZodRawShapeCompat> = {
  35. [K in keyof Shape]: SchemaOutput<Shape[K]>;
  36. };
  37. export declare function isZ4Schema(s: AnySchema): s is z4.$ZodType;
  38. export declare function objectFromShape(shape: ZodRawShapeCompat): AnyObjectSchema;
  39. export declare function safeParse<S extends AnySchema>(schema: S, data: unknown): {
  40. success: true;
  41. data: SchemaOutput<S>;
  42. } | {
  43. success: false;
  44. error: unknown;
  45. };
  46. export declare function safeParseAsync<S extends AnySchema>(schema: S, data: unknown): Promise<{
  47. success: true;
  48. data: SchemaOutput<S>;
  49. } | {
  50. success: false;
  51. error: unknown;
  52. }>;
  53. export declare function getObjectShape(schema: AnyObjectSchema | undefined): Record<string, AnySchema> | undefined;
  54. /**
  55. * Normalizes a schema to an object schema. Handles both:
  56. * - Already-constructed object schemas (v3 or v4)
  57. * - Raw shapes that need to be wrapped into object schemas
  58. */
  59. export declare function normalizeObjectSchema(schema: AnySchema | ZodRawShapeCompat | undefined): AnyObjectSchema | undefined;
  60. /**
  61. * Safely extracts an error message from a parse result error.
  62. * Zod errors can have different structures, so we handle various cases.
  63. */
  64. export declare function getParseErrorMessage(error: unknown): string;
  65. /**
  66. * Gets the description from a schema, if available.
  67. * Works with both Zod v3 and v4.
  68. *
  69. * Both versions expose a `.description` getter that returns the description
  70. * from their respective internal storage (v3: _def, v4: globalRegistry).
  71. */
  72. export declare function getSchemaDescription(schema: AnySchema): string | undefined;
  73. /**
  74. * Checks if a schema is optional.
  75. * Works with both Zod v3 and v4.
  76. */
  77. export declare function isSchemaOptional(schema: AnySchema): boolean;
  78. /**
  79. * Gets the literal value from a schema, if it's a literal schema.
  80. * Works with both Zod v3 and v4.
  81. * Returns undefined if the schema is not a literal or the value cannot be determined.
  82. */
  83. export declare function getLiteralValue(schema: AnySchema): unknown;
  84. //# sourceMappingURL=zod-compat.d.ts.map