Validation
Every request to the API is checked before it reaches any of our code. Pixelflare does this with Zod schemas, through @hono/zod-openapi, so one schema does three jobs:
- Rejects bad input with a clear
400error - Gives the route handler fully typed data
- Documents the endpoint in the API reference
How It Works
- Each endpoint is defined with
createRoute()inpackages/api/src/schemas/, describing its params, query, body and responses - Its handler is added with
router.openapi(route, handler), on a router made bycreateRouter() - Before the handler runs, Hono checks the request against the schema. If it doesn't match, the request stops there, with a
400 - In the handler,
c.req.valid('json')(or'param', or'query') returns the checked, typed data
For example, creating a tag:
// schemas/tags.ts
export const createTagRoute = createRoute({
method: 'post',
path: '/',
tags: ['Tags'],
summary: 'Create a new tag',
security: [{ Bearer: ['write'] }],
request: {
body: {
content: { 'application/json': { schema: schemas.createTag } },
required: true,
},
},
responses: {
201: {
description: 'Tag created successfully',
content: { 'application/json': { schema: TagResourceSchema } },
},
400: ErrorResponses[400],
409: ErrorResponses[409],
},
});// routes/api/tags.ts
tagsRouter.openapi(createTagRoute, async (c) => {
const data = c.req.valid('json'); // already checked, and typed
const tag = await createTag(c.get('db'), c.get('auth').owner, data);
return c.json(tag, 201);
});Shared Schemas
Pieces that more than one endpoint needs live in schemas/validation.ts, so check there before writing your own:
schemas.paginationforlimitandcursorschemas.slug,schemas.ownerParamandschemas.albumSlugParamfor URL params- Request bodies, like
schemas.createImage,schemas.createTagandschemas.createApiKey expirationSchemafor image expiry options
Limits like maximum lengths and file sizes come from @pixflare/config, never typed inline, so the API, the web app and these docs all agree.
Security Checks
Some fields get extra checks from lib/security-validation.ts, usually in a schema's .transform():
| Helper | What it does |
|---|---|
validateFilename | Rejects path traversal, hidden files, executables, prototype pollution, and anything but letters, numbers, spaces, dots, hyphens and underscores |
validateSlug | Allows only lowercase letters, numbers, hyphens and underscores |
validateContentType | Allows only the accepted image types |
sanitizeText | Trims, strips HTML tags and null bytes, and cuts to length |
The first three throw a ValidationError on bad input, which also becomes a 400. See Security for the bigger picture.
Error Responses
When a request fails validation, the API sends back a 400 saying which fields were wrong:
{
"success": false,
"error": {
"code": "VALIDATION_ERROR",
"message": "Request validation failed",
"details": [
{
"path": ["tag"],
"message": "Too small: expected string to have >=1 characters",
"code": "too_small"
}
]
}
}Adding an Endpoint
- Reuse a schema from
schemas/validation.ts, or add one there, taking any limits from@pixflare/config - Define the route with
createRoute(), in the matching file inschemas/ - Add the handler with
router.openapi(), and read the input withc.req.valid() - Test that good input works, and bad input gets a
400
Since the schemas also make the API docs, give each field an example and description with .openapi({ example, description }).