@webpack-utilities/schema

1.0.0-alpha.0 • Public • Published

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

Dependencies (3)

Dev Dependencies (7)

Package Sidebar

Install

npm i @webpack-utilities/schema

Weekly Downloads

0

Version

1.0.0-alpha.0

License

MIT

Unpacked Size

6.9 kB

Total Files

7

Last publish

Collaborators

  • michael-ciniawsky