944dc1debb1c878475a1708213e513d4ce56616f2e87462768c2d457361ae091809bec9a9ce5f24101b900bfd77f213dd6bd4d0652ce9a34173c6bfe2e4f0f 2.9 KB

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