Skip to content

Commit

Permalink
docs: add body validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Toshimichi0915 committed Jun 17, 2023
1 parent b280c35 commit 940da82
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ npm install next-pipe
- - [Method Routing](#method-routing)
- - [Next-Auth](#next-auth)
- - [Iron-Session](#iron-session)
- - [Body Validation](#body-validation)

## Basic Usage

Expand Down Expand Up @@ -205,3 +206,27 @@ export default middleware<NextApiRequest, NextApiResponse>()
res.send(`Your session is: ${JSON.stringify(session)}`)
})
```

## Body Validation

This adapter validates the request body with zod or yup.

```typescript
export function withValidatedBody<TReq extends { body?: unknown }, TRes extends ServerResponse, T>(parser: Parser<T>)
```

```typescript
import { NextApiRequest, NextApiResponse } from "next"
import { z } from "zod"
import { middleware, withValidatedBody } from "next-pipe"
const schema = z.object({
name: z.string(),
})
export default middleware<NextApiRequest, NextApiResponse>()
.pipe(withValidatedBody(schema))
.pipe((req, res, next, data) => {
res.status(200).json({ message: `Hello, ${data.name}!` })
})
```

0 comments on commit 940da82

Please sign in to comment.