Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript. Learn more »

supervizor

2.1.0 • Public • Published

supervizor

Server-level request payload validation for hapi.

NPM Version Build Status Coverage Status Dependencies Dev Dependencies

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 = require('hapi');
const Supervizor = require('supervizor');
 
const plugin = {
    plugin: Supervizor,
    options: {
        validator: (payload) => {
            // In this example, the payload must contain `valid: true`.
            if (!payload.valid) {
                // 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 = new Error('invalid payload');
                error.details = [{ path: ['valid'] }];
 
                throw error;
            }
 
            // Be nice to yourself and allow further validation.
            return payload;
        }
    }
};
 
try {
    const server = new Hapi.Server();
 
    await server.register(plugin);
    await server.start();
}
catch (err) {
    throw err;
}

Asynchronous validation

const Hapi = require('hapi');
const Supervizor = require('supervizor');
 
const plugin = {
    plugin: Supervizor,
    options: {
        validator: async (payload, options) => {
            // In this example, an asychronous validation function is called.
            try {
                await validate(payload, options);
 
                // 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 = new Error('invalid payload');
                error.details = [{ path: ['valid'] }];
 
                throw error;
            }
        }
    }
};
 
try {
    const server = new Hapi.Server();
 
    await server.register(plugin);
    await server.start();
}
catch (err) {
    throw err;
}

install

npm i supervizor

Downloadsweekly downloads

17

version

2.1.0

license

BSD-3-Clause

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability