{"id":17236,"library":"gavel","title":"Gavel HTTP Transaction Validator","description":"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.","status":"maintenance","version":"10.0.4","language":"javascript","source_language":"en","source_url":"https://github.com/apiaryio/gavel.js","tags":["javascript","http","validation","diff","request","response","gavel","typescript"],"install":[{"cmd":"npm install gavel","lang":"bash","label":"npm"},{"cmd":"yarn add gavel","lang":"bash","label":"yarn"},{"cmd":"pnpm add gavel","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"While the README shows CJS, for modern TypeScript/ESM projects, use `import gavel from 'gavel';`. Gavel ships with TypeScript types.","wrong":"const gavel = require('gavel');","symbol":"gavel","correct":"import gavel from 'gavel';"},{"note":"The primary validation logic is exposed as a method on the default `gavel` object.","wrong":"import { validate } from 'gavel'; // Gavel does not expose named exports for core functions directly.","symbol":"gavel.validate","correct":"import gavel from 'gavel'; const result = gavel.validate(expected, actual);"},{"note":"For type-checking the validation result object, import `GavelValidationResult`.","symbol":"Gavel.ValidationResult","correct":"import type { GavelValidationResult } from 'gavel';"}],"quickstart":{"code":"import gavel from 'gavel';\n\nconst expected = {\n  statusCode: 200,\n  headers: {\n    'Content-Type': 'application/json'\n  },\n  bodySchema: {\n    type: 'object',\n    properties: {\n      message: { type: 'string' }\n    },\n    required: ['message']\n  }\n};\n\nconst actual = {\n  statusCode: 200,\n  headers: {\n    'Content-Type': 'application/json'\n  },\n  body: JSON.stringify({\n    message: 'Hello, world!'\n  })\n};\n\nconst result = gavel.validate(expected, actual);\nconsole.log(result);","lang":"typescript","description":"Demonstrates basic HTTP transaction validation, including status code, headers, and JSON body against a schema."},"warnings":[{"fix":"Ensure your Node.js environment is at least v10.18.0. Update Node.js if necessary.","message":"Gavel v10.0.0 increased the minimum required Node.js version to `v10.18`.","severity":"breaking","affected_versions":">=10.0.0"},{"fix":"For Windows users, use the Node.js API directly or a WSL (Windows Subsystem for Linux) environment for CLI usage.","message":"The Gavel CLI is explicitly stated as not supported on Windows environments.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Regularly update `gavel` to the latest patch version to incorporate critical security fixes. Perform routine dependency audits.","message":"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.","severity":"gotcha","affected_versions":">=10.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Use ESM `import gavel from 'gavel';` instead of `const gavel = require('gavel');`.","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`).","error":"ReferenceError: require is not defined in ES module scope"},{"fix":"Adjust the `expected.statusCode` value to match the anticipated actual status code, or verify that the actual response is indeed incorrect.","cause":"The actual HTTP response status code does not match the expected status code defined in the validation rules.","error":"Expected status code '200', but got '404'."},{"fix":"Modify the `actual.body` to match the expected JSON Schema structure and data types, or refine the `expected.bodySchema` to accurately reflect valid inputs.","cause":"The actual HTTP body data does not conform to the JSON Schema provided in `expected.bodySchema` at the specified JSON Pointer path.","error":"At '/fruits/2' Invalid type: number (expected string)"}],"ecosystem":"npm","meta_description":null}