Unverified Commit bace9b49 authored by Sergio's avatar Sergio Committed by GitHub

Add with-joi example (#25759)

* Add joi and next-joi example

* Add more README information

* Update examples/with-joi/package.json
Co-authored-by: 's avatarLee Robinson <me@leerob.io>

* Update all dependencies to latest
Co-authored-by: 's avatarLee Robinson <me@leerob.io>
parent 46e7658f
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# next.js
/.next/
/out/
# production
/build
# misc
.DS_Store
*.pem
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local
# vercel
.vercel
# Example application using [`joi`](https://github.com/sideway/joi) and [`next-joi`](https://github.com/codecoolture/next-joi)
This example shows how to validate Next.js API routes by using the `joi` and `next-joi` packages. It contains samples for both API routes using a [Connect middleware](https://github.com/hoangvvo/next-connect) (see `/api/people`) and regular API routes (see `/api/cars`).
## Deploy your own
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example):
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-joi&project-name=with-joi&repository-name=with-joi)
## How to use
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:
```bash
npx create-next-app --example with-joi with-joi-app
# or
yarn create next-app --example with-joi with-joi-app
```
Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
{
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"joi": "latest",
"next": "latest",
"next-connect": "latest",
"next-joi": "latest",
"react": "latest",
"react-dom": "latest"
}
}
import Joi from 'joi'
import { validate } from '../../server/api/middlewares/validate'
const carSchema = Joi.object({
brand: Joi.string().required(),
model: Joi.string().required(),
})
export default validate({ body: carSchema }, (req, res) => {
const car = req.body
return res.status(201).json({ data: car })
})
import Joi from 'joi'
import connect from 'next-connect'
import { validate } from '../../server/api/middlewares/validate'
const personSchema = Joi.object({
age: Joi.number().required(),
name: Joi.string().required(),
})
export default connect().post(validate({ body: personSchema }), (req, res) => {
const person = req.body
return res.status(201).json({ data: person })
})
import Joi from 'joi'
import connect from 'next-connect'
import { validate } from '../../../server/api/middlewares/validate'
export default connect().get(
validate({
query: Joi.object({
id: Joi.string().uuid().required(),
}),
}),
(req, res) => {
const id = req.query.id
return res.status(200).json({
data: {
id,
},
})
}
)
import withJoi from 'next-joi'
export const validate = withJoi({
/**
* By default, `next-joi` will return a 400 HTTP error code but
* we can customize the error response.
*/
onValidationError: (_, res) => {
return res.status(418).json({ message: "I'm a Teapot" })
},
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment