@apideck/better-ajv-errors

中文文档
0.3.7发布于 2026/03/28MITTypeScriptESM + CJS近 30 天下载 3565.7万

Human-friendly JSON Schema validation for APIs

原始使用文档
文档目录5 节
  1. Install
  2. Usage
  3. API
  4. betterAjvErrors
  5. Related

npm (scoped) npm GitHub Workflow Status

@apideck/better-ajv-errors 👮‍♀️

Human-friendly JSON Schema validation for APIs

  • Readable and helpful ajv errors
  • API-friendly format
  • Suggestions for spelling mistakes
  • Minimal footprint: 1.56 kB (gzip + minified)

better-ajv-errors output Example

Install

$ yarn add @apideck/better-ajv-errors

or

$ npm i @apideck/better-ajv-errors

Also make sure that you've installed ajv at version 8 or higher.

Usage

After validating some data with ajv, pass the errors to betterAjvErrors

import Ajv from 'ajv';
import { betterAjvErrors } from '@apideck/better-ajv-errors';

// Without allErrors: true, ajv will only return the first error
const ajv = new Ajv({ allErrors: true });

const valid = ajv.validate(schema, data);

if (!valid) {
  const betterErrors = betterAjvErrors({ schema, data, errors: ajv.errors });
}

API

betterAjvErrors

Function that formats ajv validation errors in a human-friendly format.

Parameters

  • options: BetterAjvErrorsOptions
    • errors: ErrorObject[] | null | undefined Your ajv errors, you will find these in the errors property of your ajv instance (ErrorObject is a type from the ajv package).
    • data: Object The data you passed to ajv to be validated.
    • schema: JSONSchema The schema you passed to ajv to validate against.
    • basePath?: string An optional base path to prefix paths returned by betterAjvErrors. For example, in APIs, it could be useful to use '{requestBody}' or '{queryParemeters}' as a basePath. This will make it clear to users where exactly the error occurred.

Return Value

  • ValidationError[] Array of formatted errors (properties of ValidationError below)
    • message: string Formatted error message
    • suggestion?: string Optional suggestion based on provided data and schema
    • path: string Object path where the error occurred (example: .foo.bar.0.quz)
    • context: { errorType: DefinedError['keyword']; [additionalContext: string]: unknown } errorType is error.keyword proxied from ajv. errorType can be used as a key for i18n if needed. There might be additional properties on context, based on the type of error.
  • atlassian/better-ajv-errors was the inspiration for this library. Atlassian's library is more focused on CLI errors, this library is focused on developer-friendly API error messages.

版本与发布记录

@apideck/better-ajv-errors 共发布 16 个版本,首次发布于 2021/05/14;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.3.7latest2026/03/287 个月前
0.3.62022/06/284 年前
0.3.52022/06/284 年前
0.3.42022/05/254 年前
0.3.32022/02/094 年前
0.3.22021/12/234 年前

关于 @apideck/better-ajv-errors 的常见问题

@apideck/better-ajv-errors 是什么?

Human-friendly JSON Schema validation for APIs

如何安装 @apideck/better-ajv-errors?

在项目目录运行 npm install @apideck/better-ajv-errors,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:ajv(>=8)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@apideck/better-ajv-errors 支持 TypeScript 吗?

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

@apideck/better-ajv-errors 支持 ESM 还是 CommonJS?

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

@apideck/better-ajv-errors 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=10。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@apideck/better-ajv-errors 可以商用吗?

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

@apideck/better-ajv-errors 的下载量有多少?

根据 npm 官方下载统计,@apideck/better-ajv-errors 在 2026/10/06 – 2026/11/04 这 30 天内被下载 35,657,223 次,日均约 1,188,574 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@apideck/better-ajv-errors 还在维护吗?

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