Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

relish

Package Overview
Dependencies
Maintainers
5
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

relish

Better error messages for Hapi.js Joi validation

  • 3.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
178
increased by2.89%
Maintainers
5
Weekly downloads
 
Created
Source

Relish Logo

Better error messages for Hapi.js Joi validation

NPM Version Build Status Coverage Status

Code Style

Introduction

Joi provides awesome schema validation, but the error messages returned are not user-friendly. This package returns a more user-friendly version of Joi's default response and allows for custom error messages.

Default Joi Response

{
  "statusCode": 400,
  "error": "Bad Request",
  "message": "child \"data\" fails because [child \"name\" fails because [\"name\" is not allowed to be empty], child \"email\" fails because [\"email\" must be a valid email]]",
  "validation": {
    "source": "payload",
    "keys": [
      "data.name",
      "data.email"
    ]
  }
}

Example Relish Response

{
  "statusCode": 400,
  "error": "Bad Request",
  "message": "\"name\" is not allowed to be empty, \"email\" must be a valid email",
  "validation": {
    "source": "payload",
    "errors": [
      {
        "key": "name",
        "path": "data.name",
        "message": "\"name\" is not allowed to be empty",
        "type": "any",
        "constraint": "required"
      },
      {
        "key": "email",
        "path": "data.email",
        "message": "\"email\" must be a valid email",
        "type": "string",
        "constraint": "email"
      }
    ]
  }
}

Installation

npm install relish --save

Usage

First load and initialize the module

// load the package and set custom message options
const Relish = require('relish')({
  messages: {
    'data.name': 'Please enter your full name'
  }
});

Once initialized, this package exposes a custom failAction handler that can be used in your Hapi.js Route Options.

// call the failAction handler in your route options
server.route({
  method: 'POST',
  path: '/',
  config: {
    validate: {
      // set a custom failAction handler
      failAction: Relish.failAction,
      payload: {
        data: Joi.object({
          name: Joi.string().required(),
          email: Joi.string().email()
        })
      }
    }
  },
  handler: (request, h) => h.response()
});
Global Usage (alternative)

You can apply this module to all routes by setting the failAction in your server options.

Hapi.server({
  // ... other server options

  routes: {
    validate: {
      failAction: Relish.failAction
    }
  }
});

Version Compatibility

Below are the compatible versions of Relish for Hapi and Joi.

Relish VersionHapi VersionJoi Version
1.x<= 16.6.0<= 10.x
2.x>= 16.6.1, < 17.x>= 11.x
3.x>= 17.x>= 11.x

API Documentation

See the API Documentation for reference.

Keywords

FAQs

Package last updated on 11 Jun 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc