The new decorator function.
import { Controller, ControllerBase, IHttpRequest, IHttpResponse, JoiValidationError, POST, schema, ValidationErrorHandler } from '@egomobile/http-server'
const mySchema = schema.object({
email: schema.string().trim().email().required(),
name: schema.string().trim().min(1).optional()
})
@Controller()
export default class MyController extends ControllerBase {
@POST(mySchema)
async index(request: IHttpRequest, response: IHttpResponse) {
response.write('You send: ' + JSON.stringify(response.body!))
}
@ValidationErrorHandler() // mark that method as default schema validation
// error handler inside that controller
async handleValidationError(error: JoiValidationError, request: IHttpRequest, response: IHttpResponse) {
const errorMessage = Buffer.from('VALIDATION ERROR: ' + error.message, 'utf8')
response.writeHead(400, {
'Content-Length': String(errorMessage.length)
})
response.write(errorMessage)
response.end()
}
}
Generated using TypeDoc
Add a method of a controller as a validation error handler.