Skip to content
You are reading the v2 docs, currently in beta.V1 docs
oRPC
Esc
↑↓navigate↵open⌘Jpreview
On this page

Valibot Integration

Use Valibot schemas directly in oRPC via Standard Schema, with a dedicated JSON Schema converter for OpenAPI generation and Smart Coercion.

Installation

npm install @orpc/valibot@beta valibot
pnpm add @orpc/valibot@beta valibot
yarn add @orpc/valibot@beta valibot
bun add @orpc/valibot@beta valibot
nub add @orpc/valibot@beta valibot
aube add @orpc/valibot@beta valibot

JSON Schema Converter

ValibotToJsonSchemaConverter wraps Valibot’s built-in toJsonSchema and adds support for additional types such as v.bigint(), v.date(), v.set(), v.map(), v.file(), and v.blob(). File schemas take their contentMediaType from a v.mimeType() action. Use it with tools such as the OpenAPI Generator and Smart Coercion. It accepts the same options as Valibot’s toJsonSchema, see the source code for implementation details.

import { OpenAPIGenerator } from '@orpc/openapi'
import { ValibotToJsonSchemaConverter } from '@orpc/valibot'

const generator = new OpenAPIGenerator({
  converters: [new ValibotToJsonSchemaConverter()],
})

Reusable Schemas

A common pattern is defining reusable or recursive schemas via definitions. The converter preserves them in $defs, which OpenAPIGenerator can then hoist into components.schemas. For more on how definitions work in Valibot, see Valibot JSON Schema Definitions.

import * as v from 'valibot'

const PlanetSchema = v.object({
  id: v.string(),
  name: v.string(),
})

const generator = new OpenAPIGenerator({
  converters: [
    new ValibotToJsonSchemaConverter({
      definitions: { PlanetSchema },
    }),
  ],
})

Last updated on

Was this page helpful?