UNPKG

6.59 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[![tests][tests]][tests-url]
15[![coverage][cover]][cover-url]
16[![GitHub Discussions][discussion]][discussion-url]
17[![size][size]][size-url]
18
19# schema-utils
20
21Package for validate options in loaders and plugins.
22
23## Getting Started
24
25To begin, you'll need to install `schema-utils`:
26
27```console
28npm install schema-utils
29```
30
31## API
32
33**schema.json**
34
35```json
36{
37 "type": "object",
38 "properties": {
39 "option": {
40 "type": "boolean"
41 }
42 },
43 "additionalProperties": false
44}
45```
46
47```js
48import schema from "./path/to/schema.json";
49import { validate } from "schema-utils";
50
51const options = { option: true };
52const configuration = { name: "Loader Name/Plugin Name/Name" };
53
54validate(schema, options, configuration);
55```
56
57### `schema`
58
59Type: `String`
60
61JSON schema.
62
63Simple example of schema:
64
65```json
66{
67 "type": "object",
68 "properties": {
69 "name": {
70 "description": "This is description of option.",
71 "type": "string"
72 }
73 },
74 "additionalProperties": false
75}
76```
77
78### `options`
79
80Type: `Object`
81
82Object with options.
83
84```js
85import schema from "./path/to/schema.json";
86import { validate } from "schema-utils";
87
88const options = { foo: "bar" };
89
90validate(schema, { name: 123 }, { name: "MyPlugin" });
91```
92
93### `configuration`
94
95Allow to configure validator.
96
97There is an alternative method to configure the `name` and`baseDataPath` options via the `title` property in the schema.
98For example:
99
100```json
101{
102 "title": "My Loader options",
103 "type": "object",
104 "properties": {
105 "name": {
106 "description": "This is description of option.",
107 "type": "string"
108 }
109 },
110 "additionalProperties": false
111}
112```
113
114The last word used for the `baseDataPath` option, other words used for the `name` option.
115Based on the example above the `name` option equals `My Loader`, the `baseDataPath` option equals `options`.
116
117#### `name`
118
119Type: `Object`
120Default: `"Object"`
121
122Allow to setup name in validation errors.
123
124```js
125import schema from "./path/to/schema.json";
126import { validate } from "schema-utils";
127
128const options = { foo: "bar" };
129
130validate(schema, options, { name: "MyPlugin" });
131```
132
133```shell
134Invalid configuration object. MyPlugin has been initialised using a configuration object that does not match the API schema.
135 - configuration.optionName should be a integer.
136```
137
138#### `baseDataPath`
139
140Type: `String`
141Default: `"configuration"`
142
143Allow to setup base data path in validation errors.
144
145```js
146import schema from "./path/to/schema.json";
147import { validate } from "schema-utils";
148
149const options = { foo: "bar" };
150
151validate(schema, options, { name: "MyPlugin", baseDataPath: "options" });
152```
153
154```shell
155Invalid options object. MyPlugin has been initialised using an options object that does not match the API schema.
156 - options.optionName should be a integer.
157```
158
159#### `postFormatter`
160
161Type: `Function`
162Default: `undefined`
163
164Allow to reformat errors.
165
166```js
167import schema from "./path/to/schema.json";
168import { validate } from "schema-utils";
169
170const options = { foo: "bar" };
171
172validate(schema, options, {
173 name: "MyPlugin",
174 postFormatter: (formattedError, error) => {
175 if (error.keyword === "type") {
176 return `${formattedError}\nAdditional Information.`;
177 }
178
179 return formattedError;
180 },
181});
182```
183
184```shell
185Invalid options object. MyPlugin has been initialized using an options object that does not match the API schema.
186 - options.optionName should be a integer.
187 Additional Information.
188```
189
190## Examples
191
192**schema.json**
193
194```json
195{
196 "type": "object",
197 "properties": {
198 "name": {
199 "type": "string"
200 },
201 "test": {
202 "anyOf": [
203 { "type": "array" },
204 { "type": "string" },
205 { "instanceof": "RegExp" }
206 ]
207 },
208 "transform": {
209 "instanceof": "Function"
210 },
211 "sourceMap": {
212 "type": "boolean"
213 }
214 },
215 "additionalProperties": false
216}
217```
218
219### `Loader`
220
221```js
222import { getOptions } from "loader-utils";
223import { validate } from "schema-utils";
224
225import schema from "path/to/schema.json";
226
227function loader(src, map) {
228 const options = getOptions(this);
229
230 validate(schema, options, {
231 name: "Loader Name",
232 baseDataPath: "options",
233 });
234
235 // Code...
236}
237
238export default loader;
239```
240
241### `Plugin`
242
243```js
244import { validate } from "schema-utils";
245
246import schema from "path/to/schema.json";
247
248class Plugin {
249 constructor(options) {
250 validate(schema, options, {
251 name: "Plugin Name",
252 baseDataPath: "options",
253 });
254
255 this.options = options;
256 }
257
258 apply(compiler) {
259 // Code...
260 }
261}
262
263export default Plugin;
264```
265
266### Allow to disable and enable validation (the `validate` function do nothing)
267
268This can be useful when you don't want to do validation for `production` builds.
269
270```js
271import { disableValidation, enableValidation, validate } from "schema-utils";
272
273// Disable validation
274disableValidation();
275// Do nothing
276validate(schema, options);
277
278// Enable validation
279enableValidation();
280// Will throw an error if schema is not valid
281validate(schema, options);
282
283// Allow to undestand do you need validation or not
284const need = needValidate();
285
286console.log(need);
287```
288
289Also you can enable/disable validation using the `process.env.SKIP_VALIDATION` env variable.
290
291Supported values (case insensitive):
292
293- `yes`/`y`/`true`/`1`/`on`
294- `no`/`n`/`false`/`0`/`off`
295
296## Contributing
297
298Please take a moment to read our contributing guidelines if you haven't yet done so.
299
300[CONTRIBUTING](./.github/CONTRIBUTING.md)
301
302## License
303
304[MIT](./LICENSE)
305
306[npm]: https://img.shields.io/npm/v/schema-utils.svg
307[npm-url]: https://npmjs.com/package/schema-utils
308[node]: https://img.shields.io/node/v/schema-utils.svg
309[node-url]: https://nodejs.org
310[tests]: https://github.com/webpack/schema-utils/workflows/schema-utils/badge.svg
311[tests-url]: https://github.com/webpack/schema-utils/actions
312[cover]: https://codecov.io/gh/webpack/schema-utils/branch/master/graph/badge.svg
313[cover-url]: https://codecov.io/gh/webpack/schema-utils
314[discussion]: https://img.shields.io/github/discussions/webpack/webpack
315[discussion-url]: https://github.com/webpack/webpack/discussions
316[size]: https://packagephobia.com/badge?p=schema-utils
317[size-url]: https://packagephobia.com/result?p=schema-utils