Gathering detailed insights and metrics for schema-utils2
Gathering detailed insights and metrics for schema-utils2
Gathering detailed insights and metrics for schema-utils2
Gathering detailed insights and metrics for schema-utils2
npm install schema-utils2
Typescript
Module System
Min. Node Version
Node Version
NPM Version
72.6
Supply Chain
99
Quality
74.5
Maintenance
100
Vulnerability
99.6
License
JavaScript (99.9%)
Shell (0.1%)
Total Downloads
1,851
Last Day
1
Last Week
5
Last Month
31
Last Year
394
MIT License
250 Stars
201 Commits
39 Forks
10 Watchers
3 Branches
88 Contributors
Updated on Jul 22, 2025
Latest Version
2.6.7
Package Id
schema-utils2@2.6.7
Unpacked Size
62.73 kB
Size
13.52 kB
File Count
14
NPM Version
6.13.7
Node Version
13.11.0
Cumulative downloads
Total Downloads
Last Day
-50%
1
Compared to previous day
Last Week
0%
5
Compared to previous week
Last Month
40.9%
31
Compared to previous month
Last Year
8.8%
394
Compared to previous year
2
24
Package for validate options in loaders and plugins.
To begin, you'll need to install schema-utils
:
1npm install schema-utils
schema.json
1{ 2 "type": "object", 3 "properties": { 4 "option": { 5 "type": ["boolean"] 6 } 7 }, 8 "additionalProperties": false 9}
1import schema from './path/to/schema.json'; 2import validate from 'schema-utils'; 3 4const options = { option: true }; 5const configuration = { name: 'Loader Name/Plugin Name/Name' }; 6 7//增加Ajv实例化配置项 8const ajvOptions = { 9 useDefaults: true 10}; 11 12validate(schema, options, configuration, ajvOptions);
schema
Type: String
JSON schema.
Simple example of schema:
1{ 2 "type": "object", 3 "properties": { 4 "name": { 5 "description": "This is description of option.", 6 "type": "string" 7 } 8 }, 9 "additionalProperties": false 10}
options
Type: Object
Object with options.
1validate( 2 schema, 3 { 4 name: 123, 5 }, 6 { name: 'MyPlugin' } 7);
configuration
Allow to configure validator.
There is an alternative method to configure the name
andbaseDataPath
options via the title
property in the schema.
For example:
1{ 2 "title": "My Loader options", 3 "type": "object", 4 "properties": { 5 "name": { 6 "description": "This is description of option.", 7 "type": "string" 8 } 9 }, 10 "additionalProperties": false 11}
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
.
name
Type: Object
Default: "Object"
Allow to setup name in validation errors.
1validate(schema, options, { name: 'MyPlugin' });
1Invalid configuration object. MyPlugin has been initialised using a configuration object that does not match the API schema. 2 - configuration.optionName should be a integer.
baseDataPath
Type: String
Default: "configuration"
Allow to setup base data path in validation errors.
1validate(schema, options, { name: 'MyPlugin', baseDataPath: 'options' });
1Invalid options object. MyPlugin has been initialised using an options object that does not match the API schema. 2 - options.optionName should be a integer.
postFormatter
Type: Function
Default: undefined
Allow to reformat errors.
1validate(schema, options, { 2 name: 'MyPlugin', 3 postFormatter: (formattedError, error) => { 4 if (error.keyword === 'type') { 5 return `${formattedError}\nAdditional Information.`; 6 } 7 8 return formattedError; 9 }, 10});
1Invalid options object. MyPlugin has been initialized using an options object that does not match the API schema. 2 - options.optionName should be a integer. 3 Additional Information.
schema.json
1{ 2 "type": "object", 3 "properties": { 4 "name": { 5 "type": "string" 6 }, 7 "test": { 8 "anyOf": [ 9 { "type": "array" }, 10 { "type": "string" }, 11 { "instanceof": "RegExp" } 12 ] 13 }, 14 "transform": { 15 "instanceof": "Function" 16 }, 17 "sourceMap": { 18 "type": "boolean" 19 } 20 }, 21 "additionalProperties": false 22}
Loader
1import { getOptions } from 'loader-utils'; 2import validateOptions from 'schema-utils'; 3 4import schema from 'path/to/schema.json'; 5 6function loader(src, map) { 7 const options = getOptions(this) || {}; 8 9 validateOptions(schema, options, { 10 name: 'Loader Name', 11 baseDataPath: 'options', 12 }); 13 14 // Code... 15} 16 17export default loader;
Plugin
1import validateOptions from 'schema-utils'; 2 3import schema from 'path/to/schema.json'; 4 5class Plugin { 6 constructor(options) { 7 validateOptions(schema, options, { 8 name: 'Plugin Name', 9 baseDataPath: 'options', 10 }); 11 12 this.options = options; 13 } 14 15 apply(compiler) { 16 // Code... 17 } 18} 19 20export default Plugin;
Please take a moment to read our contributing guidelines if you haven't yet done so.
No vulnerabilities found.