UNPKG

3.41 kBMarkdownView Raw
1[![npm][npm]][npm-url]
2[![node][node]][node-url]
3[![deps][deps]][deps-url]
4[![test][test]][test-url]
5[![coverage][cover]][cover-url]
6[![chat][chat]][chat-url]
7
8<div align="center">
9 <a href="http://json-schema.org">
10 <img width="160" height="160"
11 src="https://raw.githubusercontent.com/webpack-contrib/schema-utils/master/logo.png">
12 </a>
13 <a href="https://github.com/webpack/webpack">
14 <img width="200" height="200"
15 src="https://webpack.js.org/assets/icon-square-big.svg">
16 </a>
17 <h1>Schema Utils</h1>
18</div>
19
20<h2 align="center">Install</h2>
21
22```bash
23npm i schema-utils
24```
25
26<h2 align="center">Usage</h2>
27
28### `validateOptions`
29
30**schema.json**
31```js
32{
33 "type": "object",
34 "properties": {
35 // Options...
36 },
37 "additionalProperties": false
38}
39```
40
41```js
42import schema from 'path/to/schema.json'
43import validateOptions from 'schema-utils'
44
45validateOptions(schema, options, 'Loader/Plugin Name')
46```
47
48<h2 align="center">Examples</h2>
49
50**schema.json**
51```json
52{
53 "type": "object",
54 "properties": {
55 "name": {
56 "type": "string"
57 },
58 "test": {
59 "anyOf": [
60 { "type": "array" },
61 { "type": "string" },
62 { "instanceof": "RegExp" }
63 ]
64 },
65 "transform": {
66 "instanceof": "Function"
67 },
68 "sourceMap": {
69 "type": "boolean"
70 }
71 },
72 "additionalProperties": false
73}
74```
75
76### `Loader`
77
78```js
79import { getOptions } from 'loader-utils'
80import validateOptions from 'schema-utils'
81
82import schema from 'path/to/schema.json'
83
84function loader (src, map) {
85 const options = getOptions(this) || {}
86
87 validateOptions(schema, options, 'Loader Name')
88
89 // Code...
90}
91```
92
93### `Plugin`
94
95```js
96import validateOptions from 'schema-utils'
97
98import schema from 'path/to/schema.json'
99
100class Plugin {
101 constructor (options) {
102 validateOptions(schema, options, 'Plugin Name')
103
104 this.options = options
105 }
106
107 apply (compiler) {
108 // Code...
109 }
110}
111```
112
113<h2 align="center">Maintainers</h2>
114
115<table>
116 <tbody>
117 <tr>
118 <td align="center">
119 <img width="150" height="150"
120 src="https://github.com/bebraw.png?v=3&s=150">
121 </br>
122 <a href="https://github.com/bebraw">Juho Vepsäläinen</a>
123 </td>
124 <td align="center">
125 <img width="150" height="150"
126 src="https://github.com/d3viant0ne.png?v=3&s=150">
127 </br>
128 <a href="https://github.com/d3viant0ne">Joshua Wiens</a>
129 </td>
130 <td align="center">
131 <img width="150" height="150"
132 src="https://github.com/michael-ciniawsky.png?v=3&s=150">
133 </br>
134 <a href="https://github.com/michael-ciniawsky">Michael Ciniawsky</a>
135 </td>
136 </tr>
137 <tbody>
138</table>
139
140
141[npm]: https://img.shields.io/npm/v/schema-utils.svg
142[npm-url]: https://npmjs.com/package/schema-utils
143
144[node]: https://img.shields.io/node/v/schema-utils.svg
145[node-url]: https://nodejs.org
146
147[deps]: https://david-dm.org/webpack-contrib/schema-utils.svg
148[deps-url]: https://david-dm.org/webpack-contrib/schema-utils
149
150[test]: http://img.shields.io/travis/webpack-contrib/schema-utils.svg
151[test-url]: https://travis-ci.org/webpack-contrib/schema-utils
152
153[cover]: https://codecov.io/gh/webpack-contrib/schema-utils/branch/master/graph/badge.svg
154[cover-url]: https://codecov.io/gh/webpack-contrib/schema-utils
155
156[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
157[chat-url]: https://gitter.im/webpack/webpack