Building Schemas Programmatically with Zod
Instead of using JSON Schemas, you have the option to define your schemas using zod (opens in a new tab). feTS will automatically convert these into JSON Schemas behind the scenes and use 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'
})
}
})