包详细信息

schema-utils

webpack374.1mMIT4.3.0

webpack Validation Utils

webpack

自述文件

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 schema from "./path/to/schema.json";
import { validate } from "schema-utils";

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 schema from "./path/to/schema.json";
import { validate } from "schema-utils";

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 schema from "./path/to/schema.json";
import { validate } from "schema-utils";

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 schema from "./path/to/schema.json";
import { validate } from "schema-utils";

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 schema from "./path/to/schema.json";
import { validate } from "schema-utils";

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 { getOptions } from "loader-utils";
import { validate } from "schema-utils";

import schema from "path/to/schema.json";

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

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

  // Code...
}

export default loader;

Plugin

import { validate } from "schema-utils";

import schema from "path/to/schema.json";

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.

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

Contributing

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

CONTRIBUTING

License

MIT

更新日志

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

4.3.0 (2024-12-11)

Features

  • backport old logic from v3 (2e2ba9d)

Bug Fixes

4.2.0 (2023-06-14)

Features

  • added API to disable and enable validation (#180) (d6b9c9e)

Bug Fixes

4.1.0 (2023-06-07)

Features

  • implement undefinedAsNull keyword for enum type (#175) (1265eac)

4.0.1 (2023-04-15)

Bug Fixes

  • (perf) improved initial start time

4.0.0 (2021-11-16)

⚠ BREAKING CHANGES

  • minimum supported Node.js version is >= 12.13.0
  • update ajv to 8.8.0 version, please read internal changes, postFormatter require attention due some properties were changed
  • logic for formatExclusiveMaximum and formatExclusiveMinimum was changed (due usage ajv-formats package)

3.1.1 (2021-07-19)

Bug Fixes

3.1.0 (2021-06-30)

Features

  • added the link property in validation error (589aa59)

Bug Fixes

3.0.0 (2020-10-05)

⚠ BREAKING CHANGES

  • minimum supported Node.js version is 10.13.0,
  • the packages exports was changed, please use const { validate } = require('schema-utils');
  • the ValidateError export was removed in favor the ValidationError export, please use const { ValidationError } = require('schema-utils');

2.7.1 (2020-08-31)

Bug Fixes

2.7.0 (2020-05-29)

Features

Bug Fixes

  • move @types/json-schema from devDependencies to dependencies (#97) (#98) (945e67d)

2.6.6 (2020-04-17)

Bug Fixes

  • improve perf

2.6.5 (2020-03-11)

Bug Fixes

  • correct dots at end of sentence (7284beb)

2.6.4 (2020-01-17)

Bug Fixes

  • change initialised to initialized (#87) (70f12d3)

2.6.3 (2020-01-17)

Bug Fixes

  • prefer the baseDataPath option from arguments (#86) (e236859)

2.6.2 (2020-01-14)

Bug Fixes

  • better handle Windows absolute paths (#85) (1fa2930)

2.6.1 (2019-11-28)

Bug Fixes

2.6.0 (2019-11-27)

Features

Bug Fixes

2.5.0 (2019-10-15)

Bug Fixes

  • rework format for maxLength, minLength (#67) (0d12259)
  • support all cases with one number in range (#64) (7fc8069)
  • typescript definition and export naming (#69) (a435b79)

Features

2.4.1 (2019-09-27)

Bug Fixes

2.4.0 (2019-09-26)

Features

  • better errors when the type keyword doesn't exist (0988be2)
  • support $data reference (#56) (d2f11d6)
  • types definitions (#52) (facb431)

2.3.0 (2019-09-26)

Features

2.2.0 (2019-09-02)

Features

2.1.0 (2019-08-07)

Bug Fixes

Features

2.0.1 (2019-07-18)

Bug Fixes

2.0.0 (2019-07-17)

BREAKING CHANGES

  • drop support for Node.js < 8.9.0
  • drop support errorMessage, please use description for links.
  • api was changed, please look documentation.
  • error messages was fully rewritten.

1.0.0 (2018-08-07)

Features

  • src: add support for custom error messages (#33) (1cbe4ef)

0.4.7 (2018-08-07)

Bug Fixes

0.4.6 (2018-08-06)

Bug Fixes

  • package: remove lockfile (#28) (69f1a81)
  • package: remove unnecessary webpack dependency (#26) (532eaa5)

0.4.5 (2018-02-13)

Bug Fixes

  • CHANGELOG: update broken links (4483b9f)
  • package: update broken links (f2494ba)

0.4.4 (2018-02-13)

Bug Fixes

0.4.3 (2017-12-14)

Bug Fixes

  • validateOptions: throw err instead of process.exit(1) (#17) (c595eda)
  • ValidationError: never return this in the ctor (#16) (c723791)

0.4.2 (2017-11-09)

Bug Fixes

  • validateOptions: catch ValidationError and handle it internally (#15) (9c5ef5e)

0.4.1 (2017-11-03)

Bug Fixes

  • ValidationError: use Error.captureStackTrace for err.stack handling (#14) (a6fb974)

0.4.0 (2017-10-28)

Features

  • add support for typeof, instanceof ({Function\|RegExp}) (#10) (9f01816)

0.3.0 (2017-04-29)

Features

0.2.1 (2017-03-13)

Bug Fixes

  • Include .babelrc to files (28f0363)
  • Include source to files (43b0f2f)

0.2.0 (2017-03-12)

0.1.0 (2017-03-07)

Features

  • validations: add validateOptions module (ae9b47b)

Change Log

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.