Docs/Controllers
@defineRequest
Declares and validates the incoming request shape.
@defineRequest({
headers: AnyClass,
query: AnyClass,
path: AnyClass,
body: AnyClass,
files: AnyClass,
auth: AnyClass,
})Each value is a DTO class whose properties carry @validateProperty, @documentProperty, etc.
Undeclared fields are removed from req.query, req.path, req.body and req.files unless the route allows unknown fields. The query string is only parsed when something reads it: declared query fields, allowUnknownFields, or a @withAuth / @accessControl rule that points at query.*.
Header names are case-insensitive. A header DTO property, or headers.X-Api-Key in @withAuth / @accessControl, matches the header whatever case the client sends, and the OpenAPI document keeps the spelling you declared. Declaring the same header twice in different cases (X-Api-Key and x-api-key) throws AladoConfigurationError with code: 'HEADER_DECLARED_TWICE' at startup.
Every declared field needs a validator: a DTO property with only @documentProperty or @transformProperty stops the application at startup with an error naming it, such as Request field body.reason has no validator.
The request body
The body is read for POST, PUT, PATCH and QUERY only: RFC 9110 gives a GET, HEAD, DELETE or OPTIONS body no meaning. A route of those methods that declares body or files, or reads body.*, rawBody or files.* in @withAuth / @accessControl, throws AladoConfigurationError with code: 'BODY_NOT_ALLOWED' at startup.
A route reads the body only when it declares something that needs it: body or files fields, a body.* / rawBody / files.* auth input or access-control rule, allowUnknownFields, or rawBody: true in the route options. Otherwise the body is neither read nor parsed: req.body is {} and req.rawBody is unset, and a malformed body does not matter. A route that only needs the raw text, such as a webhook checking a signature, declares rawBody: true:
@post('/webhooks/payments', { rawBody: true })
async payment(req: Request) {
verifySignature(req.rawBody, req.headers['x-signature']);
return { statusCode: 204 };
}A body nobody reads (also on a 404 or 405) is discarded by Node when it is within maxBodySizeBytes, so the connection is kept; a larger or chunked one closes the connection after the response instead of being read.
What req.body holds:
| Request | req.body |
|---|---|
No body (empty, no Content-Type) |
missing |
Empty text/plain or other text type |
'' |
| Empty JSON, XML, urlencoded or multipart body | {} |
JSON false, 0, "", null |
that value |
Binary type (application/octet-stream, image/*, ...) |
{}; the bytes are in req.rawBodyBytes |
Text bodies are read as UTF-8. A body with a binary Content-Type (application/octet-stream, image/*, audio/*, video/*, font/*, application/pdf, application/zip, application/gzip, protobuf, msgpack, cbor) is not text: it is given as its exact bytes in req.rawBodyBytes (a Buffer), with req.rawBody unset. The route needs rawBody: true (or body fields) to read it, as for any body:
@put('/avatars/:id', { rawBody: true })
@defineRequest({ path: Id })
async upload(req: Request) {
await storage.put(req.path.id, req.rawBodyBytes); // a Buffer
return { statusCode: 204 };
}A compressed body (Content-Encoding other than identity) gets 415 Unsupported Content-Encoding, and a text body declaring a charset other than UTF-8 gets 415 Unsupported charset. A UTF-8 byte order mark at the start of a text body is skipped for parsing (rawBody keeps it).
Keys named __proto__, constructor, prototype, __defineGetter__ and __defineSetter__ are removed from JSON bodies (at any depth), form fields (urlencoded and multipart), query strings and XML elements and attributes: they could reach the prototype of the object they are copied into. A list key such as __defineGetter__[] is removed too (3.9.10; before, query strings and XML kept __defineGetter__ and __defineSetter__, and forms kept them with a [] suffix). A field with one of those names never reaches a validator or the handler.
Urlencoded bodies and multipart text fields follow the query rules: a key sent more than once, or as key[], gives an array of every value in order (checkboxes, multi-selects), a single key a string. A field declared with validate.string() therefore refuses a repeated value instead of keeping one of them. Empty form fields are kept as ''; empty query values are skipped.
Query and path values, and the fields of a form body (urlencoded or multipart), arrive as strings. A field validated by validate.number() or validate.bigint() gets the string converted first, and the handler receives the number. See Available Rules.
Order of the steps
Routing, @withAuth, @accessControl, then the body is read and parsed (when the route needs it), then the request parts are validated, then the files. The body is read earlier when the auth input or an access-control rule points at body.*. The first step that fails answers with its configured error, and the handler does not run. Hooks run around these steps: onRequest before routing, beforeHandler after the files are validated, beforeSend before the response is written.
Example:
@post('/user', { tags: ['User'] })
@defineRequest({ body: CredentialsDto })
async create(req: Request) {
console.log(req.body); // typed, validated CredentialsDto fields
}