supervizor
Server-level request payload validation for hapi.
Table of Contents
Installation
Install via NPM.
$ npm install supervizor
Usage
Register the package as a server plugin and provide a validation function via the options
that will be attached to each route.
If the validation fails, a joi-like 400 Bad Request
error is returned alongside an additional content-validation: failure
response header. If everything is ok, the response will ultimately contain a content-validation: success
header.
Synchronous validation
const Hapi = ;const Supervizor = ; const plugin = plugin: Supervizor options: { // In this example, the payload must contain `valid: true`. if !payloadvalid // Be nice to everyone and provide details about the issue. // protip: https://github.com/hapijs/joi/blob/v13.0.1/API.md#errors const error = 'invalid payload'; errordetails = path: 'valid' ; throw error; // Be nice to yourself and allow further validation. return payload; } ; try const server = ; await server; await serverstart;catch err throw err;
Asynchronous validation
const Hapi = ;const Supervizor = ; const plugin = plugin: Supervizor options: validator: async { // In this example, an asychronous validation function is called. try await ; // Be nice to yourself and allow further validation. return payload; catch err // Be nice to everyone and provide details about the issue. // protip: https://github.com/hapijs/joi/blob/v13.0.1/API.md#errors const error = 'invalid payload'; errordetails = path: 'valid' ; throw error; } ; try const server = ; await server; await serverstart;catch err throw err;