计划图
用于加载器和插件中验证选项的软件包 .
开始
开始,你需要安装 schema-utils数字 :
npm install schema-utils
API 密码
计划.json
{
"type": "object",
"properties": {
"option": {
"type": ["boolean"]
}
},
"additionalProperties": false
}
import schema from './path/to/schema.json';
import validate from 'schema-utils';
const options = { option: true };
const configuration = { name: 'Loader Name/Plugin Name/Name' };
validate(schema, options, configuration);
schema
类型 : String
JSON计划.
简单的计划示例 :
{
"type": "object",
"properties": {
"name": {
"description": "This is description of option.",
"type": "string"
}
},
"additionalProperties": false
}
options
类型 : Object
带有选项的对象 .
validate(
schema,
{
name: 123,
},
{ name: 'MyPlugin' }
);
configuration
允许配置验证符 .
配置 name 财务报告和审定财务报表baseDataPath 通过 title 财产在计划.
例如:
{
"title": "My Loader options",
"type": "object",
"properties": {
"name": {
"description": "This is description of option.",
"type": "string"
}
},
"additionalProperties": false
}
用于 baseDataPath 选项,用于 name 选项.
根据上文的例子 name 选项等于 My Loader,则 baseDataPath 选项等于 options。 。 。 .
name
类型 : Object
默认 : "Object"
允许在验证错误中设置名称 .
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.
baseDataPath
类型 : String
默认 : "configuration"
允许在验证错误中设置基础数据路径.
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.
postFormatter
类型 : Function
默认 : undefined
允许重塑错误 .
validate(schema, options, {
name: 'MyPlugin',
postFormatter: (formattedError, error) => {
if (error.keyword === 'type') {
return `${formattedError}\nAdditional Information.`;
}
return formattedError;
},
});
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.
实例
计划.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, {
name: 'Loader Name',
baseDataPath: 'options',
});
// Code...
}
export default loader;
Plugin
import validateOptions from 'schema-utils';
import schema from 'path/to/schema.json';
class Plugin {
constructor(options) {
validateOptions(schema, options, {
name: 'Plugin Name',
baseDataPath: 'options',
});
this.options = options;
}
apply(compiler) {
// Code...
}
}
export default Plugin;
捐款
请花点时间来读一下我们的贡献准则 如果你还没有这样做.