# middy-zod-validator

> Middleware to use zod validations with the middy middleware library

Latest version **3.0.4** (published 2023-03-15) · ISC license · 0 weekly downloads

## Install

```sh
npm install middy-zod-validator
pnpm add middy-zod-validator
yarn add middy-zod-validator
bun add middy-zod-validator
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.4 |
| Published | 2023-03-15 |
| First published | 2020-11-01 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 6.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | Luis Chodiman |
| Maintainers | lechodiman |
| Keywords | middy, zod, zod-validator, validator, serverless, aws |

## Links

- npm: https://www.npmjs.com/package/middy-zod-validator
- Repository: https://github.com/lechodiman/middy-zod-validator
- Homepage: https://github.com/lechodiman/middy-zod-validator#readme
- Issues: https://github.com/lechodiman/middy-zod-validator/issues
- npm.io page: https://npm.io/package/middy-zod-validator

## Dependencies (3)

- [zod](https://npm.io/package/zod.md) ^3.11.6
- [@middy/core](https://npm.io/package/@middy/core.md) ^2.5.7
- [http-errors](https://npm.io/package/http-errors.md) ^2.0.0

## Alternatives

- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads
- [@oridune/validator](https://npm.io/package/@oridune/validator.md) — 16 weekly downloads

## Recent versions

- 3.0.4 (latest) — 2023-03-15
- 3.0.3 — 2023-03-15
- 1.0.5 — 2020-11-11
- 1.0.4 — 2020-11-11
- 1.0.2 — 2020-11-11
- 1.0.1 — 2020-11-02
- 1.0.0 — 2020-11-01

## README

# Zod Validator

Validator middleware for the middy framework. It is similar to the `@middy/validator` package but with support for the awesome `zod` validation library.

<hr>

This middleware automatically validates incomming events using a given `zod` schema. It is specially useful for Serverless projects using Typescript. 💪

If an incoming event fails validation a `Bad Request` error is raised.

This middleware can be used in combination with `httpErrorHandler` to automatically return the right response to the user.

## Install

To install this middleware you can run:

```
npm i --save middy-zod-validator
```

## Options

- `schema`: (zod schema) (optional): The `zod` schema that will be used to parse the input (`handler.event`) of the Lambda handler. **It can be either a syncronous or an asyncronous schema**

## Sample usage

Example for input validation:

```typescript
import middy from '@middy/core';
import { zodValidator } from 'middy-zod-validator';

const handler = middy((event, context, cb) => {
  cb(null, {});
});

const schema = z.object({
  body: z.string() // this will pass validation
  foo: z.string() // this won't as it won't be in the event
})

handler.use(
  zodValidator(schema)
);

// invokes the handler, note that property foo is missing
const event = {
  body: JSON.stringify({ something: 'somethingelse' }),
};

handler(event, {}, (err, res) => {
  // err will be a BadRequestError
});
```

## Contributing

Everyone is welcome to contribute to this repository. Feel free to raise issues or submit Pull Requests.

---
_Source: https://npm.io/package/middy-zod-validator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
