@ehuelsmann/openapi-validator

中文文档
0.19.2发布于 2026/11/01MITTypeScriptESM + CJS近 30 天下载 2027

Common code for jest-openapi and Chai OpenAPI Response Validator

原始使用文档
文档目录6 节
  1. Contributing ✨
  2. Installation
  3. Importing
  4. Usage
  5. Loading an OpenAPI spec
  6. Validating a response

OpenAPI Validator

downloads npm build status style codecov contributions welcome

Note This project is a fork from the openapi-library project because that project is not maintained at the moment. This project contains updates and security fixes, but ideally will be merged back into that project, as soon as it's maintained again.

Common code for @ehuelsmann/jest-openapi and Chai OpenAPI Response Validator

Contributing ✨

If you've come here to help contribute - thanks! Take a look at the contributing docs to get started.

Installation

npm

npm install --save-dev @ehuelsmann/openapi-validator

yarn

yarn add --dev @ehuelsmann/openapi-validator

Importing

This package ships with both ESM (preferred) and CommonJS builds, so it works in any environment.

ESM / TypeScript

import { makeApiSpec, makeResponse } from '@ehuelsmann/openapi-validator';

CommonJS / JavaScript

const { makeApiSpec, makeResponse } = require('@ehuelsmann/openapi-validator');

Usage

This package provides the shared validation logic used by @ehuelsmann/jest-openapi and @ehuelsmann/chai-openapi-response-validator. Use it to build your own OpenAPI validation integration.

Loading an OpenAPI spec

makeApiSpec accepts either an absolute filepath or an object representing an OpenAPI (v2 or v3) document:

import { makeApiSpec } from '@ehuelsmann/openapi-validator';

// From an absolute filepath (YAML or JSON)
const spec = makeApiSpec('/absolute/path/to/openapi.yml');

// From an object
const spec = makeApiSpec({
  openapi: '3.0.0',
  info: { title: 'Example API', version: '1.0.0' },
  paths: {},
});

Validating a response

makeResponse wraps an HTTP response from axios, supertest, superagent, or chai-http so it can be validated against the loaded spec:

import { makeApiSpec, makeResponse } from '@ehuelsmann/openapi-validator';

const spec = makeApiSpec('/absolute/path/to/openapi.yml');

// res is an axios, supertest, superagent, or chai-http response object
const response = makeResponse(res);
const validationError = spec.validateResponse(response);
if (validationError) {
  throw new Error(validationError.message);
}

版本与发布记录

@ehuelsmann/openapi-validator 共发布 14 个版本,首次发布于 2026/04/11;过去 12 个月发布了 14 个版本。

版本发布日期距今
0.19.2latest2026/11/0113 天前
0.19.12026/10/031 个月前
0.19.02026/09/042 个月前
0.18.22026/08/202 个月前
0.18.12026/08/202 个月前
0.18.02026/06/135 个月前

关于 @ehuelsmann/openapi-validator 的常见问题

@ehuelsmann/openapi-validator 是什么?

Common code for jest-openapi and Chai OpenAPI Response Validator

如何安装 @ehuelsmann/openapi-validator?

在项目目录运行 npm install @ehuelsmann/openapi-validator,或使用 pnpm add / yarn add / bun add。

@ehuelsmann/openapi-validator 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ehuelsmann/openapi-validator 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@ehuelsmann/openapi-validator 可以商用吗?

@ehuelsmann/openapi-validator 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ehuelsmann/openapi-validator 的下载量有多少?

根据 npm 官方下载统计,@ehuelsmann/openapi-validator 在 2026/10/13 – 2026/11/11 这 30 天内被下载 2,027 次,日均约 68 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ehuelsmann/openapi-validator 还在维护吗?

最新版本 0.19.2 发布于 2026/11/01(13 天前);至今共发布 14 个版本,其中过去 12 个月发布了 14 个;首次发布于 2026/04/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。