{"id":16483538,"url":"https://github.com/trevorblades/hapi-format-validation","last_synced_at":"2025-10-27T17:32:04.447Z","repository":{"id":57260883,"uuid":"108074265","full_name":"trevorblades/hapi-format-validation","owner":"trevorblades","description":"⚠️ Formats validation errors in Hapi","archived":false,"fork":false,"pushed_at":"2018-11-30T00:23:03.000Z","size":89,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-01T07:23:28.953Z","etag":null,"topics":["api","error-handling","error-messages","hapi","joi","sequelize"],"latest_commit_sha":null,"homepage":"https://npm.im/hapi-format-validation","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/trevorblades.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-10-24T03:54:25.000Z","updated_at":"2018-12-01T16:39:40.000Z","dependencies_parsed_at":"2022-08-31T16:12:16.440Z","dependency_job_id":null,"html_url":"https://github.com/trevorblades/hapi-format-validation","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trevorblades%2Fhapi-format-validation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trevorblades%2Fhapi-format-validation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trevorblades%2Fhapi-format-validation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trevorblades%2Fhapi-format-validation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trevorblades","download_url":"https://codeload.github.com/trevorblades/hapi-format-validation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238535926,"owners_count":19488624,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","error-handling","error-messages","hapi","joi","sequelize"],"created_at":"2024-10-11T13:14:21.607Z","updated_at":"2025-10-27T17:32:04.107Z","avatar_url":"https://github.com/trevorblades.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# hapi-format-validation\n\n[![Build Status](https://travis-ci.com/trevorblades/hapi-format-validation.svg?branch=master)](https://travis-ci.com/trevorblades/hapi-format-validation)\n\nThis [Hapi](https://hapijs.com/) plugin formats validation errors in a way that is consistent, simple, and easy to render in client-side forms. Take your typical [Joi](https://github.com/hapijs/joi) validation error reply for instance...\n\n#### Before `hapi-format-validation` 😿\n\n```js\n{\n  statusCode: 400,\n  error: 'Bad Request',\n  // this message should get formatted before displaying it to a user\n  message: 'child \"name\" fails because [\"name\" is not allowed to be empty]. child \"email\" fails because [\"email\" must be a valid email]',\n  validation: {\n    source: 'payload',\n    // extra work required of the client to link these keys to error messages :(\n    keys: [\n      'name',\n      'email'\n    ]\n  }\n}\n```\n\n#### After `hapi-format-validation` 😍\n\n```js\n{\n  statusCode: 400,\n  error: 'Bad Request',\n  // a newline-separated string, ready-to-use if necessary\n  message: '\"name\" is not allowed to be empty↵\"email\" must be a valid email',\n  validation: {\n    source: 'payload',\n    // a simple key-value mapping of fields and their errors\n    errors: {\n      name: '\"name\" is not allowed to be empty',\n      email: '\"email\" must be a valid email'\n    }\n  }\n}\n```\n\n## Installation\n\n```shell\n$ npm install --save hapi-format-validation\n```\n\n## Usage\n\n```js\nconst FormatValidation = require('hapi-format-validation');\n\nserver.register(FormatValidation, err =\u003e {\n  // server fun times\n});\n```\n\n## Options\n\n- **`stripQuotes`**: (optional) if `true`, strips double quotation marks from around the path name in error messages\n- **`capitalize`**: (optional) if `true`, capitalizes the first letter of each error message\n- **`sequelize`**: (optional) pass a `Sequelize` instance to format unique key violations (more information below)\n\n## Sequelize integration\n\n`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](http://docs.sequelizejs.com/)) as an option to the plugin when you register it to enable this feature.\n\n```js\nconst FormatValidation = require('hapi-format-validation');\nconst Sequelize = require('sequelize');\n\nconst sequelize = new Sequelize(...);\n\nserver.register(\n  {\n    register: FormatValidation,\n    options: {sequelize}\n  },\n  err =\u003e {\n    // do your server stuff\n  }\n);\n```\n\n#### Before\n\n```js\n{\n  statusCode: 500,\n  error: 'Internal Server Error',\n  message: 'An internal server error occurred'\n}\n```\n\n#### After\n\n```js\n{\n  'statusCode': 400,\n  'error': 'Bad Request',\n  'message': '\"username\" must be unique',\n  'validation': {\n    'source': 'payload',\n    'errors': {\n      'username': '\"username\" must be unique'\n    }\n  }\n}\n```\n\n## Acknowledgements 👊\n\n- [joi-errors-for-forms](https://github.com/eddyystop/joi-errors-for-forms) for the validation payload format inspiration\n- [This article](https://medium.com/@andv/hapi-transforming-an-internal-server-error-occured-into-correct-boom-errors-1a2a72e6ffff) by [Andrey Viktorov](https://medium.com/@andv) for the idea around using the `onPreResponse` hook and checking for Sequelize errors\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrevorblades%2Fhapi-format-validation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrevorblades%2Fhapi-format-validation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrevorblades%2Fhapi-format-validation/lists"}