API
Validation
valibot helpers for request bodies, queries and route params.
Modelled on h3-zod, built on
valibot. Each helper accepts a valibot schema or a plain object of schemas,
which is wrapped in v.object.
import { useValidatedBody, useValidatedQuery, vh } from '@wervt/nuxt/server'
import * as v from 'valibot'
const body = await useValidatedBody(event, { title: v.pipe(v.string(), v.nonEmpty()) })
const { page } = await useValidatedQuery(event, { page: v.optional(vh.intAsString, '1') })
Throwing helpers
| Helper | Validates |
|---|---|
useValidatedBody(event, schema) | the JSON body |
useValidatedQuery(event, schema) | query parameters (strings) |
useValidatedParams(event, schema) | route params like [id] (strings) |
They return Promise<Output> and throw a 400 when validation fails. The error's data is valibot's
flatten() result:
{
"status": 400,
"message": "Validation failed",
"data": { "nested": { "title": ["Invalid length: Expected !0 but received 0"] } }
}
Safe helpers
useSafeValidatedBody, useSafeValidatedQuery and useSafeValidatedParams return
{ success: true, data } or { success: false, issues } instead of throwing.
vh
Schemas for values that arrive as strings.
| Schema | Input | Output |
|---|---|---|
vh.id | '42' | 42 (positive integer) |
vh.intAsString | '-3' | -3 |
vh.numAsString | '1.5' | 1.5 |
vh.boolAsString | 'true' / 'false' | true / false |
vh.checkboxAsString | 'on' / missing | true / false |