Skip to content
Options Validation
JavaScript
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github
src fix: throw error on sparse arrays (#47) Aug 7, 2019
test fix: throw error on sparse arrays (#47) Aug 7, 2019
.editorconfig chore(default): update (#40) Jun 13, 2019
.eslintignore chore(default): update (#40) Jun 13, 2019
.eslintrc.js chore(default): update (#40) Jun 13, 2019
.gitattributes build(defaults): v0.4.5...1.6.0 (#11) Oct 28, 2017
.gitignore chore(default): update (#40) Jun 13, 2019
.prettierignore chore(default): update (#40) Jun 13, 2019
.prettierrc.js chore(default): update (#40) Jun 13, 2019
CHANGELOG.md chore(release): 2.1.0 Aug 7, 2019
LICENSE chore(package): update to defaults 0.2.1 Mar 7, 2017
README.md refactor: code (#41) Jul 16, 2019
azure-pipelines.yml
babel.config.js chore(default): update (#40) Jun 13, 2019
commitlint.config.js
husky.config.js
lint-staged.config.js refactor: code (#41) Jul 16, 2019
package-lock.json chore(release): 2.1.0 Aug 7, 2019
package.json chore(release): 2.1.0 Aug 7, 2019

README.md

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.

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 initialised 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

You can’t perform that action at this time.