# schema-utils

> webpack Validation Utils

Latest version **4.5.0** (published 2026-09-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install schema-utils
pnpm add schema-utils
yarn add schema-utils
bun add schema-utils
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 4.5.0 |
| Published | 2026-09-13 |
| First published | 2017-03-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 10.13.0 |
| Dependencies | 4 |
| Unpacked size | 93.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 251 |
| Author | webpack Contrib |
| Maintainers | 15000621931, ev1stensberg, __hai, sokra, avivkeller, evilebottnawi |
| Keywords | webpack |

## Links

- npm: https://www.npmjs.com/package/schema-utils
- Repository: https://github.com/webpack/schema-utils
- Issues: https://github.com/webpack/schema-utils/issues
- Funding: https://opencollective.com/webpack
- npm.io page: https://npm.io/package/schema-utils

## Dependencies (4)

- [ajv](https://npm.io/package/ajv.md) ^8.20.0
- [ajv-formats](https://npm.io/package/ajv-formats.md) ^3.0.1
- [ajv-keywords](https://npm.io/package/ajv-keywords.md) ^5.1.0
- [@types/json-schema](https://npm.io/package/@types/json-schema.md) ^7.0.15

## Alternatives

- [raw-loader](https://npm.io/package/raw-loader.md) — 4.3M weekly downloads
- [plop](https://npm.io/package/plop.md) — 1.4M weekly downloads
- [webpack-deadcode-plugin](https://npm.io/package/webpack-deadcode-plugin.md) — 80.3K weekly downloads
- [@storybook/preact-vite](https://npm.io/package/@storybook/preact-vite.md) — 54.2K weekly downloads
- [vite-plugin-transform](https://npm.io/package/vite-plugin-transform.md) — 2.4K weekly downloads

## Recent versions

- 4.5.0 (latest) — 2026-09-13
- 3.3.0 (version-3) — 2023-06-14
- 4.4.0 — 2026-09-09
- 4.3.3 — 2025-10-02
- 4.3.2 — 2025-04-22
- 4.3.1 — 2025-04-22
- 4.3.0 — 2024-12-11
- 4.2.0 — 2023-06-14
- 3.2.0 — 2023-06-07
- 4.1.0 — 2023-06-07
- 3.1.2 — 2023-04-15
- 4.0.1 — 2023-04-15
- 4.0.0 — 2021-11-16
- 3.1.1 — 2021-07-19
- 3.1.0 — 2021-07-05
- … 30 more at https://npm.io/package/schema-utils/versions

## README

<div align="center">
  <a href="http://json-schema.org">
    <img width="160" height="160"
      src="https://raw.githubusercontent.com/webpack-contrib/schema-utils/main/.github/assets/logo.png">
  </a>
  <a href="https://github.com/webpack/webpack">
    <img width="200" height="200"
      src="https://webpack.js.org/assets/icon-square-big.svg">
  </a>
</div>

[![npm][npm]][npm-url]
[![node][node]][node-url]
[![tests][tests]][tests-url]
[![coverage][cover]][cover-url]
[![GitHub Discussions][discussion]][discussion-url]
[![size][size]][size-url]

# schema-utils

Package for validate options in loaders and plugins.

## Getting Started

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

```console
npm install schema-utils
```

## API

**schema.json**

```json
{
  "type": "object",
  "properties": {
    "option": {
      "type": "boolean"
    }
  },
  "additionalProperties": false
}
```

```js
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:

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

### `options`

Type: `Object`

Object with options.

```js
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` and`baseDataPath` options via the `title` property in the schema.
For example:

```json
{
  "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.

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

const options = { foo: "bar" };

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

```shell
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.

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

const options = { foo: "bar" };

validate(schema, options, { name: "MyPlugin", baseDataPath: "options" });
```

```shell
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.

```js
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;
  },
});
```

```shell
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**

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

### `Loader`

```js
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`

```js
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;
```

### 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.

```js
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:

```console
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](https://github.com/webpack/schema-utils?tab=contributing-ov-file#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](./LICENSE)

[npm]: https://img.shields.io/npm/v/schema-utils.svg
[npm-url]: https://npmjs.com/package/schema-utils
[node]: https://img.shields.io/node/v/schema-utils.svg
[node-url]: https://nodejs.org
[tests]: https://github.com/webpack/schema-utils/workflows/schema-utils/badge.svg
[tests-url]: https://github.com/webpack/schema-utils/actions
[cover]: https://codecov.io/gh/webpack/schema-utils/branch/main/graph/badge.svg
[cover-url]: https://codecov.io/gh/webpack/schema-utils
[discussion]: https://img.shields.io/github/discussions/webpack/webpack
[discussion-url]: https://github.com/webpack/webpack/discussions
[size]: https://packagephobia.com/badge?p=schema-utils
[size-url]: https://packagephobia.com/result?p=schema-utils

---
_Source: https://npm.io/package/schema-utils · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
