UNPKG

5.92 kBMarkdownView Raw
1<div align="center">
2 <a href="http://json-schema.org">
3 <img width="160" height="160"
4 src="https://raw.githubusercontent.com/webpack-contrib/schema-utils/master/.github/assets/logo.png">
5 </a>
6 <a href="https://github.com/webpack/webpack">
7 <img width="200" height="200"
8 src="https://webpack.js.org/assets/icon-square-big.svg">
9 </a>
10</div>
11
12[![npm][npm]][npm-url]
13[![node][node]][node-url]
14[![deps][deps]][deps-url]
15[![tests][tests]][tests-url]
16[![coverage][cover]][cover-url]
17[![chat][chat]][chat-url]
18[![size][size]][size-url]
19
20# schema-utils
21
22Package for validate options in loaders and plugins.
23
24## Getting Started
25
26To begin, you'll need to install `schema-utils`:
27
28```console
29npm install schema-utils
30```
31
32## API
33
34**schema.json**
35
36```json
37{
38 "type": "object",
39 "properties": {
40 "option": {
41 "type": "boolean"
42 }
43 },
44 "additionalProperties": false
45}
46```
47
48```js
49import schema from "./path/to/schema.json";
50import { validate } from "schema-utils";
51
52const options = { option: true };
53const configuration = { name: "Loader Name/Plugin Name/Name" };
54
55validate(schema, options, configuration);
56```
57
58### `schema`
59
60Type: `String`
61
62JSON schema.
63
64Simple 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
81Type: `Object`
82
83Object with options.
84
85```js
86import schema from "./path/to/schema.json";
87import { validate } from "schema-utils";
88
89const options = { foo: "bar" };
90
91validate(schema, { name: 123 }, { name: "MyPlugin" });
92```
93
94### `configuration`
95
96Allow to configure validator.
97
98There is an alternative method to configure the `name` and`baseDataPath` options via the `title` property in the schema.
99For example:
100
101```json
102{
103 "title": "My Loader options",
104 "type": "object",
105 "properties": {
106 "name": {
107 "description": "This is description of option.",
108 "type": "string"
109 }
110 },
111 "additionalProperties": false
112}
113```
114
115The last word used for the `baseDataPath` option, other words used for the `name` option.
116Based on the example above the `name` option equals `My Loader`, the `baseDataPath` option equals `options`.
117
118#### `name`
119
120Type: `Object`
121Default: `"Object"`
122
123Allow to setup name in validation errors.
124
125```js
126import schema from "./path/to/schema.json";
127import { validate } from "schema-utils";
128
129const options = { foo: "bar" };
130
131validate(schema, options, { name: "MyPlugin" });
132```
133
134```shell
135Invalid configuration object. MyPlugin has been initialised using a configuration object that does not match the API schema.
136 - configuration.optionName should be a integer.
137```
138
139#### `baseDataPath`
140
141Type: `String`
142Default: `"configuration"`
143
144Allow to setup base data path in validation errors.
145
146```js
147import schema from "./path/to/schema.json";
148import { validate } from "schema-utils";
149
150const options = { foo: "bar" };
151
152validate(schema, options, { name: "MyPlugin", baseDataPath: "options" });
153```
154
155```shell
156Invalid options object. MyPlugin has been initialised using an options object that does not match the API schema.
157 - options.optionName should be a integer.
158```
159
160#### `postFormatter`
161
162Type: `Function`
163Default: `undefined`
164
165Allow to reformat errors.
166
167```js
168import schema from "./path/to/schema.json";
169import { validate } from "schema-utils";
170
171const options = { foo: "bar" };
172
173validate(schema, options, {
174 name: "MyPlugin",
175 postFormatter: (formattedError, error) => {
176 if (error.keyword === "type") {
177 return `${formattedError}\nAdditional Information.`;
178 }
179
180 return formattedError;
181 },
182});
183```
184
185```shell
186Invalid options object. MyPlugin has been initialized using an options object that does not match the API schema.
187 - options.optionName should be a integer.
188 Additional Information.
189```
190
191## Examples
192
193**schema.json**
194
195```json
196{
197 "type": "object",
198 "properties": {
199 "name": {
200 "type": "string"
201 },
202 "test": {
203 "anyOf": [
204 { "type": "array" },
205 { "type": "string" },
206 { "instanceof": "RegExp" }
207 ]
208 },
209 "transform": {
210 "instanceof": "Function"
211 },
212 "sourceMap": {
213 "type": "boolean"
214 }
215 },
216 "additionalProperties": false
217}
218```
219
220### `Loader`
221
222```js
223import { getOptions } from "loader-utils";
224import { validate } from "schema-utils";
225
226import schema from "path/to/schema.json";
227
228function loader(src, map) {
229 const options = getOptions(this);
230
231 validate(schema, options, {
232 name: "Loader Name",
233 baseDataPath: "options",
234 });
235
236 // Code...
237}
238
239export default loader;
240```
241
242### `Plugin`
243
244```js
245import { validate } from "schema-utils";
246
247import schema from "path/to/schema.json";
248
249class Plugin {
250 constructor(options) {
251 validate(schema, options, {
252 name: "Plugin Name",
253 baseDataPath: "options",
254 });
255
256 this.options = options;
257 }
258
259 apply(compiler) {
260 // Code...
261 }
262}
263
264export default Plugin;
265```
266
267## Contributing
268
269Please take a moment to read our contributing guidelines if you haven't yet done so.
270
271[CONTRIBUTING](./.github/CONTRIBUTING.md)
272
273## License
274
275[MIT](./LICENSE)
276
277[npm]: https://img.shields.io/npm/v/schema-utils.svg
278[npm-url]: https://npmjs.com/package/schema-utils
279[node]: https://img.shields.io/node/v/schema-utils.svg
280[node-url]: https://nodejs.org
281[deps]: https://david-dm.org/webpack/schema-utils.svg
282[deps-url]: https://david-dm.org/webpack/schema-utils
283[tests]: https://github.com/webpack/schema-utils/workflows/schema-utils/badge.svg
284[tests-url]: https://github.com/webpack/schema-utils/actions
285[cover]: https://codecov.io/gh/webpack/schema-utils/branch/master/graph/badge.svg
286[cover-url]: https://codecov.io/gh/webpack/schema-utils
287[chat]: https://badges.gitter.im/webpack/webpack.svg
288[chat-url]: https://gitter.im/webpack/webpack
289[size]: https://packagephobia.com/badge?p=schema-utils
290[size-url]: https://packagephobia.com/result?p=schema-utils