Package for validate options in loaders and plugins.
To begin, you'll need to install schema-utils:
npm install schema-utilsschema.json
{
"type": "object",
"properties": {
"option": {
"type": "boolean"
}
},
"additionalProperties": false
}import{validate}from"schema-utils";importschemafrom"./path/to/schema.json";constoptions={option: true};constconfiguration={name: "Loader Name/Plugin Name/Name"};validate(schema,options,configuration);Type: String
JSON schema.
Simple example of schema:
{
"type": "object",
"properties": {
"name": {
"description": "This is description of option.",
"type": "string"
}
},
"additionalProperties": false
}Type: Object
Object with options.
import{validate}from"schema-utils";importschemafrom"./path/to/schema.json";constoptions={foo: "bar"};validate(schema,{name: 123},{name: "MyPlugin"});Allow to configure validator.
There is an alternative method to configure the name andbaseDataPath options via the title property in the schema.
For example:
{
"title": "My Loader options",
"type": "object",
"properties": {
"name": {
"description": "This is description of option.",
"type": "string"
}
},
"additionalProperties": false
}The last word used for the baseDataPath option, other words used for the name option.
Based on the example above the name option equals My Loader, the baseDataPath option equals options.
Type: Object
Default: "Object"
Allow to setup name in validation errors.
import{validate}from"schema-utils";importschemafrom"./path/to/schema.json";constoptions={foo: "bar"};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.Type: String
Default: "configuration"
Allow to setup base data path in validation errors.
import{validate}from"schema-utils";importschemafrom"./path/to/schema.json";constoptions={foo: "bar"};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.Type: Function
Default: undefined
Allow to reformat errors.
import{validate}from"schema-utils";importschemafrom"./path/to/schema.json";constoptions={foo: "bar"};validate(schema,options,{name: "MyPlugin",postFormatter: (formattedError,error)=>{if(error.keyword==="type"){return`${formattedError}\nAdditional Information.`;}returnformattedError;},});Invalid options object. MyPlugin has been initialized using an options object that does not match the API schema.
- options.optionName should be a integer.
Additional Information.schema.json
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"test": {
"anyOf": [
{ "type": "array" },
{ "type": "string" },
{ "instanceof": "RegExp" }
]
},
"transform": {
"instanceof": "Function"
},
"sourceMap": {
"type": "boolean"
}
},
"additionalProperties": false
}importschemafrom"path/to/schema.json";import{getOptions}from"loader-utils";import{validate}from"schema-utils";functionloader(src,map){constoptions=getOptions(this);validate(schema,options,{name: "Loader Name",baseDataPath: "options",});// Code...}exportdefaultloader;importschemafrom"path/to/schema.json";import{validate}from"schema-utils";classPlugin{constructor(options){validate(schema,options,{name: "Plugin Name",baseDataPath: "options",});this.options=options;}apply(compiler){// Code...}}exportdefaultPlugin;This can be useful when you don't want to do validation for production builds.
import{disableValidation,enableValidation,validate}from"schema-utils";// Disable validationdisableValidation();// Do nothingvalidate(schema,options);// Enable validationenableValidation();// Will throw an error if schema is not validvalidate(schema,options);// Allow to undestand do you need validation or notconstneed=needValidate();console.log(need);Also you can enable/disable validation using the process.env.SKIP_VALIDATION env variable.
Supported values (case insensitive):
yes/y/true/1/onno/n/false/0/off
Please take a moment to read our contributing guidelines if you haven't yet done so.
