@@ -31,8 +31,6 @@ npm install schema-utils
31
31
32
32
## API
33
33
34
- ### validateOptions
35
-
36
34
** schema.json**
37
35
38
36
``` json
@@ -43,20 +41,117 @@ npm install schema-utils
43
41
"type" : [" boolean" ]
44
42
}
45
43
},
46
- "errorMessage" : {
47
- "option" : " should be {Boolean} (https:/github.com/org/repo#anchor)"
48
- },
49
44
"additionalProperties" : false
50
45
}
51
46
```
52
47
53
48
``` js
54
49
import schema from ' ./path/to/schema.json' ;
55
- import validateOptions from ' schema-utils' ;
50
+ import validate from ' schema-utils' ;
56
51
57
52
const options = { option: true };
53
+ const configuration = { name: ' Loader Name/Plugin Name/Name' };
58
54
59
- validateOptions (schema, options, ' Loader/Plugin Name' );
55
+ validate (schema, options, configuration);
56
+ ```
57
+
58
+ ### ` schema `
59
+
60
+ Type: ` String `
61
+
62
+ JSON schema.
63
+
64
+ Simple example of schema:
65
+
66
+ ``` json
67
+ {
68
+ "type" : " object" ,
69
+ "properties" : {
70
+ "name" : {
71
+ "description" : " This is description of option." ,
72
+ "type" : " string"
73
+ }
74
+ },
75
+ "additionalProperties" : false
76
+ }
77
+ ```
78
+
79
+ ### ` options `
80
+
81
+ Type: ` Object `
82
+
83
+ Object with options.
84
+
85
+ ``` js
86
+ validate (
87
+ schema,
88
+ {
89
+ name: 123 ,
90
+ },
91
+ { name: ' MyPlugin' }
92
+ );
93
+ ```
94
+
95
+ ### ` configuration `
96
+
97
+ Allow to configure validator.
98
+
99
+ #### ` name `
100
+
101
+ Type: ` Object `
102
+ Default: ` "Object" `
103
+
104
+ Allow to setup name in validation errors.
105
+
106
+ ``` js
107
+ validate (schema, options, { name: ' MyPlugin' });
108
+ ```
109
+
110
+ ``` shell
111
+ Invalid configuration object. MyPlugin has been initialised using a configuration object that does not match the API schema.
112
+ - configuration.optionName should be a integer.
113
+ ```
114
+
115
+ #### ` baseDataPath `
116
+
117
+ Type: ` String `
118
+ Default: ` "configuration" `
119
+
120
+ Allow to setup base data path in validation errors.
121
+
122
+ ``` js
123
+ validate (schema, options, { name: ' MyPlugin' , baseDataPath: ' options' });
124
+ ```
125
+
126
+ ``` shell
127
+ Invalid options object. MyPlugin has been initialised using an options object that does not match the API schema.
128
+ - options.optionName should be a integer.
129
+ ```
130
+
131
+ #### ` postFormatter `
132
+
133
+ Type: ` Function `
134
+ Default: ` undefined `
135
+
136
+ Allow to reformat errors.
137
+
138
+ ``` js
139
+ validate (schema, options, {
140
+ name: ' MyPlugin' ,
141
+ postFormatter : (formattedError , error ) => {
142
+ if (error .keyword === ' type' ) {
143
+ return ` ${ formattedError} \n Additional Information.` ;
144
+ }
145
+
146
+ return formattedError;
147
+ },
148
+ });
149
+ ```
150
+
151
+ ``` shell
152
+ Invalid options object. MyPlugin has been initialised using an options object that does not match the API schema.
153
+ - options.optionName should be a integer.
154
+ Additional Information.
60
155
```
61
156
62
157
## Examples
@@ -99,10 +194,15 @@ import schema from 'path/to/schema.json';
99
194
function loader (src , map ) {
100
195
const options = getOptions (this ) || {};
101
196
102
- validateOptions (schema, options, ' Loader Name' );
197
+ validateOptions (schema, options, {
198
+ name: ' Loader Name' ,
199
+ baseDataPath: ' options' ,
200
+ });
103
201
104
202
// Code...
105
203
}
204
+
205
+ export default loader ;
106
206
```
107
207
108
208
### ` Plugin `
@@ -114,7 +214,10 @@ import schema from 'path/to/schema.json';
114
214
115
215
class Plugin {
116
216
constructor (options ) {
117
- validateOptions (schema, options, ' Plugin Name' );
217
+ validateOptions (schema, options, {
218
+ name: ' Plugin Name' ,
219
+ baseDataPath: ' options' ,
220
+ });
118
221
119
222
this .options = options;
120
223
}
@@ -123,6 +226,8 @@ class Plugin {
123
226
// Code...
124
227
}
125
228
}
229
+
230
+ export default Plugin ;
126
231
```
127
232
128
233
## Contributing
@@ -139,12 +244,12 @@ Please take a moment to read our contributing guidelines if you haven't yet done
139
244
[ npm-url ] : https://npmjs.com/package/schema-utils
140
245
[ node ] : https://img.shields.io/node/v/schema-utils.svg
141
246
[ node-url ] : https://nodejs.org
142
- [ deps ] : https://david-dm.org/webpack-contrib /schema-utils.svg
143
- [ deps-url ] : https://david-dm.org/webpack-contrib /schema-utils
144
- [ tests ] : https://dev.azure.com/webpack-contrib /schema-utils/_apis/build/status/webpack-contrib .schema-utils?branchName=master
145
- [ tests-url ] : https://dev.azure.com/webpack-contrib /schema-utils/_build/latest?definitionId=2 &branchName=master
146
- [ cover ] : https://codecov.io/gh/webpack-contrib /schema-utils/branch/master/graph/badge.svg
147
- [ cover-url ] : https://codecov.io/gh/webpack-contrib /schema-utils
247
+ [ deps ] : https://david-dm.org/webpack/schema-utils.svg
248
+ [ deps-url ] : https://david-dm.org/webpack/schema-utils
249
+ [ tests ] : https://dev.azure.com/webpack/schema-utils/_apis/build/status/webpack.schema-utils?branchName=master
250
+ [ tests-url ] : https://dev.azure.com/webpack/schema-utils/_build/latest?definitionId=9 &branchName=master
251
+ [ cover ] : https://codecov.io/gh/webpack/schema-utils/branch/master/graph/badge.svg
252
+ [ cover-url ] : https://codecov.io/gh/webpack/schema-utils
148
253
[ chat ] : https://badges.gitter.im/webpack/webpack.svg
149
254
[ chat-url ] : https://gitter.im/webpack/webpack
150
255
[ size ] : https://packagephobia.now.sh/badge?p=schema-utils
0 commit comments