|
| 1 | +<h1 align="center"> |
| 2 | + next-joi |
| 3 | +</h1> |
| 4 | + |
| 5 | +<p align="center"> |
| 6 | + Validate NEXT.js API Routes with <em>joi</em> 😄 |
| 7 | +</p> |
| 8 | + |
| 9 | +- [Install](#install) |
| 10 | +- [Getting started](#getting-started) |
| 11 | + - [How does it work?](#how-does-it-work) |
| 12 | + - [Working with NEXT.js API Routes](#working-with-nextjs-api-routes) |
| 13 | + - [NEXT.js & `connect`-like middlewares](#nextjs--connect-like-middlewares) |
| 14 | +- [API](#api) |
| 15 | + - [`validate(schemas, handler)`](#validateschemas-handler) |
| 16 | + - [`schemas`](#schemas) |
| 17 | + - [`schemas.body`](#schemasbody) |
| 18 | + - [`schemas.query`](#schemasquery) |
| 19 | + - [`handler`](#handler) |
| 20 | + |
| 21 | +## Install |
| 22 | + |
| 23 | +``` |
| 24 | +yarn add next-joi |
| 25 | +``` |
| 26 | + |
| 27 | +This 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. |
| 28 | + |
| 29 | +## Getting started |
| 30 | + |
| 31 | +### How does it work? |
| 32 | + |
| 33 | +The `validate` function will check the incoming request against the defined validation schemas. If the request does not comply with the schemas, it will be aborted inmediately and a `400 BAD REQUEST` response will be returned. |
| 34 | + |
| 35 | +### Working with NEXT.js API Routes |
| 36 | + |
| 37 | +If you are using standard NEXT.js API Routes, you may use the `validate` function to wrap your route definition and pass |
| 38 | +along the validation schema: |
| 39 | + |
| 40 | +```ts |
| 41 | +import Joi from "joi"; |
| 42 | +import validate from "next-joi"; |
| 43 | + |
| 44 | +const schema = Joi.object({ |
| 45 | + birthdate: Joi.date().iso(), |
| 46 | + email: Joi.string().email().required(), |
| 47 | + name: Joi.string().required(), |
| 48 | +}); |
| 49 | + |
| 50 | +export default validate({ body: schema }, (req, res) => { |
| 51 | + // This function will be only executed if the incoming request complies |
| 52 | + // with the validation schema defined above. |
| 53 | +}); |
| 54 | +``` |
| 55 | + |
| 56 | +### NEXT.js & `connect`-like middlewares |
| 57 | + |
| 58 | +If your routes are powered by using a package such as `next-connect`, you can still use `validate`! |
| 59 | +The function is ready to work as a `connect` middleware just out-of-the-box: |
| 60 | + |
| 61 | +```ts |
| 62 | +import Joi from "joi"; |
| 63 | +import connect from "next-connect"; |
| 64 | +import validate from "next-joi"; |
| 65 | + |
| 66 | +const schema = Joi.object({ |
| 67 | + birthdate: Joi.date().iso(), |
| 68 | + email: Joi.string().email().required(), |
| 69 | + name: Joi.string().required(), |
| 70 | +}); |
| 71 | + |
| 72 | +export default connect().post(validate({ body: schema }), (req, res) => { |
| 73 | + // This function will be only executed if the incoming request complies |
| 74 | + // with the validation schema defined above. |
| 75 | +})) |
| 76 | +``` |
| 77 | + |
| 78 | +## API |
| 79 | + |
| 80 | +### `validate(schemas, handler)` |
| 81 | + |
| 82 | +The `validate` function has support to check two request's fields: `query` and `body`. Independently from the route's |
| 83 | +definition mechanism (see examples above), the first argument for this function should always be an object with the |
| 84 | +desired validation schemas. |
| 85 | + |
| 86 | +#### `schemas` |
| 87 | + |
| 88 | +**Required** |
| 89 | + |
| 90 | +Even if empty, this argument is required. |
| 91 | + |
| 92 | +##### `schemas.body` |
| 93 | + |
| 94 | +**Optional** |
| 95 | + |
| 96 | +A valid `joi` schema. |
| 97 | + |
| 98 | +##### `schemas.query` |
| 99 | + |
| 100 | +**Optional** |
| 101 | + |
| 102 | +A valid `joi` schema. |
| 103 | + |
| 104 | +#### `handler` |
| 105 | + |
| 106 | +**Optional** |
| 107 | + |
| 108 | +A valid `next` API Route handler. If you are using the `validate` function without a `connect`-like middleware engine, this argument becomes mandatory. |
| 109 | + |
| 110 | +Example: |
| 111 | + |
| 112 | +```ts |
| 113 | +const handler = function (req: NextApiRequest, res: NextApiResponse) { |
| 114 | + // implementation |
| 115 | +}; |
| 116 | + |
| 117 | +export default validate({}, handler); |
| 118 | +``` |
0 commit comments