@webpack-utilities/schema

webpack Validation Utils

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
00Jun 26, 2018Jun 6, 2018Minified + gzip package size for @webpack-utilities/schema in KB

Readme

npm node deps test coverage code style chat

Schema Utils

Options Validation for Loaders/Plugins

Install

npm i @webpack-utilities/schema

Usage

validateOptions(schema, options, title)

schema.json

{
  "type": "object",
  "properties": {
    "option": {
      "type": [ "boolean" ]
    }
  },
  "errorMessage": {
    "option": "should be {Boolean} (https:/github.com/org/repo#anchor)"
  }
  "additionalProperties": false
}
import schema from 'path/to/schema.json'
import { validateOptions } from '@webpack-utilities/schema'

validateOptions(schema, options, 'Loader/Plugin Name')

Examples

options.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 '@webpack-utilities/loader'
import { validateOptions } from '@webpack-utilities/schema'

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

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

  validateOptions(schema, options, 'Loader Name')

  // Code...
}

Plugin

import schema from 'path/to/options.json'
import { validateOptions } from '@webpack-utilities/schema'

class Plugin {
  constructor (options) {
    validateOptions(schema, options, 'Plugin Name')

    this.options = options
  }

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

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.