You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Daniel Hübleitner 615619e6e7 start werk 5 years ago
..
src start werk 5 years ago
CHANGELOG.md start werk 5 years ago
LICENSE start werk 5 years ago
README.md start werk 5 years ago
package.json start werk 5 years ago

README.md

npm node deps test coverage chat

Schema Utils

Install

npm i schema-utils

Usage

validateOptions

schema.json

{
  "type": "object",
  "properties": {
    // Options...
  },
  "additionalProperties": false
}

Error Messages (Custom)

schema.json

{
  "type": "object",
  "properties": {
    "option": {
      "type": [ "boolean" ]
    }
  },
  // Overrides the default err.message for option
  "errorMessage": {
    "option": "should be {Boolean} (https:/github.com/org/repo#anchor)"
  }
  "additionalProperties": false
}
import schema from 'path/to/schema.json'
import validateOptions from 'schema-utils'

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

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, 'Loader Name')

  // Code...
}

Plugin

import validateOptions from 'schema-utils'

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

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

    this.options = options
  }

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