Volt v0.1 · TypeScript APIs on Bun · Get started
Docs/Validation & Typing/Schema Validation

Schema Validation

Use Volt schemas, numeric limits, validation errors, and explicit static types.

Complete validated request example

Use Volt's t builder in the third route argument. Numeric constraints are min and max. The body is validated at runtime; use Static<typeof Schema> for an explicit TypeScript type.

index.ts
import { Volt, t, type Static } from 'voltts';

const ProductInput = t.Object({
  title: t.String({ minLength: 3, maxLength: 100 }),
  price: t.Number({ min: 0 }),
  stock: t.Integer({ min: 0 }),
  tags: t.Optional(t.Array(t.String())),
}, { additionalProperties: false });
type ProductInput = Static<typeof ProductInput>;

const app = new Volt();
app.post('/products', (c) => {
  const product: ProductInput = c.body;
  return c.json({ product }, 201);
}, { body: ProductInput });
if (globalThis._importMeta_.main) app.listen(3000);
export default app;

Test valid and invalid input

bash
bun run index.ts
curl -i -X POST http://localhost:3000/products -H 'Content-Type: application/json' -d '{"title":"Coffee","price":12,"stock":3}'
curl -i -X POST http://localhost:3000/products -H 'Content-Type: application/json' -d '{"title":"x","price":-1,"stock":3}'

Expect 201 for the valid input and 400 for the invalid input. additionalProperties: false rejects extra keys.

Schema vocabulary

BuilderOptions or behavior
t.String()minLength, maxLength, format
t.Number(), t.Integer()min, max; Integer rejects fractions
t.Boolean()Requires a boolean in a JSON body
t.Object(properties)Validates nested properties
t.Array(item)minItems, maxItems
t.Optional(schema), t.Nullable(schema)Allows missing values or null
t.Enum(values), t.Literal(value)Restricts to known values
t.Union(schemas), t.Custom(fn)Alternative types or a validation callback

Validation targets and type limits

Routes accept body, query, params, headers, and response entries. Body, query, params, and headers are request validators. Header names should be lowercase. Response schemas provide OpenAPI metadata; the current core does not enforce them on outgoing data. The built-in route signature does not infer body/query types from schemas. Zod objects are not accepted directly as Volt schemas. See Type Coercion for URL inputs.