Skip to content

API

createValidatorCompiler

Creates a compiler function that validates Fastify request data using Zod schemas.

Pass the returned function to Fastify’s setValidatorCompiler.

createSerializerCompiler

Creates a function that can be used as a response serializer compiler, validating responses using Zod.

Pass the returned function to Fastify’s setSerializerCompiler.

Supported options:

ts
export interface ZodSerializerCompilerOptions {
  replacer?: (this: any, key: string, value: any) => any;
}

createJsonSchemaTransform

Creates a method to transform Zod schemas into JSON Schema.

Supported options:

ts
interface CreateJsonSchemaTransformOptions {
  /**
   * Zod registry that will be be used to generate `components.schemas`.
   *
   * @default import('zod').globalRegistry
   */
  schemaRegistry?: $ZodRegistry<{ id?: string | undefined }>;
}

createJsonSchemaTransformObject

Same as createJsonSchemaTransform, but returns an object containing both the transform and a reference resolver.

Supported options:

ts
interface CreateJsonSchemaTransformObjectOptions {
  /**
   * Zod registry that will be be used to generate `components.schemas`.
   *
   * @default import('zod').globalRegistry
   */
  schemaRegistry?: $ZodRegistry<{ id?: string | undefined }>;

  /**
   * Use `id` to populate schema `title` field when generating OpenAPI `components.schemas`.
   * Might be useful when using OpenAPI tools, like scalar, who rely on this to show components names.
   *
   * @default false
   */
  setIdAsTitleInSchemas?: boolean;
}

hasZodFastifySchemaValidationErrors

Utility to detect whether an error comes from Zod schema validation.

See: Customizing error responses

isResponseSerializationError

Utility to detect whether an error was thrown by Zod during response serialization.

See: Customizing error responses