to-json-schema.d.cts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import type * as core from "../core/index.cjs";
  2. import type * as JSONSchema from "./json-schema.cjs";
  3. import { type $ZodRegistry } from "./registries.cjs";
  4. import type * as schemas from "./schemas.cjs";
  5. import type { StandardJSONSchemaV1, StandardSchemaWithJSONProps } from "./standard-schema.cjs";
  6. export type Processor<T extends schemas.$ZodType = schemas.$ZodType> = (schema: T, ctx: ToJSONSchemaContext, json: JSONSchema.BaseSchema, params: ProcessParams) => void;
  7. export interface JSONSchemaGeneratorParams {
  8. processors: Record<string, Processor>;
  9. /** A registry used to look up metadata for each schema. Any schema with an `id` property will be extracted as a $def.
  10. * @default globalRegistry */
  11. metadata?: $ZodRegistry<Record<string, any>>;
  12. /** The JSON Schema version to target.
  13. * - `"draft-2020-12"` — Default. JSON Schema Draft 2020-12
  14. * - `"draft-07"` — JSON Schema Draft 7
  15. * - `"draft-04"` — JSON Schema Draft 4
  16. * - `"openapi-3.0"` — OpenAPI 3.0 Schema Object */
  17. target?: "draft-04" | "draft-07" | "draft-2020-12" | "openapi-3.0" | ({} & string) | undefined;
  18. /** How to handle unrepresentable types.
  19. * - `"throw"` — Default. Unrepresentable types throw an error
  20. * - `"any"` — Unrepresentable types become `{}` */
  21. unrepresentable?: "throw" | "any";
  22. /** Arbitrary custom logic that can be used to modify the generated JSON Schema. */
  23. override?: (ctx: {
  24. zodSchema: schemas.$ZodTypes;
  25. jsonSchema: JSONSchema.BaseSchema;
  26. path: (string | number)[];
  27. }) => void;
  28. /** Whether to extract the `"input"` or `"output"` type. Relevant to transforms, defaults, coerced primitives, etc.
  29. * - `"output"` — Default. Convert the output schema.
  30. * - `"input"` — Convert the input schema. */
  31. io?: "input" | "output";
  32. cycles?: "ref" | "throw";
  33. reused?: "ref" | "inline";
  34. external?: {
  35. registry: $ZodRegistry<{
  36. id?: string | undefined;
  37. }>;
  38. uri?: ((id: string) => string) | undefined;
  39. defs: Record<string, JSONSchema.BaseSchema>;
  40. } | undefined;
  41. }
  42. /**
  43. * Parameters for the toJSONSchema function.
  44. */
  45. export type ToJSONSchemaParams = Omit<JSONSchemaGeneratorParams, "processors" | "external">;
  46. /**
  47. * Parameters for the toJSONSchema function when passing a registry.
  48. */
  49. export interface RegistryToJSONSchemaParams extends ToJSONSchemaParams {
  50. uri?: (id: string) => string;
  51. }
  52. export interface ProcessParams {
  53. schemaPath: schemas.$ZodType[];
  54. path: (string | number)[];
  55. }
  56. export interface Seen {
  57. /** JSON Schema result for this Zod schema */
  58. schema: JSONSchema.BaseSchema;
  59. /** A cached version of the schema that doesn't get overwritten during ref resolution */
  60. def?: JSONSchema.BaseSchema;
  61. defId?: string | undefined;
  62. /** Number of times this schema was encountered during traversal */
  63. count: number;
  64. /** Cycle path */
  65. cycle?: (string | number)[] | undefined;
  66. isParent?: boolean | undefined;
  67. /** Schema to inherit JSON Schema properties from (set by processor for wrappers) */
  68. ref?: schemas.$ZodType | null;
  69. /** JSON Schema property path for this schema */
  70. path?: (string | number)[] | undefined;
  71. }
  72. export interface ToJSONSchemaContext {
  73. processors: Record<string, Processor>;
  74. metadataRegistry: $ZodRegistry<Record<string, any>>;
  75. target: "draft-04" | "draft-07" | "draft-2020-12" | "openapi-3.0" | ({} & string);
  76. unrepresentable: "throw" | "any";
  77. override: (ctx: {
  78. zodSchema: schemas.$ZodType;
  79. jsonSchema: JSONSchema.BaseSchema;
  80. path: (string | number)[];
  81. }) => void;
  82. io: "input" | "output";
  83. counter: number;
  84. seen: Map<schemas.$ZodType, Seen>;
  85. cycles: "ref" | "throw";
  86. reused: "ref" | "inline";
  87. external?: {
  88. registry: $ZodRegistry<{
  89. id?: string | undefined;
  90. }>;
  91. uri?: ((id: string) => string) | undefined;
  92. defs: Record<string, JSONSchema.BaseSchema>;
  93. } | undefined;
  94. }
  95. export declare function initializeContext(params: JSONSchemaGeneratorParams): ToJSONSchemaContext;
  96. export declare function process<T extends schemas.$ZodType>(schema: T, ctx: ToJSONSchemaContext, _params?: ProcessParams): JSONSchema.BaseSchema;
  97. export declare function extractDefs<T extends schemas.$ZodType>(ctx: ToJSONSchemaContext, schema: T): void;
  98. export declare function finalize<T extends schemas.$ZodType>(ctx: ToJSONSchemaContext, schema: T): ZodStandardJSONSchemaPayload<T>;
  99. export type ZodStandardSchemaWithJSON<T> = StandardSchemaWithJSONProps<core.input<T>, core.output<T>>;
  100. export interface ZodStandardJSONSchemaPayload<T> extends JSONSchema.BaseSchema {
  101. "~standard": ZodStandardSchemaWithJSON<T>;
  102. }
  103. /**
  104. * Creates a toJSONSchema method for a schema instance.
  105. * This encapsulates the logic of initializing context, processing, extracting defs, and finalizing.
  106. */
  107. export declare const createToJSONSchemaMethod: <T extends schemas.$ZodType>(schema: T, processors?: Record<string, Processor>) => (params?: ToJSONSchemaParams) => ZodStandardJSONSchemaPayload<T>;
  108. /**
  109. * Creates a toJSONSchema method for a schema instance.
  110. * This encapsulates the logic of initializing context, processing, extracting defs, and finalizing.
  111. */
  112. type StandardJSONSchemaMethodParams = Parameters<StandardJSONSchemaV1["~standard"]["jsonSchema"]["input"]>[0];
  113. export declare const createStandardJSONSchemaMethod: <T extends schemas.$ZodType>(schema: T, io: "input" | "output", processors?: Record<string, Processor>) => (params?: StandardJSONSchemaMethodParams) => JSONSchema.BaseSchema;
  114. export {};