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.

276 lines
5.4 KiB

3 months ago
  1. <div align="center">
  2. <a href="http://json-schema.org">
  3. <img width="160" height="160"
  4. src="https://raw.githubusercontent.com/webpack-contrib/schema-utils/master/.github/assets/logo.png">
  5. </a>
  6. <a href="https://github.com/webpack/webpack">
  7. <img width="200" height="200"
  8. src="https://webpack.js.org/assets/icon-square-big.svg">
  9. </a>
  10. </div>
  11. [![npm][npm]][npm-url]
  12. [![node][node]][node-url]
  13. [![deps][deps]][deps-url]
  14. [![tests][tests]][tests-url]
  15. [![coverage][cover]][cover-url]
  16. [![chat][chat]][chat-url]
  17. [![size][size]][size-url]
  18. # schema-utils
  19. Package for validate options in loaders and plugins.
  20. ## Getting Started
  21. To begin, you'll need to install `schema-utils`:
  22. ```console
  23. npm install schema-utils
  24. ```
  25. ## API
  26. **schema.json**
  27. ```json
  28. {
  29. "type": "object",
  30. "properties": {
  31. "option": {
  32. "type": "boolean"
  33. }
  34. },
  35. "additionalProperties": false
  36. }
  37. ```
  38. ```js
  39. import schema from './path/to/schema.json';
  40. import validate from 'schema-utils';
  41. const options = { option: true };
  42. const configuration = { name: 'Loader Name/Plugin Name/Name' };
  43. validate(schema, options, configuration);
  44. ```
  45. ### `schema`
  46. Type: `String`
  47. JSON schema.
  48. Simple example of schema:
  49. ```json
  50. {
  51. "type": "object",
  52. "properties": {
  53. "name": {
  54. "description": "This is description of option.",
  55. "type": "string"
  56. }
  57. },
  58. "additionalProperties": false
  59. }
  60. ```
  61. ### `options`
  62. Type: `Object`
  63. Object with options.
  64. ```js
  65. validate(
  66. schema,
  67. {
  68. name: 123,
  69. },
  70. { name: 'MyPlugin' }
  71. );
  72. ```
  73. ### `configuration`
  74. Allow to configure validator.
  75. There is an alternative method to configure the `name` and`baseDataPath` options via the `title` property in the schema.
  76. For example:
  77. ```json
  78. {
  79. "title": "My Loader options",
  80. "type": "object",
  81. "properties": {
  82. "name": {
  83. "description": "This is description of option.",
  84. "type": "string"
  85. }
  86. },
  87. "additionalProperties": false
  88. }
  89. ```
  90. The last word used for the `baseDataPath` option, other words used for the `name` option.
  91. Based on the example above the `name` option equals `My Loader`, the `baseDataPath` option equals `options`.
  92. #### `name`
  93. Type: `Object`
  94. Default: `"Object"`
  95. Allow to setup name in validation errors.
  96. ```js
  97. validate(schema, options, { name: 'MyPlugin' });
  98. ```
  99. ```shell
  100. Invalid configuration object. MyPlugin has been initialised using a configuration object that does not match the API schema.
  101. - configuration.optionName should be a integer.
  102. ```
  103. #### `baseDataPath`
  104. Type: `String`
  105. Default: `"configuration"`
  106. Allow to setup base data path in validation errors.
  107. ```js
  108. validate(schema, options, { name: 'MyPlugin', baseDataPath: 'options' });
  109. ```
  110. ```shell
  111. Invalid options object. MyPlugin has been initialised using an options object that does not match the API schema.
  112. - options.optionName should be a integer.
  113. ```
  114. #### `postFormatter`
  115. Type: `Function`
  116. Default: `undefined`
  117. Allow to reformat errors.
  118. ```js
  119. validate(schema, options, {
  120. name: 'MyPlugin',
  121. postFormatter: (formattedError, error) => {
  122. if (error.keyword === 'type') {
  123. return `${formattedError}\nAdditional Information.`;
  124. }
  125. return formattedError;
  126. },
  127. });
  128. ```
  129. ```shell
  130. Invalid options object. MyPlugin has been initialized using an options object that does not match the API schema.
  131. - options.optionName should be a integer.
  132. Additional Information.
  133. ```
  134. ## Examples
  135. **schema.json**
  136. ```json
  137. {
  138. "type": "object",
  139. "properties": {
  140. "name": {
  141. "type": "string"
  142. },
  143. "test": {
  144. "anyOf": [
  145. { "type": "array" },
  146. { "type": "string" },
  147. { "instanceof": "RegExp" }
  148. ]
  149. },
  150. "transform": {
  151. "instanceof": "Function"
  152. },
  153. "sourceMap": {
  154. "type": "boolean"
  155. }
  156. },
  157. "additionalProperties": false
  158. }
  159. ```
  160. ### `Loader`
  161. ```js
  162. import { getOptions } from 'loader-utils';
  163. import validateOptions from 'schema-utils';
  164. import schema from 'path/to/schema.json';
  165. function loader(src, map) {
  166. const options = getOptions(this) || {};
  167. validateOptions(schema, options, {
  168. name: 'Loader Name',
  169. baseDataPath: 'options',
  170. });
  171. // Code...
  172. }
  173. export default loader;
  174. ```
  175. ### `Plugin`
  176. ```js
  177. import validateOptions from 'schema-utils';
  178. import schema from 'path/to/schema.json';
  179. class Plugin {
  180. constructor(options) {
  181. validateOptions(schema, options, {
  182. name: 'Plugin Name',
  183. baseDataPath: 'options',
  184. });
  185. this.options = options;
  186. }
  187. apply(compiler) {
  188. // Code...
  189. }
  190. }
  191. export default Plugin;
  192. ```
  193. ## Contributing
  194. Please take a moment to read our contributing guidelines if you haven't yet done so.
  195. [CONTRIBUTING](./.github/CONTRIBUTING.md)
  196. ## License
  197. [MIT](./LICENSE)
  198. [npm]: https://img.shields.io/npm/v/schema-utils.svg
  199. [npm-url]: https://npmjs.com/package/schema-utils
  200. [node]: https://img.shields.io/node/v/schema-utils.svg
  201. [node-url]: https://nodejs.org
  202. [deps]: https://david-dm.org/webpack/schema-utils.svg
  203. [deps-url]: https://david-dm.org/webpack/schema-utils
  204. [tests]: https://github.com/webpack/schema-utils/workflows/schema-utils/badge.svg
  205. [tests-url]: https://github.com/webpack/schema-utils/actions
  206. [cover]: https://codecov.io/gh/webpack/schema-utils/branch/master/graph/badge.svg
  207. [cover-url]: https://codecov.io/gh/webpack/schema-utils
  208. [chat]: https://badges.gitter.im/webpack/webpack.svg
  209. [chat-url]: https://gitter.im/webpack/webpack
  210. [size]: https://packagephobia.com/badge?p=schema-utils
  211. [size-url]: https://packagephobia.com/result?p=schema-utils