jest-validate

中文文档
30.5.1发布于 2026/09/01MITTypeScriptESM + CJS近 30 天下载 2.7亿

通用配置验证工具, 用于帮助您处理警告、 错误、 贬值消息以及显示用户正确配置的例子 .

中文使用文档
文档目录7 节
  1. 使用量
  2. API 密码
  3. 选项
  4. 示例
  5. 实例
  6. 实例验证多个类型
  7. 实例验证 CLI 参数

快速验证

通用配置验证工具, 用于帮助您处理警告、 错误、 贬值消息以及显示用户正确配置的例子 .

npm install --save jest-validate

使用量

import {validate} from 'jest-validate';

validate(config, validationOptions); // => {hasDeprecationWarnings: boolean, isValid: boolean}

何处 ValidationOptions 它们是:

type ValidationOptions = {
  comment?: string;
  condition?: (option: unknown, validOption: unknown) => boolean;
  deprecate?: (
    config: Record<string, unknown>,
    option: string,
    deprecatedOptions: DeprecatedOptions,
    options: ValidationOptions,
  ) => boolean;
  deprecatedConfig?: DeprecatedOptions;
  error?: (
    option: string,
    received: unknown,
    defaultValue: unknown,
    options: ValidationOptions,
    path?: Array<string>,
  ) => void;
  exampleConfig: Record<string, unknown>;
  recursive?: boolean;
  recursiveBlacklist?: Array<string>;
  recursiveDenylist?: Array<string>;
  title?: Title;
  unknown?: (
    config: Record<string, unknown>,
    exampleConfig: Record<string, unknown>,
    option: string,
    options: ValidationOptions,
    path?: Array<string>,
  ) => void;
};

type Title = {
  deprecation?: string;
  error?: string;
  warning?: string;
};

exampleConfig 唯一的选项.

API 密码

默认 jest-validate 将打印通用警告和错误消息。 您可以通过提供 options: ValidationOptions 对象作为第二个参数 :

几乎任何东西都可以被覆盖 以满足你的需求.

选项

  • recursiveDenylist – 字符串键盘的可选数组,应当排除于深(反向)验证.
  • comment — 在错误/ 警告消息下可选择的字符串 .
  • condition – 具有验证条件的可选函数.
  • deprecate, (中文(简体) ), error, (中文(简体) ), unknown - 用于显示警告和错误消息的可选功能.
  • deprecatedConfig – 带有已贬值配置密钥的可选对象.
  • exampleConfig - 唯一一个 所需经费 选项,您要测试其配置.
  • recursive - 确定是否递归比较的可选布尔 exampleConfig 改为 config (默认 : true) (中文(简体) ).
  • title - 错误和消息标题的可选对象.

你会找到实例 condition, (中文(简体) ), deprecate, (中文(简体) ), error, (中文(简体) ), unknown,以及 deprecatedConfig 此寄存器的内源, 分别命名 .

示例

exampleConfig 应为包含每个密钥的有效值示例的有密钥/值对的对象。 配置值在 :

  • 它与示例值的JavaScript类型相匹配,例如. string, (中文(简体) ), number, (中文(简体) ), array, (中文(简体) ), boolean, (中文(简体) ), function,或 object
  • 这是 null 或者说 undefined
  • 它符合传递给任何参数的 Javascript 类型 MultipleValidOptions(...)

最后一个条件是一个特殊的语法,允许在允许一个以上类型的情况下进行验证;参见下面的例子. 在示例中拥有多个同类型值是可以接受的,所以也可以使用这个语法提供多个实例. 当验证失败发生时,错误消息会将阵列中的所有其他值显示为示例.

实例

最小实例 :

validate(config, {exampleConfig});

略作修改的例子:

validate(config, {
  comment: '  Documentation: http://custom-docs.com',
  deprecatedConfig,
  exampleConfig,
  title: {
    deprecation: 'Custom Deprecation',
    // leaving 'error' and 'warning' as default
  },
});

这将输出 :

警告:

● Validation Warning:

  Unknown option transformx with value "<rootDir>/node_modules/babel-jest" was found.
  This is either a typing error or a user mistake. Fixing it will remove this message.

  Documentation: http://custom-docs.com

错误 :

● Validation Error:

  Option transform must be of type:
    object
  but instead received:
    string

  Example:
  {
    "transform": {
      "\\.js$": "<rootDir>/preprocessor.js"
    }
  }

  Documentation: http://custom-docs.com

实例验证多个类型

import {multipleValidOptions} from 'jest-validate';

validate(config, {
  // `bar` will accept either a string or a number
  bar: multipleValidOptions('string is ok', 2),
});

错误 :

● Validation Error:

  Option foo must be of type:
    string or number
  but instead received:
    array

  Example:
  {
    "bar": "string is ok"
  }

  or

  {
    "bar": 2
  }

  Documentation: http://custom-docs.com

折旧

依据 deprecatedConfig 对象,并带有适当的折旧信息。 注自定义标题 :

Custom Deprecation:

  Option scriptPreprocessor was replaced by transform, which support multiple preprocessors.

  Jest now treats your current configuration as:
  {
    "transform": {".*": "xxx"}
  }

  Please update your configuration.

  Documentation: http://custom-docs.com

实例验证 CLI 参数

import {validate} from 'jest-validate';

validateCLIOptions(argv, {...allowedOptions, deprecatedOptions});

若为 argv 包含一个未在 allowedOptions, (中文(简体) ), validateCLIOptions 将随信件在 deprecatedOptions 配置 :

● collectCoverageOnlyFrom:

  Option "collectCoverageOnlyFrom" was replaced by "collectCoverageFrom"

  CLI Options Documentation: https://jestjs.io/docs/en/cli.html

如果折旧选项仍列在 allowedOptions 配置,然后 validateCLIOptions 将打印警告而不显示错误.

版本与发布记录

jest-validate 共发布 237 个版本,首次发布于 2017/01/09;过去 12 个月发布了 5 个版本。

版本发布日期距今
30.5.1latest2026/09/012 个月前
30.5.02026/08/282 个月前
30.4.12026/05/086 个月前
30.4.02026/05/086 个月前
30.3.02026/03/107 个月前
30.2.02025/09/281.1 年前

其他发布通道:next → 30.0.0-rc.1。

关于 jest-validate 的常见问题

jest-validate 是什么?

通用配置验证工具, 用于帮助您处理警告、 错误、 贬值消息以及显示用户正确配置的例子 .

如何安装 jest-validate?

在项目目录运行 npm install jest-validate,或使用 pnpm add / yarn add / bun add。

jest-validate 支持 TypeScript 吗?

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

jest-validate 支持 ESM 还是 CommonJS?

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

jest-validate 需要什么版本的 Node.js?

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

jest-validate 可以商用吗?

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

jest-validate 的下载量有多少?

根据 npm 官方下载统计,jest-validate 在 2026/10/07 – 2026/11/05 这 30 天内被下载 274,172,992 次,日均约 9,139,100 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

jest-validate 还在维护吗?

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