# yup-express-middleware

> Express middleware for yup validation

Latest version **1.0.2** (published 2022-04-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install yup-express-middleware
pnpm add yup-express-middleware
yarn add yup-express-middleware
bun add yup-express-middleware
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2022-04-17 |
| First published | 2022-04-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 6.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Santhanam |
| Maintainers | porridgewithraisins |
| Keywords | yup, express, validation |

## Links

- npm: https://www.npmjs.com/package/yup-express-middleware
- Repository: https://github.com/porridgewithraisins/yup-express-middleware
- Homepage: https://github.com/porridgewithraisins/yup-express-middleware#readme
- Issues: https://github.com/porridgewithraisins/yup-express-middleware/issues
- npm.io page: https://npm.io/package/yup-express-middleware

## Dependencies (1)

- [yup](https://npm.io/package/yup.md) ^0.32.11

## Alternatives

- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@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

## Recent versions

- 1.0.2 (latest) — 2022-04-17
- 1.0.1 — 2022-04-17
- 1.0.0 — 2022-04-17

## README

# Yup express middleware

Offers [Yup](https://github.com/jquense/yup) schema validation for Express with first class typescript support.

`npm i yup-express-middleware`

or

`yarn add yup-express-middleware`

## Features

1. `req.body` and/or `req.query` can be validated
2. Yup transformations supported
3. `req.body` and/or `req.query` will be of the type of your schema in your controller!
4. No messy try-catch validation handling in your controllers, this library will automatically call express' error route handler (optionally with a custom error you can define).

## Usage

```ts
import { validation } from "yup-express-middleware";
import * as yup from "yup";
import express from "express";

const app = express();

const schema = yup.object().shape({
    name: yup.string().required(),
    age: yup
        .number()
        .required()
        .transform(val => val + 2),
});

const schema2 = yup.object({ bla: yup.object({ bloo: yup.string() }) });

app.post("/", validation({ body: schema, query: schema2 }), (req, res, next) => {
    const { name, age } = req.body;
    //  string^   ^number => validated, transformed (if the schema did so) and typed correctly

    const {
        bla: { bloo }, // bloo is typed correctly as string
    } = req.query;
});
```

When validation fails

```ts
class HttpError extends Error {
    constructor(message: string, public status: number) {
        super(message);
    }
}

class BadRequestError extends HttpError {
    constructor(message: string) {
        super(message, 400);
    }
}

app.post(
    "/",
    validation(
        { body: schema },
        { yupOptions: { abortEarly: false }, ErrorClass: BadRequestError }
    ),
    (req, res, next) => {
        // If validation fails, this won't get called
        // the middleware will call `next(new BadRequestError("the message provided by yup"))`
        // and you will have to catch that in your express error handler.
    }
);

// for example

app.use(((err, req, res, next) => {
    if (res.headersSent) next(err);
    if (err.name === "BadRequestError") {
        res.status(err.status).json({
            reason: "Validation Error",
            error: err.message,
        });
    }
}) as ErrorRequestHandler);
```

## API

```

validation(schema, options);

schema:
    body: (optional) yup schema
    query: (optional) yup schema

options:

    yupOptions: Options to pass to yup.validate()

    ErrorClass: The error class to throw when validation fails (defaults to `Error`)

    finallyFunction: the function to call regardless of whether validation succeeds or fails)

```

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