-
Notifications
You must be signed in to change notification settings - Fork 1
Koa Context Functions and Properties
Each context function is added to the Koa 2 context and is available in routes and middleware for any request.
-
A bodyparser is required to use
validateBody(). If you aren't using one, I suggest using koa-bodyparser. -
A router is required to use
validateParams(). If you aren't using one, I suggest using koa-router.
Validates the incoming body based on the structure and validators given.
ctx.validateBody({
firstName: v().required()
});Validates the incoming parameters based on the structure and validators given.
ctx.validateParams({
firstName: v().required()
});Validates the incoming query string values based on the structure and validators given.
ctx.validateQuery({
firstName: v().required()
});Validates the incoming header values based on the structure and validators given.
ctx.validateHeaders({
'x-my-token': v().required().uuid()
});This is a variable that is set in the event of a failed validation. It is an object that contains keys and messages for the values that failed validation.
In the example below there is a top level property and an example of a response if an object of address and line1 is required.
Example JSON Response:
{
"firstName": "Value is required.",
"address.line1": "Value is required."
}