schema-utils

中文文档
4.8.2发布于 2026/11/01MITTypeScriptCommonJS构建来源已验证近 30 天下载 4.6亿

webpack Validation Utils

原始使用文档
文档目录12 节
  1. Getting Started
  2. API
  3. schema
  4. options
  5. configuration
  6. Examples
  7. Loader
  8. Plugin
  9. Precompiled schemas
  10. Allow to disable and enable validation (the validate functi…
  11. Contributing
  12. License

npm node tests coverage GitHub Discussions size

schema-utils

Package for validate options in loaders and plugins.

Getting Started

To begin, you'll need to install schema-utils:

npm install schema-utils

API

schema.json

{
  "type": "object",
  "properties": {
    "option": {
      "type": "boolean"
    }
  },
  "additionalProperties": false
}
import { validate } from "schema-utils";
import schema from "./path/to/schema.json";

const options = { option: true };
const configuration = { name: "Loader Name/Plugin Name/Name" };

validate(schema, options, configuration);

schema

Type: String

JSON schema.

Simple example of schema:

{
  "type": "object",
  "properties": {
    "name": {
      "description": "This is description of option.",
      "type": "string"
    }
  },
  "additionalProperties": false
}

options

Type: Object

Object with options.

import { validate } from "schema-utils";
import schema from "./path/to/schema.json";

const options = { foo: "bar" };

validate(schema, { name: 123 }, { name: "MyPlugin" });

configuration

Allow to configure validator.

There is an alternative method to configure the name andbaseDataPath options via the title property in the schema. For example:

{
  "title": "My Loader options",
  "type": "object",
  "properties": {
    "name": {
      "description": "This is description of option.",
      "type": "string"
    }
  },
  "additionalProperties": false
}

The last word used for the baseDataPath option, other words used for the name option. Based on the example above the name option equals My Loader, the baseDataPath option equals options.

name

Type: Object Default: "Object"

Allow to setup name in validation errors.

import { validate } from "schema-utils";
import schema from "./path/to/schema.json";

const options = { foo: "bar" };

validate(schema, options, { name: "MyPlugin" });
Invalid configuration object. MyPlugin has been initialised using a configuration object that does not match the API schema.
 - configuration.optionName should be a integer.

baseDataPath

Type: String Default: "configuration"

Allow to setup base data path in validation errors.

import { validate } from "schema-utils";
import schema from "./path/to/schema.json";

const options = { foo: "bar" };

validate(schema, options, { name: "MyPlugin", baseDataPath: "options" });
Invalid options object. MyPlugin has been initialised using an options object that does not match the API schema.
 - options.optionName should be a integer.

postFormatter

Type: Function Default: undefined

Allow to reformat errors.

import { validate } from "schema-utils";
import schema from "./path/to/schema.json";

const options = { foo: "bar" };

validate(schema, options, {
  name: "MyPlugin",
  postFormatter: (formattedError, error) => {
    if (error.keyword === "type") {
      return `${formattedError}\nAdditional Information.`;
    }

    return formattedError;
  },
});
Invalid options object. MyPlugin has been initialized using an options object that does not match the API schema.
 - options.optionName should be a integer.
   Additional Information.

Examples

schema.json

{
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "test": {
      "anyOf": [
        { "type": "array" },
        { "type": "string" },
        { "instanceof": "RegExp" }
      ]
    },
    "transform": {
      "instanceof": "Function"
    },
    "sourceMap": {
      "type": "boolean"
    }
  },
  "additionalProperties": false
}

Loader

import schema from "path/to/schema.json";
import { getOptions } from "loader-utils";
import { validate } from "schema-utils";

function loader(src, map) {
  const options = getOptions(this);

  validate(schema, options, {
    name: "Loader Name",
    baseDataPath: "options",
  });

  // Code...
}

export default loader;

Plugin

import schema from "path/to/schema.json";
import { validate } from "schema-utils";

class Plugin {
  constructor(options) {
    validate(schema, options, {
      name: "Plugin Name",
      baseDataPath: "options",
    });

    this.options = options;
  }

  apply(compiler) {
    // Code...
  }
}

export default Plugin;

Precompiled schemas

Compiling a schema at runtime loads ajv, which costs about 150ms for the first schema of a process and a few to tens of milliseconds for every other one. A loader or a plugin can compile its schema ahead of time instead, into a check that validate takes as its fourth argument:

npx schema-utils precompile src/options.json

This writes src/options.check.js next to the schema. Commit it, and publish it with the package:

import { validate } from "schema-utils";
import check from "./options.check.js";
import schema from "./options.json";

validate(schema, options, { name: "Loader Name" }, check);

When the options are valid, the check is all that runs and ajv is never loaded. When they are not, validate runs the full validation to report what is wrong, so the errors are the same as without a check.

Run schema-utils precompile --check src/options.json in CI to fail when the check is missing or out of date with its schema. The generated module may need schema-utils as a dependency of the package. It is an ES module when the nearest package.json has "type": "module", the way Node.js reads a .js file there, and CommonJS otherwise.

With --minify, the check drops the error objects it never reports, has its helpers written into it instead of loaded from schema-utils, and is minified with webpack's own minifier, which needs webpack 5.112.0 or later. For webpack's own options that is less than half the size and loads in 13ms instead of 21ms. --check has to be given the same --minify to compare against the right code.

A relative $ref to another file resolves against the schema. To compile a schema from code, use precompile(schema, { loadSchema, esm, minify }), it returns the source of the module.

Allow to disable and enable validation (the validate function do nothing)

This can be useful when you don't want to do validation for production builds.

import { disableValidation, enableValidation, validate } from "schema-utils";

// Disable validation
disableValidation();
// Do nothing
validate(schema, options);

// Enable validation
enableValidation();
// Will throw an error if schema is not valid
validate(schema, options);

// Allow to undestand do you need validation or not
const need = needValidate();

console.log(need);

Also you can enable/disable validation using the process.env.SKIP_VALIDATION env variable.

Supported values (case insensitive):

  • yes/y/true/1/on
  • no/n/false/0/off

The variable is read when schema-utils is loaded, so set it before starting the process:

SKIP_VALIDATION=y webpack

Use enableValidation()/disableValidation() to change it while the process is running - they take effect immediately and apply to every copy of schema-utils in the process.

Contributing

Please take a moment to read our contributing guidelines if you haven't yet done so.

CONTRIBUTING

The package itself runs on Node.js 10.13.0 and above, but the dev dependencies require an active LTS release, so use one to work on it. CI installs older ones to run the tests on the older versions of Node.js the package supports.

License

MIT

版本与发布记录

schema-utils 共发布 50 个版本,首次发布于 2017/03/07;过去 12 个月发布了 7 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.8.2latest2026/11/015 天前
4.8.12026/10/1918 天前
4.8.02026/10/1918 天前
4.7.02026/10/1918 天前
4.6.02026/10/1719 天前
4.5.02026/09/131 个月前

其他发布通道:version-3 → 3.3.0。

关于 schema-utils 的常见问题

schema-utils 是什么?

webpack Validation Utils

如何安装 schema-utils?

在项目目录运行 npm install schema-utils,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 schema-utils,可以用 npx schema-utils 直接运行。

schema-utils 支持 TypeScript 吗?

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

schema-utils 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

schema-utils 需要什么版本的 Node.js?

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

schema-utils 可以商用吗?

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

schema-utils 的下载量有多少?

根据 npm 官方下载统计,schema-utils 在 2026/10/05 – 2026/11/03 这 30 天内被下载 460,723,607 次,日均约 15,357,454 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

schema-utils 还在维护吗?

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