Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@develar/schema-utils

webpack2.2mMIT2.6.5TypeScript support: included

webpack Validation Utils

webpack

readme

npm node deps tests coverage chat 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.

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.

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.

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.

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 validateOptions from 'schema-utils';

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

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

  validateOptions(schema, options, {
    name: 'Loader Name',
    baseDataPath: 'options',
  });

  // Code...
}

export default loader;

Plugin

import validateOptions from 'schema-utils';

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

class Plugin {
  constructor(options) {
    validateOptions(schema, options, {
      name: 'Plugin Name',
      baseDataPath: 'options',
    });

    this.options = options;
  }

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

export default Plugin;

Contributing

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

CONTRIBUTING

License

MIT

changelog

Changelog

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

2.6.5 (2020-03-11)

Bug Fixes

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.