# gavel

> Validator of HTTP transactions (JavaScript implementation)

Latest version **10.0.4** (published 2021-12-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install gavel
pnpm add gavel
yarn add gavel
bun add gavel
```

Provides the command `gavel`.

## 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 | 10.0.4 |
| Published | 2021-12-09 |
| First published | 2013-07-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 10.18 |
| Dependencies | 8 |
| Unpacked size | 47.3 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 97 |
| Author | Apiary Czech Republic, s.r.o. |
| Maintainers | kubakubula, apiary-sre |
| Keywords | http, validation, diff, request, response, gavel |

## Links

- npm: https://www.npmjs.com/package/gavel
- Repository: https://github.com/apiaryio/gavel.js
- Homepage: https://github.com/apiaryio/gavel.js#readme
- Issues: https://github.com/apiaryio/gavel.js/issues
- npm.io page: https://npm.io/package/gavel

## Dependencies (8)

- [ajv](https://npm.io/package/ajv.md) 6.12.6
- [tv4](https://npm.io/package/tv4.md) 1.3.0
- [commander](https://npm.io/package/commander.md) 8.1.0
- [deep-equal](https://npm.io/package/deep-equal.md) 2.0.5
- [media-typer](https://npm.io/package/media-typer.md) 1.1.0
- [content-type](https://npm.io/package/content-type.md) 1.0.4
- [curl-trace-parser](https://npm.io/package/curl-trace-parser.md) 0.0.10
- [http-string-parser](https://npm.io/package/http-string-parser.md) 0.0.6

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 10.0.4 (latest) — 2021-12-09
- 10.0.3 — 2021-05-11
- 10.0.2 — 2021-03-26
- 10.0.1 — 2021-01-25
- 10.0.0 — 2021-01-22
- 9.1.5 — 2020-08-13
- 9.1.4 — 2020-08-13
- 9.1.3 — 2020-06-02
- 9.1.2 — 2020-04-24
- 9.1.1 — 2020-02-05
- 9.1.0 — 2020-02-04
- 9.0.0 — 2020-01-30
- 8.2.3 — 2020-01-24
- 8.2.2 — 2019-12-13
- 8.2.1 — 2019-12-09
- … 69 more at https://npm.io/package/gavel/versions

## README

<p align="center">
  <a href="https://badge.fury.io/js/gavel" target="_blank">
    <img src="https://badge.fury.io/js/gavel.svg" alt="npm version" />
  </a>
  <a href="https://snyk.io/test/npm/gavel" target="_blank">
    <img src="https://snyk.io/test/npm/gavel/badge.svg" alt="Known Vulnerabilities" />
  </a>
</p>

<br />

<p align="center">
  <img src="https://raw.githubusercontent.com/apiaryio/gavel/master/img/gavel.png?v=1" alt="Gavel logo" />
</p>

<h1 align="center">Gavel</h1>

<p align="center">Gavel tells you whether an actual HTTP message is valid against an expected HTTP message.</p>

## Install

```bash
npm install gavel
```

## Usage

### CLI

```bash
# (Optional) Record HTTP messages
curl -s --trace - http://httpbin.org/ip | curl-trace-parser > expected
curl -s --trace - http://httpbin.org/ip | curl-trace-parser > actual

# Perform the validation
cat actual | gavel expected
```

> **Gavel CLI is not supported on Windows**. Example above uses [`curl-trace-parser`](https://github.com/apiaryio/curl-trace-parser).

### NodeJS

```js
const gavel = require('gavel');

// Define HTTP messages
const expected = {
  statusCode: 200,
  headers: {
    'Content-Type': 'application/json'
  }
};

const actual = {
  statusCode: 404,
  headers: {
    'Content-Type': 'application/json'
  }
};

// Perform the validation
const result = gavel.validate(expected, actual);
```

The code above would return the following validation `result`:

```js
{
  valid: false,
  fields: {
    statusCode: {
      valid: false,
      kind: 'text',
      values: {
        expected: '200',
        actual: '404'
      },
      errors: [
        {
          message: `Expected status code '200', but got '404'.`
        }
      ]
    },
    headers: {
      valid: true,
      kind: 'json',
      values: {
        expected: {
          'Content-Type': 'application/json'
        },
        actual: {
          'Content-Type': 'application/json'
        }
      },
      errors: []
    }
  }
}
```

### Usage with JSON Schema

> When a parsable JSON body is expected without an explicit schema the [default schema](https://github.com/apiaryio/gavel-spec/blob/master/features/expectations/bodyJsonExample.feature) is inferred.

You can describe the body expectations using [JSON Schema](https://json-schema.org/) by providing a valid schema to the `bodySchema` property of the expected HTTP message:

```js
const gavel = require('gavel');

const expected = {
  bodySchema: {
    type: 'object',
    properties: {
      fruits: {
        type: 'array',
        items: {
          type: 'string'
        }
      }
    }
  }
};

const actual = {
  body: JSON.stringify({
    fruits: ['apple', 'banana', 2]
  })
};

const result = gavel.validate(expected, actual);
```

The validation `result` against the given JSON Schema will look as follows:

```js
{
  valid: false,
  fields: {
    body: {
      valid: false,
      kind: 'json',
      values: {
        actual: "{\"fruits\":[\"apple\",\"banana\",2]}"
      },
      errors: [
        {
          message: `At '/fruits/2' Invalid type: number (expected string)`,
          location: {
            pointer: '/fruits/2'
          }
        }
      ]
    }
  }
}
```

### Supported JSON Schema versions

- [JSON Schema Draft 7](https://json-schema.org/specification-links.html#draft-7)
- [JSON Schema Draft 6](https://json-schema.org/specification-links.html#draft-6)
- [JSON Schema Draft 4](https://json-schema.org/specification-links.html#draft-4)

## Examples

Take a look at the [Gherkin](https://cucumber.io/docs/gherkin/) specification, which describes on examples how validation of each field behaves:

- [`method`](https://github.com/apiaryio/gavel-spec/blob/master/features/javascript/fields/method.feature)
- [`uri`](https://github.com/apiaryio/gavel-spec/blob/master/features/javascript/fields/uri.feature)
- [`statusCode`](https://github.com/apiaryio/gavel-spec/blob/master/features/javascript/fields/statusCode.feature)
- [`headers`](https://github.com/apiaryio/gavel-spec/blob/master/features/javascript/fields/headers.feature)
- [`body`](https://github.com/apiaryio/gavel-spec/blob/master/features/javascript/fields/body.feature)
- [`bodySchema`](https://github.com/apiaryio/gavel-spec/blob/master/features/javascript/fields/bodySchema.feature)

## Type definitions

Gavel ships with [TypeScript type definitions](./typings.d.ts). Please refer to the definitions file for more details.

## API

- `validate(expected: HttpMessage, actual: HttpMessage): ValidationResult`

## License

[MIT](LICENSE)

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