# valid-oid

> Express middleware for validating a MongoDB ObjectId in the request path.

Latest version **2.0.7** (published 2023-04-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install valid-oid
pnpm add valid-oid
yarn add valid-oid
bun add valid-oid
```

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.7 |
| Published | 2023-04-22 |
| First published | 2021-10-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Paul Smith |
| Maintainers | merlin4 |
| Keywords | express, middleware, validate, validator, validation, objectid, oid |

## Links

- npm: https://www.npmjs.com/package/valid-oid
- Repository: https://github.com/paulsmithkc/express-packages
- Homepage: https://github.com/paulsmithkc/express-packages/tree/main/packages/valid-oid#readme
- Issues: https://github.com/paulsmithkc/express-packages/issues
- npm.io page: https://npm.io/package/valid-oid

## 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

- 2.0.7 (latest) — 2023-04-22
- 2.0.6 — 2023-04-22
- 2.0.5 — 2023-04-16
- 2.0.4 — 2023-04-09
- 2.0.3 — 2023-02-12
- 2.0.2 — 2023-02-12
- 2.0.1 — 2023-02-12
- 2.0.0 — 2023-02-12
- 1.0.0 — 2021-12-04
- 0.0.1 — 2021-10-06

## README

# valid-oid

Express middleware for validating a MongoDB ObjectId in the request path.

## Install

```bash
npm install valid-oid
```

## Usage

Import the middleware as below:

```js
const validId = require('valid-oid');
```

Install the middleware on a route:

```js
app.get('/api/product/id/:productId', validId('productId'), (req, res) => {
  const productId = req.productId;
  const product = getProductById(productId);
  if (!product) {
    return res.status(404).json({ error: `Product "${productId}" not found.` });
  } else {
    return res.json(product);
  }
});
```

## Implementation

1. The middleware looks for a path param with the provided name.

2. If the param exists and is valid, the validated ObjectId is added to the request, and control passes to the next middleware.

3. Otherwise, a 404 response is sent.

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