You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
2.6 KiB

4 years ago
  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. ```js
  35. import schema from 'path/to/schema.json'
  36. import validateOptions from 'schema-utils'
  37. validateOptions(schema, options, 'Loader/Plugin Name')
  38. ```
  39. <h2 align="center">Examples</h2>
  40. **schema.json**
  41. ```json
  42. {
  43. "type": "object",
  44. "properties": {
  45. "name": {
  46. "type": "string"
  47. },
  48. "test": {
  49. "anyOf": [
  50. { "type": "array" },
  51. { "type": "string" },
  52. { "instanceof": "RegExp" }
  53. ]
  54. },
  55. "transform": {
  56. "instanceof": "Function"
  57. },
  58. "sourceMap": {
  59. "type": "boolean"
  60. }
  61. },
  62. "additionalProperties": false
  63. }
  64. ```
  65. ### `Loader`
  66. ```js
  67. import { getOptions } from 'loader-utils'
  68. import validateOptions from 'schema-utils'
  69. import schema from 'path/to/schema.json'
  70. function loader (src, map) {
  71. const options = getOptions(this) || {}
  72. validateOptions(schema, options, 'Loader Name')
  73. // Code...
  74. }
  75. ```
  76. ### `Plugin`
  77. ```js
  78. import validateOptions from 'schema-utils'
  79. import schema from 'path/to/schema.json'
  80. class Plugin {
  81. constructor (options) {
  82. validateOptions(schema, options, 'Plugin Name')
  83. this.options = options
  84. }
  85. apply (compiler) {
  86. // Code...
  87. }
  88. }
  89. ```
  90. [npm]: https://img.shields.io/npm/v/schema-utils.svg
  91. [npm-url]: https://npmjs.com/package/schema-utils
  92. [node]: https://img.shields.io/node/v/schema-utils.svg
  93. [node-url]: https://nodejs.org
  94. [deps]: https://david-dm.org/webpack-contrib/schema-utils.svg
  95. [deps-url]: https://david-dm.org/webpack-contrib/schema-utils
  96. [test]: http://img.shields.io/travis/webpack-contrib/schema-utils.svg
  97. [test-url]: https://travis-ci.org/webpack-contrib/schema-utils
  98. [cover]: https://codecov.io/gh/webpack-contrib/schema-utils/branch/master/graph/badge.svg
  99. [cover-url]: https://codecov.io/gh/webpack-contrib/schema-utils
  100. [chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
  101. [chat-url]: https://gitter.im/webpack/webpack