@condiments/chow-chow

    6.0.0 • Public • Published

    logo

    Simplify registration of Relish.js messages.

    npm version Known Vulnerabilities GitHub license Build Status Coverage Status

    Lead Maintainer: Robert Hernandez

    Introduction

    Relish provides "better error message for Hapi.js Joi validation."

    However, in large scale applications managing the individual route validate failAction functions custom messages can become tedious. Even though a common object may be defined in the hapi server's route configuration, this only facilitates common messages.

    For example, if you need to have a option which is required on one route but optional on another, it is difficult to change the custom message.

    Problem Example

        server.route({
            method: 'POST',
            path: '/relish/custom',
            options: {
                validate: {
                    payload: {
                        input: Joi.string().min(3).max(10)
                    },
                    /// this is a bulky statement since it is a function call then a pointer to a function.
                    failAction: Relish({
                        messages: {
                            input: 'input is required and must be between 3 and 10 characters in length'
                        }
                    }).failAction
                },
                handler: handler
            }
        });

    Solution Example

        server.route({
            method: 'POST',
            path: '/example',
            options: {
                validate: {
                    payload: {
                        input: Joi.string().min(3).max(10)
                    }
                },
                // hapi standard plugin syntax
                plugins: {
                    chowChow: {
                        input: 'input is required and must be between 3 and 10 characters in length'
                    }
                },
                handler: handler
            }
        });

    Getting Started

    Installation

    npm install @condiments/chow-chow
    

    Bootsrapping in hapi server

    const server = new Hapi.Server({
        port: 3000,
        host: 'localhost'
    });
    
    
    const init = async () => {
        await server.register(require('@condiments/chow-chow'));
        // register routes
    };
    
    init();

    Example Outputs

    Example chow-chow

    Request

    curl http://localhost:3000/example -H "Content-Type: application/json" -d "{\"input\":\"HI\"}"
    

    Response

    {
        "statusCode": 400,
        "error": "Bad Request",
        "message": "input is required and must be between 3 and 10 characters in length",
        "validation": {
            "source": "payload",
            "errors": [
                {
                    "key": "input",
                    "path": "input",
                    "message": "input is required and must be between 3 and 10 characters in length",
                    "type": "string",
                    "constraint": "min"
                }
            ]
        }
    }

    Default Relish

    Request

    curl http://localhost:3000/relish/default -H "Content-Type: application/json" -d "{\"input\":\"HI\"}"
    

    Response

    {
        "statusCode": 400,
        "error": "Bad Request",
        "message": "\"input\" length must be at least 3 characters long",
        "validation": {
            "source": "payload",
            "errors": [
                {
                    "key": "input",
                    "path": "input",
                    "message": "\"input\" length must be at least 3 characters long",
                    "type": "string",
                    "constraint": "min"
                }
            ]
        }
    }

    Custom Relish

    Request

    curl http://localhost:3000/relish/custom -H "Content-Type: application/json" -d "{\"input\":\"HI\"}"
    

    Response

    {
        "statusCode": 400,
        "error": "Bad Request",
        "message": "input is required and must be between 3 and 10 characters in length",
        "validation": {
            "source": "payload",
            "errors": [
                {
                    "key": "input",
                    "path": "input",
                    "message": "input is required and must be between 3 and 10 characters in length",
                    "type": "string",
                    "constraint": "min"
                }
            ]
        }
    }

    Native hapi

    Request

    curl http://localhost:3000/native -H "Content-Type: application/json" -d "{\"input\":\"HI\"}"
    

    Response

    {
        "statusCode": 400,
        "error": "Bad Request",
        "message": "Invalid request payload input"
    }

    Install

    npm i @condiments/chow-chow

    DownloadsWeekly Downloads

    47

    Version

    6.0.0

    License

    ISC

    Unpacked Size

    9.39 kB

    Total Files

    5

    Last publish

    Collaborators

    • gentleman-turk