Server
Programmatic Schemas with Zod

Programmatic Schemas with Zod

You can also define your schemas with zod (opens in a new tab) instead of JSON Schemas. feTS automatically converts them into JSON Schemas under the hood and uses them to validate your data.

Example

import { createRouter, Response } from 'fets'
import { z } from 'zod'
 
const router = createRouter().route({
  path: '/user/:id',
  method: 'POST',
  schemas: {
    request: {
      headers: z.object({
        authorization: z.string().regex(/Bearer .+/)
      }),
      params: z.object({
        id: z.string().uuid()
      })
    }
  },
  // Response type will be automatically inferred from the handler's return type
  handler: request => {
    if (request.params.id !== EXPECTED_UUID) {
      return Response.json(
        {
          message: 'User not found'
        },
        {
          status: 404
        }
      )
    }
    return Response.json({
      id: request.params.id,
      name: 'John Doe'
    })
  }
})