-
-
Notifications
You must be signed in to change notification settings - Fork 159
feat(contract): Response Validation Plugin #953
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
apps/content/docs/openapi/advanced/expanding-type-support-for-openapi-link.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| --- | ||
| title: Expanding Type Support for OpenAPI Link | ||
| description: Learn how to extend OpenAPILink to support additional data types beyond JSON's native capabilities using the Response Validation Plugin and schema coercion. | ||
| --- | ||
|
|
||
| # Expanding Type Support for OpenAPI Link | ||
|
|
||
| This guide will show you how to extend [OpenAPILink](/docs/openapi/client/openapi-link) to support additional data types beyond JSON's native capabilities using the [Response Validation Plugin](/docs/plugins/response-validation). | ||
|
|
||
| ## How It Works | ||
|
|
||
| To enable this functionality, you need to customize your output schema with proper coercion logic. | ||
|
|
||
| **Why?** OpenAPI response data only represents JSON's native capabilities. We use schema coercion logic in output schemas to convert the data to the desired type. | ||
|
|
||
| ::: warning | ||
| Beyond JSON limitations, outputs containing `Blob` or `File` types (outside the root level) also face [Bracket Notation](/docs/openapi/bracket-notation#limitations) limitations. | ||
| ::: | ||
|
|
||
| ```ts | ||
| const contract = oc.output(z.object({ | ||
| date: z.coerce.date(), // [!code highlight] | ||
| bigint: z.coerce.bigint(), // [!code highlight] | ||
| })) | ||
|
|
||
| const procedure = implement(contract).handler(() => ({ | ||
| date: new Date(), | ||
| bigint: 123n, | ||
| })) | ||
| ``` | ||
|
|
||
| On the client side, you'll receive the output like this: | ||
|
|
||
| ```ts | ||
| const beforeValidation = { | ||
| date: '2025-09-01T07:24:39.000Z', | ||
| bigint: '123' | ||
| } | ||
| ``` | ||
|
|
||
| Since your output schema contains coercion logic, the Response Validation Plugin will convert the data to the desired type after validation. | ||
|
|
||
| ```ts | ||
| const afterValidation = { | ||
| date: new Date('2025-09-01T07:24:39.000Z'), | ||
| bigint: 123n | ||
| } | ||
| ``` | ||
|
|
||
| ::: warning | ||
| To support more types than those in [OpenAPI Handler](/docs/openapi/openapi-handler#supported-data-types), you must first extend the [OpenAPI JSON Serializer](/docs/openapi/advanced/openapi-json-serializer) first. | ||
| ::: | ||
|
|
||
| ## Setup | ||
|
|
||
| After understanding how it works and expanding output schemas with coercion logic, you only need to set up the [Response Validation Plugin](/docs/plugins/response-validation) and remove the `JsonifiedClient` wrapper. | ||
|
|
||
| ```diff | ||
| import type { ContractRouterClient } from '@orpc/contract' | ||
| import { createORPCClient } from '@orpc/client' | ||
| import { OpenAPILink } from '@orpc/openapi-client/fetch' | ||
| import { ResponseValidationPlugin } from '@orpc/contract/plugins' | ||
|
|
||
| const link = new OpenAPILink(contract, { | ||
| url: 'http://localhost:3000/api', | ||
| plugins: [ | ||
| + new ResponseValidationPlugin(contract), | ||
| ] | ||
| }) | ||
|
|
||
| -const client: JsonifiedClient<ContractRouterClient<typeof contract>> = createORPCClient(link) | ||
| +const client: ContractRouterClient<typeof contract> = createORPCClient(link) | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| --- | ||
| title: Response Validation Plugin | ||
| description: A plugin that validates server responses against the contract schema to ensure that the data returned from your server matches the expected types defined in your contract. | ||
| --- | ||
|
|
||
| # 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](/docs/contract-first/define-contract). [Minified Contract](/docs/contract-first/router-to-contract#minify-export-the-contract-router-for-the-client) is **not supported** because it removes the schema from the contract. | ||
| ::: | ||
|
|
||
| ## Setup | ||
|
|
||
| ```ts twoslash | ||
| import { contract } from './shared/planet' | ||
| import { createORPCClient } from '@orpc/client' | ||
| import type { ContractRouterClient } from '@orpc/contract' | ||
| // ---cut--- | ||
| 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](/docs/client/rpc-link), [OpenAPILink](/docs/openapi/client/openapi-link), 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](/docs/openapi/advanced/expanding-type-support-for-openapi-link). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| it('exports something', async () => { | ||
| expect(await import('./index')).toHaveProperty('ResponseValidationPlugin') | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './response-validation' |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.