Skip to content

Request Validation Plugin

The Request Validation Plugin ensures that only valid requests are sent to your server. This is especially valuable for applications that depend on server-side validation.

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 {
RequestValidationPlugin
} from '@orpc/contract/plugins'
const
link
= new
RPCLink
({
url
: 'http://localhost:3000/rpc',
plugins
: [
new
RequestValidationPlugin
(
contract
),
], }) const
client
:
ContractRouterClient
<typeof
contract
> =
createORPCClient
(
link
)

INFO

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

Form Validation

You can simplify your frontend by removing heavy form validation libraries and relying on oRPC's validation errors instead, since input validation runs directly in the browser and is highly performant.

tsx
import { getIssueMessage, parseFormData } from '@orpc/openapi-client/helpers'

export function ContactForm() {
  const [error, setError] = useState()

  const handleSubmit = async (form: FormData) => {
    try {
      const output = await client.someProcedure(parseFormData(form))
      console.log(output)
    }
    catch (error) {
      setError(error)
    }
  }

  return (
    <form action={handleSubmit}>
      <input name="user[name]" type="text" />
      <span>{getIssueMessage(error, 'user[name]')}</span>

      <input name="user[emails][]" type="email" />
      <span>{getIssueMessage(error, 'user[emails][]')}</span>

      <button type="submit">Submit</button>
    </form>
  )
}

INFO

This example uses Form Data Helpers.

Released under the MIT License.