Skip to main content

Import

What they do

Schema adapters wrap external validation libraries into a SchemaAdapter<T> interface so you can pass them to ApiClient’s validateResponse option. The client calls adapter.parse(data) after every successful response — if parsing fails, the error propagates immediately.

SchemaAdapter<T>

Any object that satisfies this interface works as a response validator. zodAdapter and valibotAdapter are convenience wrappers for the two most popular schema libraries.

zodAdapter

Wraps a Zod schema. Since Zod schemas already expose a parse method, you can also pass a Zod schema directly — the adapter simply adds type safety.

Zod example

valibotAdapter

Wraps a Valibot schema and its parse function. Unlike Zod, Valibot separates the schema definition from the parse function, so both must be provided.

Valibot example

Type guard

isSchemaAdapter(obj) returns true if the object has a parse function — useful when you accept both ValidationSchema and SchemaAdapter in generic code.

Comparison

  • You already use Zod in your project
  • You want .parse() + .safeParse() error messages
  • You need transforms (.transform(), .default(), .refine())
  • You prefer Valibot’s tree-shakeable, functional API
  • You want a smaller bundle footprint for validation
  • You want zero external dependencies
  • You only need structural type checks and simple constraints
  • See ResponseValidator
Schema adapters are optional. If you don’t pass validateResponse, ApiClient returns the raw parsed JSON without validation.