Blame view

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