npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hapi-format-validation

v2.3.2

Published

Formats validation errors in Hapi

Downloads

15

Readme

hapi-format-validation

Build Status

This Hapi plugin formats validation errors in a way that is consistent, simple, and easy to render in client-side forms. Take your typical Joi validation error reply for instance...

Before hapi-format-validation 😿

{
  statusCode: 400,
  error: 'Bad Request',
  // this message should get formatted before displaying it to a user
  message: 'child "name" fails because ["name" is not allowed to be empty]. child "email" fails because ["email" must be a valid email]',
  validation: {
    source: 'payload',
    // extra work required of the client to link these keys to error messages :(
    keys: [
      'name',
      'email'
    ]
  }
}

After hapi-format-validation 😍

{
  statusCode: 400,
  error: 'Bad Request',
  // a newline-separated string, ready-to-use if necessary
  message: '"name" is not allowed to be empty↵"email" must be a valid email',
  validation: {
    source: 'payload',
    // a simple key-value mapping of fields and their errors
    errors: {
      name: '"name" is not allowed to be empty',
      email: '"email" must be a valid email'
    }
  }
}

Installation

$ npm install --save hapi-format-validation

Usage

const FormatValidation = require('hapi-format-validation');

server.register(FormatValidation, err => {
  // server fun times
});

Options

  • stripQuotes: (optional) if true, strips double quotation marks from around the path name in error messages
  • capitalize: (optional) if true, capitalizes the first letter of each error message
  • sequelize: (optional) pass a Sequelize instance to format unique key violations (more information below)

Sequelize integration

hapi-format-validation also handles Sequelize unique key violation errors, which would otherwise be a 500 Internal Server Error. Pass your Sequelize instance (sold separately) as an option to the plugin when you register it to enable this feature.

const FormatValidation = require('hapi-format-validation');
const Sequelize = require('sequelize');

const sequelize = new Sequelize(...);

server.register(
  {
    register: FormatValidation,
    options: {sequelize}
  },
  err => {
    // do your server stuff
  }
);

Before

{
  statusCode: 500,
  error: 'Internal Server Error',
  message: 'An internal server error occurred'
}

After

{
  'statusCode': 400,
  'error': 'Bad Request',
  'message': '"username" must be unique',
  'validation': {
    'source': 'payload',
    'errors': {
      'username': '"username" must be unique'
    }
  }
}

Acknowledgements 👊