Aura Router
Guides

Schema Validation

Learn how to add schema validation to your Aura Router endpoints using popular validation libraries.

Schema validation is an optional configuration in createEndpoint and createEndpointConfig. It enables runtime validation for headers, params, searchParams, body, and response, verifying the incoming data and giving the endpoint's request context full type safety.

Aura Router doesn't ship built-in schema validation. Install a supported validation library to use this feature — see Installation below.

Aura Router has limited type inference for TypeBox schemas, due to the expensive computation operations performed by TypeBox. If you want to use TypeBox, use the Static type to infer the schema type inside the endpoint handler.

import { z } from "zod"
import { createEndpoint } from "@aura-stack/router"

export const endpoint = createEndpoint(
  "POST",
  "/auth/signIn",
  async (ctx) => {
    const { username, password } = ctx.body
    return ctx.json({ message: "Successful Login" })
  },
  {
    schemas: {
      body: z.object({
        username: z.string(),
        password: z.string(),
      }),
    },
  }
)
import * as valibot from "valibot"
import { createEndpoint } from "@aura-stack/router"

export const endpoint = createEndpoint(
  "POST",
  "/auth/signIn",
  async (ctx) => {
    const { username, password } = ctx.body
    return ctx.json({ message: "Successful Login" })
  },
  {
    schemas: {
      body: valibot.object({
        username: valibot.string(),
        password: valibot.string(),
      }),
    },
  }
)
import { type } from "arktype"
import { createEndpoint } from "@aura-stack/router"

export const endpoint = createEndpoint(
  "POST",
  "/auth/signIn",
  async (ctx) => {
    const { username, password } = ctx.body
    return ctx.json({ message: "Successful Login" })
  },
  {
    schemas: {
      body: type({
        username: "string",
        password: "string",
      }),
    },
  }
)
import { Type, type Static } from "typebox"
import { createEndpoint } from "@aura-stack/router"

export const endpoint = createEndpoint(
  "POST",
  "/auth/signIn",
  async (ctx) => {
    // TypeBox has limited type inference, cast manually using Static<>
    const { username, password } = ctx.body as unknown as Static<typeof ctx.body>
    return ctx.json({ message: "Successful Login" })
  },
  {
    schemas: {
      body: Type.Object({
        username: Type.String(),
        password: Type.String(),
      }),
    },
  }
)

Installation

To add schema validation support, install the corresponding validation library alongside @aura-stack/router. Supported validators are Zod, TypeBox, Valibot, and ArkType.

npm install zod
npm install typebox
npm install valibot
npm install arktype

On this page