Projekt

Obecné

Profil

Stáhnout (2.93 KB) Statistiky
| Větev: | Revize:
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/docs/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
23
npm 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
#### Error Messages (Custom)
42

    
43
**`schema.json`**
44
```js
45
{
46
  "type": "object",
47
  "properties": {
48
    "option": {
49
      "type": [ "boolean" ]
50
    }
51
  },
52
  // Overrides the default err.message for option
53
  "errorMessage": {
54
    "option": "should be {Boolean} (https:/github.com/org/repo#anchor)"
55
  }
56
  "additionalProperties": false
57
}
58
```
59

    
60
```js
61
import schema from 'path/to/schema.json'
62
import validateOptions from 'schema-utils'
63

    
64
validateOptions(schema, options, 'Loader/Plugin Name')
65
```
66

    
67
<h2 align="center">Examples</h2>
68

    
69
**schema.json**
70
```json
71
{
72
  "type": "object",
73
  "properties": {
74
    "name": {
75
      "type": "string"
76
    },
77
    "test": {
78
      "anyOf": [
79
        { "type": "array" },
80
        { "type": "string" },
81
        { "instanceof": "RegExp" }
82
      ]
83
    },
84
    "transform": {
85
      "instanceof": "Function"
86
    },
87
    "sourceMap": {
88
      "type": "boolean"
89
    }
90
  },
91
  "additionalProperties": false
92
}
93
```
94

    
95
### `Loader`
96

    
97
```js
98
import { getOptions } from 'loader-utils'
99
import validateOptions from 'schema-utils'
100

    
101
import schema from 'path/to/schema.json'
102

    
103
function loader (src, map) {
104
  const options = getOptions(this) || {}
105

    
106
  validateOptions(schema, options, 'Loader Name')
107

    
108
  // Code...
109
}
110
```
111

    
112
### `Plugin`
113

    
114
```js
115
import validateOptions from 'schema-utils'
116

    
117
import schema from 'path/to/schema.json'
118

    
119
class Plugin {
120
  constructor (options) {
121
    validateOptions(schema, options, 'Plugin Name')
122

    
123
    this.options = options
124
  }
125

    
126
  apply (compiler) {
127
    // Code...
128
  }
129
}
130
```
131

    
132

    
133
[npm]: https://img.shields.io/npm/v/schema-utils.svg
134
[npm-url]: https://npmjs.com/package/schema-utils
135

    
136
[node]: https://img.shields.io/node/v/schema-utils.svg
137
[node-url]: https://nodejs.org
138

    
139
[deps]: https://david-dm.org/webpack-contrib/schema-utils.svg
140
[deps-url]: https://david-dm.org/webpack-contrib/schema-utils
141

    
142
[test]: http://img.shields.io/travis/webpack-contrib/schema-utils.svg
143
[test-url]: https://travis-ci.org/webpack-contrib/schema-utils
144

    
145
[cover]: https://codecov.io/gh/webpack-contrib/schema-utils/branch/master/graph/badge.svg
146
[cover-url]: https://codecov.io/gh/webpack-contrib/schema-utils
147

    
148
[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
149
[chat-url]: https://gitter.im/webpack/webpack
(3-3/4)