Gavel HTTP Transaction Validator
Gavel is a JavaScript library designed for validating actual HTTP messages against expected HTTP messages. It provides a detailed comparison, indicating discrepancies in status codes, headers, and body content. The library supports JSON Schema validation (Draft 4, 6, and 7) for defining complex body expectations. The current stable version is 10.0.4, last released in December 2021. Its release cadence appears infrequent, with recent updates primarily focused on security patches for internal dependencies rather than new feature development. Gavel differentiates itself by offering granular validation results, pinpointing exact failures, including specific error messages and locations within JSON Schema mismatches, making it a valuable tool for API contract testing and robust HTTP interaction validation.
Common errors
-
ReferenceError: require is not defined in ES module scope
cause Attempting to use CommonJS `require()` in an ESM context (e.g., in a `.mjs` file or when `type: "module"` is set in `package.json`).fixUse ESM `import gavel from 'gavel';` instead of `const gavel = require('gavel');`. -
Expected status code '200', but got '404'.
cause The actual HTTP response status code does not match the expected status code defined in the validation rules.fixAdjust the `expected.statusCode` value to match the anticipated actual status code, or verify that the actual response is indeed incorrect. -
At '/fruits/2' Invalid type: number (expected string)
cause The actual HTTP body data does not conform to the JSON Schema provided in `expected.bodySchema` at the specified JSON Pointer path.fixModify the `actual.body` to match the expected JSON Schema structure and data types, or refine the `expected.bodySchema` to accurately reflect valid inputs.
Warnings
- breaking Gavel v10.0.0 increased the minimum required Node.js version to `v10.18`.
- gotcha The Gavel CLI is explicitly stated as not supported on Windows environments.
- gotcha Recent releases (v10.0.x) primarily consist of dependency security updates (e.g., `glob-parent`, `tar`, `lodash`). While fixes, frequent updates for transitive dependencies might indicate an older dependency tree or potential supply chain risks if not regularly patched.
Install
-
npm install gavel -
yarn add gavel -
pnpm add gavel
Imports
- gavel
const gavel = require('gavel');import gavel from 'gavel';
- gavel.validate
import { validate } from 'gavel'; // Gavel does not expose named exports for core functions directly.import gavel from 'gavel'; const result = gavel.validate(expected, actual);
- Gavel.ValidationResult
import type { GavelValidationResult } from 'gavel';
Quickstart
import gavel from 'gavel';
const expected = {
statusCode: 200,
headers: {
'Content-Type': 'application/json'
},
bodySchema: {
type: 'object',
properties: {
message: { type: 'string' }
},
required: ['message']
}
};
const actual = {
statusCode: 200,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
message: 'Hello, world!'
})
};
const result = gavel.validate(expected, actual);
console.log(result);