{"id":13423814,"url":"https://github.com/codecoolture/next-joi","last_synced_at":"2025-03-15T17:32:21.717Z","repository":{"id":37991831,"uuid":"285603342","full_name":"codecoolture/next-joi","owner":"codecoolture","description":"Validate NEXT.js API Routes with joi","archived":false,"fork":false,"pushed_at":"2022-06-12T18:07:24.000Z","size":319,"stargazers_count":111,"open_issues_count":5,"forks_count":5,"subscribers_count":3,"default_branch":"trunk","last_synced_at":"2025-02-18T22:38:37.414Z","etag":null,"topics":["joi","middleware","middlewares","nextjs","nodejs","react","typescript","validation","validation-schema","vercel"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codecoolture.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-06T15:15:28.000Z","updated_at":"2025-02-11T21:35:05.000Z","dependencies_parsed_at":"2022-07-12T00:17:23.586Z","dependency_job_id":null,"html_url":"https://github.com/codecoolture/next-joi","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codecoolture%2Fnext-joi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codecoolture%2Fnext-joi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codecoolture%2Fnext-joi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codecoolture%2Fnext-joi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codecoolture","download_url":"https://codeload.github.com/codecoolture/next-joi/tar.gz/refs/heads/trunk","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243767308,"owners_count":20344904,"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":["joi","middleware","middlewares","nextjs","nodejs","react","typescript","validation","validation-schema","vercel"],"created_at":"2024-07-31T00:00:43.087Z","updated_at":"2025-03-15T17:32:21.690Z","avatar_url":"https://github.com/codecoolture.png","language":"TypeScript","readme":"\u003ch1 align=\"center\"\u003e\n  next-joi\n\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n  Validate NEXT.js API Routes with \u003cem\u003ejoi\u003c/em\u003e 😄\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://github.com/codecoolture/next-joi/workflows/test/badge.svg?branch=trunk\" alt=\"github action badge\"\u003e\n\u003c/p\u003e\n\n- [Install](#install)\n- [Getting started](#getting-started)\n  - [How does it work?](#how-does-it-work)\n  - [Working with NEXT.js API Routes](#working-with-nextjs-api-routes)\n  - [NEXT.js \u0026 `connect`-like middlewares](#nextjs--connect-like-middlewares)\n- [API](#api)\n  - [`withJoi(config?) =\u003e validate`](#withjoiconfig--validate)\n    - [`config`](#config)\n      - [`config.onValidationError`](#configonvalidationerror)\n  - [`validate(schemas, handler)`](#validateschemas-handler)\n    - [`schemas`](#schemas)\n      - [`schemas.body`](#schemasbody)\n      - [`schemas.headers`](#schemasheaders)\n      - [`schemas.query`](#schemasquery)\n    - [`handler`](#handler)\n\n## Install\n\n```\nyarn add next-joi\n```\n\nThis package does not bundle with [`next.js`](https://github.com/vercel/next.js) or [`joi`](https://github.com/sideway/joi), so you will need to install them separately.\n\n## Getting started\n\n### How does it work?\n\nThe validation function will check the incoming request against the defined validation schemas. If the request does not comply with the schemas, it will be aborted immediately, and (by default) a `400 BAD REQUEST` response will be returned. It is possible to customize this error handling by passing a custom `onValidationError` function to the primary factory function.\n\n**lib/middlewares/validation.ts**\n\n```ts\nimport withJoi from \"next-joi\";\n\nexport default withJoi({\n  onValidationError: (_, res) =\u003e {\n    res.status(400).end();\n  },\n});\n```\n\n### Working with NEXT.js API Routes\n\nIf you are using standard NEXT.js API Routes, you may use the validation function to wrap your route definition and pass\nalong the validation schema:\n\n```ts\nimport Joi from \"joi\";\n\nimport validate from \"/lib/middlewares/validation\";\n\nconst schema = Joi.object({\n  birthdate: Joi.date().iso(),\n  email: Joi.string().email().required(),\n  name: Joi.string().required(),\n});\n\nexport default validate({ body: schema }, (req, res) =\u003e {\n  // This function will be only executed if the incoming request complies\n  // with the validation schema defined above.\n});\n```\n\n### NEXT.js \u0026 `connect`-like middlewares\n\nIf your routes are powered by using a package such as `next-connect`, you can still use `next-joi`!\nThe middleware function is ready to work with `connect` just out-of-the-box:\n\n```ts\nimport Joi from \"joi\";\nimport connect from \"next-connect\";\n\nimport validate from \"/lib/middlewares/validation\";\n\nconst schema = Joi.object({\n  birthdate: Joi.date().iso(),\n  email: Joi.string().email().required(),\n  name: Joi.string().required(),\n});\n\nexport default connect().post(validate({ body: schema }), (req, res) =\u003e {\n  // This function will be only executed if the incoming request complies\n  // with the validation schema defined above.\n}))\n```\n\n## API\n\n### `withJoi(config?) =\u003e validate`\n\nThis factory function may optionally receive a configuration object. It will return the actual validation function (`validate`) that can be used as API route middleware.\n\n#### `config`\n\n**Optional**\n\nIf omitted, `next-joi` will use a default configuration.\n\n##### `config.onValidationError`\n\n**Required**\n\nCustom error function to handle validation errors. It will receive the API request, response, and [validation error](https://joi.dev/api/?v=17.4.0#validationerror).\n\n```ts\nimport withJoi from \"next-joi\";\n\nexport default withJoi({\n  onValidationError: (req, res, error) =\u003e {\n    res.status(400).end();\n  },\n});\n```\n\n### `validate(schemas, handler)`\n\nThe `validate` function has support to check the following request fields: `body`, `headers` and `query`. The first argument for this function should always be an object with the desired validation schemas.\n\n#### `schemas`\n\n**Required**\n\nEven if empty, this argument is required.\n\n##### `schemas.body`\n\n**Optional**\n\nA valid `joi` schema.\n\n##### `schemas.headers`\n\n**Optional**\n\n\u003e Note: since most of the time, you may receive more headers than expected, it is a good practice to make this\n\u003e schema always support [`unknown`](https://joi.dev/api/?v=17.3.0#objectunknownallow) keys. Otherwise, the validation\n\u003e will fail.\n\nA valid `joi` schema.\n\n##### `schemas.query`\n\n**Optional**\n\nA valid `joi` schema.\n\n#### `handler`\n\n**Optional**\n\nA valid `next` API Route handler. If you are using the `validate` function without a `connect`-like middleware engine, this argument becomes mandatory.\n\nExample:\n\n```ts\nconst handler = function (req: NextApiRequest, res: NextApiResponse) {\n  // implementation\n};\n\nexport default validate({}, handler);\n```\n","funding_links":[],"categories":["Extensions","TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodecoolture%2Fnext-joi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodecoolture%2Fnext-joi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodecoolture%2Fnext-joi/lists"}