455bebd2d5ee8045a2ddaca4b060d59c954c015df94bdfc6aa031f2e67d44d82c7c7a63d065d7412f84f1983f99f8a25a17eed8d32f35d81b7ce24687f7b5c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. import schema from "./path/to/schema.json";
  66. import { validate } from "schema-utils";
  67. const options = { foo: "bar" };
  68. validate(schema, { name: 123 }, { name: "MyPlugin" });
  69. ```
  70. ### `configuration`
  71. Allow to configure validator.
  72. There is an alternative method to configure the `name` and`baseDataPath` options via the `title` property in the schema.
  73. For example:
  74. ```json
  75. {
  76. "title": "My Loader options",
  77. "type": "object",
  78. "properties": {
  79. "name": {
  80. "description": "This is description of option.",
  81. "type": "string"
  82. }
  83. },
  84. "additionalProperties": false
  85. }
  86. ```
  87. The last word used for the `baseDataPath` option, other words used for the `name` option.
  88. Based on the example above the `name` option equals `My Loader`, the `baseDataPath` option equals `options`.
  89. #### `name`
  90. Type: `Object`
  91. Default: `"Object"`
  92. Allow to setup name in validation errors.
  93. ```js
  94. import schema from "./path/to/schema.json";
  95. import { validate } from "schema-utils";
  96. const options = { foo: "bar" };
  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. import schema from "./path/to/schema.json";
  109. import { validate } from "schema-utils";
  110. const options = { foo: "bar" };
  111. validate(schema, options, { name: "MyPlugin", baseDataPath: "options" });
  112. ```
  113. ```shell
  114. Invalid options object. MyPlugin has been initialised using an options object that does not match the API schema.
  115. - options.optionName should be a integer.
  116. ```
  117. #### `postFormatter`
  118. Type: `Function`
  119. Default: `undefined`
  120. Allow to reformat errors.
  121. ```js
  122. import schema from "./path/to/schema.json";
  123. import { validate } from "schema-utils";
  124. const options = { foo: "bar" };
  125. validate(schema, options, {
  126. name: "MyPlugin",
  127. postFormatter: (formattedError, error) => {
  128. if (error.keyword === "type") {
  129. return `${formattedError}\nAdditional Information.`;
  130. }
  131. return formattedError;
  132. },
  133. });
  134. ```
  135. ```shell
  136. Invalid options object. MyPlugin has been initialized using an options object that does not match the API schema.
  137. - options.optionName should be a integer.
  138. Additional Information.
  139. ```
  140. ## Examples
  141. **schema.json**
  142. ```json
  143. {
  144. "type": "object",
  145. "properties": {
  146. "name": {
  147. "type": "string"
  148. },
  149. "test": {
  150. "anyOf": [
  151. { "type": "array" },
  152. { "type": "string" },
  153. { "instanceof": "RegExp" }
  154. ]
  155. },
  156. "transform": {
  157. "instanceof": "Function"
  158. },
  159. "sourceMap": {
  160. "type": "boolean"
  161. }
  162. },
  163. "additionalProperties": false
  164. }
  165. ```
  166. ### `Loader`
  167. ```js
  168. import { getOptions } from "loader-utils";
  169. import { validate } from "schema-utils";
  170. import schema from "path/to/schema.json";
  171. function loader(src, map) {
  172. const options = getOptions(this);
  173. validate(schema, options, {
  174. name: "Loader Name",
  175. baseDataPath: "options",
  176. });
  177. // Code...
  178. }
  179. export default loader;
  180. ```
  181. ### `Plugin`
  182. ```js
  183. import { validate } from "schema-utils";
  184. import schema from "path/to/schema.json";
  185. class Plugin {
  186. constructor(options) {
  187. validate(schema, options, {
  188. name: "Plugin Name",
  189. baseDataPath: "options",
  190. });
  191. this.options = options;
  192. }
  193. apply(compiler) {
  194. // Code...
  195. }
  196. }
  197. export default Plugin;
  198. ```
  199. ## Contributing
  200. Please take a moment to read our contributing guidelines if you haven't yet done so.
  201. [CONTRIBUTING](./.github/CONTRIBUTING.md)
  202. ## License
  203. [MIT](./LICENSE)
  204. [npm]: https://img.shields.io/npm/v/schema-utils.svg
  205. [npm-url]: https://npmjs.com/package/schema-utils
  206. [node]: https://img.shields.io/node/v/schema-utils.svg
  207. [node-url]: https://nodejs.org
  208. [deps]: https://david-dm.org/webpack/schema-utils.svg
  209. [deps-url]: https://david-dm.org/webpack/schema-utils
  210. [tests]: https://github.com/webpack/schema-utils/workflows/schema-utils/badge.svg
  211. [tests-url]: https://github.com/webpack/schema-utils/actions
  212. [cover]: https://codecov.io/gh/webpack/schema-utils/branch/master/graph/badge.svg
  213. [cover-url]: https://codecov.io/gh/webpack/schema-utils
  214. [chat]: https://badges.gitter.im/webpack/webpack.svg
  215. [chat-url]: https://gitter.im/webpack/webpack
  216. [size]: https://packagephobia.com/badge?p=schema-utils
  217. [size-url]: https://packagephobia.com/result?p=schema-utils