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