GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - ruiquelhas/supervizor: Server-level request payload validation for hapi

BranchesTags
Open more actions menu

Latest commit

 

History

38 Commits
38 Commits

Folders and files

NameName
Last commit message
Last commit date
lib
lib
 
 
test
test
 
 
.gitignore
.gitignore
 
 
.travis.yml
.travis.yml
 
 
CHANGELOG.md
CHANGELOG.md
 
 
CONTRIBUTING.md
CONTRIBUTING.md
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
package-lock.json
package-lock.json
 
 
package.json
package.json
 
 

Repository files navigation

supervizor

Server-level request payload validation for hapi.

NPM Version Build Status Coverage Status Dependencies Dev Dependencies

Table of Contents

  • Installation
  • Usage
    • Synchronous validation
    • Asynchronous validation

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

www.npmjs.com/package/supervizor

Resources

Readme
BSD-3-Clause license

Contributing

Contributing
Activity

Stars

1 star

Watchers

1 watching

Forks

1 fork
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.