Skip to content

Response Validation Plugin

The Response Validation Plugin validates server responses against your contract schema, ensuring that data returned from your server matches the expected types defined in your contract.

INFO

This plugin is best suited for Contract-First Development. Minified Contract is not supported because it removes the schema from the contract.

Setup

ts
import { 
RPCLink
} from '@orpc/client/fetch'
import {
ResponseValidationPlugin
} from '@orpc/contract/plugins'
const
link
= new
RPCLink
({
url
: 'http://localhost:3000/rpc',
plugins
: [
new
ResponseValidationPlugin
(
contract
),
], }) const
client
:
ContractRouterClient
<typeof
contract
> =
createORPCClient
(
link
)

INFO

The link can be any supported oRPC link, such as RPCLink, OpenAPILink, or custom implementations.

Limitations

Schemas that transform data into different types than the expected schema types are not supported.

Why? Consider this example schema that accepts a number and transforms it into a string after validation:

ts
const unsupported = z.number().transform(value => value.toString())

When the server validates output, it transforms the number into a string. The client receives a string, but the string no longer matches the original schema, causing validation to fail.

Advanced Usage

Beyond response validation, this plugin also serves special purposes such as Expanding Type Support for OpenAPI Link.

Released under the MIT License.