Skip to content

ruiquelhas/supervizor

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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;
}

About

Server-level request payload validation for hapi

Resources

License

Stars

Watchers

Forks

Packages

No packages published