Skip to content
GitHub

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>>,
) => void

Validates 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

NameTypeDescription
schemaS

A Standard Schema for the whole input object.

options?ValidateOptions<Pipes>

pipes, for single keys of the schema's output, run before that key's @UsePipe 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 @UsePipe pipes.

Returns

Returns a method decorator.

<M extends Handler>(
  target: object,
  propertyKey: string,
  _descriptor: TypedPropertyDescriptor<M> &
    AcceptsInput<ParamsOf<M>, ValidatedInput<S, Pipes>>,
) => void

Throws

  • Error when schema is not a Standard Schema, or the handler already has a @Validate, as the decorator applies.

Examples

TypeScript
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.`)
}