# @expresso/validator

> JSON Schema route validation middleware for Expresso

Latest version **1.1.7** (published 2020-11-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install @expresso/validator
pnpm add @expresso/validator
yarn add @expresso/validator
bun add @expresso/validator
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.7 |
| Published | 2020-11-24 |
| First published | 2019-03-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 18.3 KB |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Lucas Santos |
| Maintainers | rjmunhoz, khaosdoctor |
| Keywords | expresso, json-schema, validation, express |

## Links

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

## Dependencies (3)

- [ajv](https://npm.io/package/ajv.md) 6.12.0
- [@hapi/boom](https://npm.io/package/@hapi/boom.md) ^9.1.0
- [typescript-json-schema](https://npm.io/package/typescript-json-schema.md) 0.42.0

## 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.1.7 (latest) — 2020-11-24
- 1.1.6 — 2020-11-24
- 1.1.5 — 2020-03-04
- 1.1.4 — 2020-02-01
- 1.1.3 — 2020-01-31
- 1.1.2 — 2020-01-03
- 1.1.1 — 2019-12-04
- 1.1.0 — 2019-12-04
- 1.0.5 — 2019-08-06
- 1.0.4 — 2019-04-05
- 1.0.3 — 2019-03-30
- 1.0.2 — 2019-03-30
- 1.0.1 — 2019-03-29
- 1.0.0 — 2019-03-28

## README

# Expresso Validator

> JSON Schema validation middleware for [Expresso](https://github.com/expresso)

## Summary

- [Expresso Validator](#expresso-validator)
  - [Summary](#summary)
  - [What is this](#what-is-this)
  - [Basic Usage](#basic-usage)
    - [Validating queries](#validating-queries)
    - [validateType function](#validatetype-function)
  - [Errors](#errors)

## What is this

This middleware validates an input against a [JSON Schema](https://json-schema.org) and automatically throws a boom error for `badData` (which is HTTP code `422 - Unprocessable Entity`) if this input is not matched.

## Basic Usage

**Install**:

```sh
$ npm i @expresso/validator
```

**Import and use**:

```ts
import { validate } from '@expresso/validator'

// Your expresso initialization here

const schema = {
  type: 'object',
  properties: {
    name: {
      type: 'string'
    },
    age: {
      type: 'integer'
    }
  },
  additionalProperties: false,
  required: ['name', 'age']
}

app.post('/users', validate(schema), (req: Reques, res: Response, next: NextFunction) => { // ... // })
```

### Validating queries

By default, the validator will match the schema against the body of the request, this is why the root type is `{ type: 'object' }`. It is also possible to validate the query string params:

```ts
import { validate } from '@expresso/validator'

// Your expresso initialization here

const schema = {
  type: 'object',
  properties: {
    name: {
      type: 'string'
    },
    age: {
      type: 'integer'
    }
  },
  required: ['name', 'age']
}

app.get('/users', validate.query(schema), (req: Reques, res: Response, next: NextFunction) => { // ... // })
```

This will validate the whole query string against the given schema.

### validateType function
`validateType` is actually a dummy function that should be removed in runtime. It exists to give type
safety for using [@expresso/ts-transformer-json-schema](https://npmjs.org/packages/@expresso/ts-transformer-json-schema)

## Errors

This validator follows Expresso's directives and returns a [Boom](https://github.com/hapijs/boom) error for BadData, right now it is not possible to change this behavior

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