Validate
function in meocord/decorator Since 4.1.0
Validate<
S extends StandardSchemaV1,
const Pipes extends ValidatePipes<S> = Record<never, never>,
>(
schema: S,
options?: ValidateOptions<Pipes>,
): <M extends Handler>(
target: object,
propertyKey: string,
_descriptor: TypedPropertyDescriptor<M> &
AcceptsInput<ParamsOf<M>, ValidatedInput<S, Pipes>>,
) => voidValidates a handler's input with a Standard Schema before it runs.
Use it so a handler receives typed, valid values or does not run at all: a command's options, a
component's customId params, a modal's fields or a message command's params. Any Standard Schema library
works, such as zod, valibot or arktype. To transform one value, use UsePipe.
Where it runs
- Validation — after the guards and the fetch, inside the interceptors
Parameters
| Name | Type | Description |
|---|---|---|
schema | S | A Standard Schema for the whole input object. |
options? | ValidateOptions<Pipes> |
|
options.pipes? | Pipes | Pipes for single values of the schema's output, by key: one pipe, or several applied in order. They run
before the key's |
Returns
Returns a method decorator.
<M extends Handler>(
target: object,
propertyKey: string,
_descriptor: TypedPropertyDescriptor<M> &
AcceptsInput<ParamsOf<M>, ValidatedInput<S, Pipes>>,
) => voidThrows
Error when
schemais not a Standard Schema, or the handler already has a@Validate, as the decorator applies.
Examples
import { z } from 'zod'
@Command('remind', CommandType.SLASH)
@Validate(z.object({ minutes: z.number().int().min(1).max(1440) }))
async remind(interaction: ChatInputCommandInteraction, { minutes }: { minutes: number }) {
await respond(interaction).send(`I'll remind you in ${minutes} minutes.`)
}