a zip code crypto-currency system good for red ONLY

WebpackOptionsValidationError.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Gajus Kuizinas @gajus
  4. */
  5. "use strict";
  6. const WebpackError = require("./WebpackError");
  7. const webpackOptionsSchema = require("../schemas/webpackOptionsSchema.json");
  8. const getSchemaPart = (path, parents, additionalPath) => {
  9. parents = parents || 0;
  10. path = path.split("/");
  11. path = path.slice(0, path.length - parents);
  12. if(additionalPath) {
  13. additionalPath = additionalPath.split("/");
  14. path = path.concat(additionalPath);
  15. }
  16. let schemaPart = webpackOptionsSchema;
  17. for(let i = 1; i < path.length; i++) {
  18. const inner = schemaPart[path[i]];
  19. if(inner)
  20. schemaPart = inner;
  21. }
  22. return schemaPart;
  23. };
  24. const getSchemaPartText = (schemaPart, additionalPath) => {
  25. if(additionalPath) {
  26. for(let i = 0; i < additionalPath.length; i++) {
  27. const inner = schemaPart[additionalPath[i]];
  28. if(inner)
  29. schemaPart = inner;
  30. }
  31. }
  32. while(schemaPart.$ref) schemaPart = getSchemaPart(schemaPart.$ref);
  33. let schemaText = WebpackOptionsValidationError.formatSchema(schemaPart);
  34. if(schemaPart.description)
  35. schemaText += `\n${schemaPart.description}`;
  36. return schemaText;
  37. };
  38. const indent = (str, prefix, firstLine) => {
  39. if(firstLine) {
  40. return prefix + str.replace(/\n(?!$)/g, "\n" + prefix);
  41. } else {
  42. return str.replace(/\n(?!$)/g, `\n${prefix}`);
  43. }
  44. };
  45. class WebpackOptionsValidationError extends WebpackError {
  46. constructor(validationErrors) {
  47. super();
  48. this.name = "WebpackOptionsValidationError";
  49. this.message = "Invalid configuration object. " +
  50. "Webpack has been initialised using a configuration object that does not match the API schema.\n" +
  51. validationErrors.map(err => " - " + indent(WebpackOptionsValidationError.formatValidationError(err), " ", false)).join("\n");
  52. this.validationErrors = validationErrors;
  53. Error.captureStackTrace(this, this.constructor);
  54. }
  55. static formatSchema(schema, prevSchemas) {
  56. prevSchemas = prevSchemas || [];
  57. const formatInnerSchema = (innerSchema, addSelf) => {
  58. if(!addSelf) return WebpackOptionsValidationError.formatSchema(innerSchema, prevSchemas);
  59. if(prevSchemas.indexOf(innerSchema) >= 0) return "(recursive)";
  60. return WebpackOptionsValidationError.formatSchema(innerSchema, prevSchemas.concat(schema));
  61. };
  62. if(schema.type === "string") {
  63. if(schema.minLength === 1)
  64. return "non-empty string";
  65. else if(schema.minLength > 1)
  66. return `string (min length ${schema.minLength})`;
  67. return "string";
  68. } else if(schema.type === "boolean") {
  69. return "boolean";
  70. } else if(schema.type === "number") {
  71. return "number";
  72. } else if(schema.type === "object") {
  73. if(schema.properties) {
  74. const required = schema.required || [];
  75. return `object { ${Object.keys(schema.properties).map(property => {
  76. if(required.indexOf(property) < 0) return property + "?";
  77. return property;
  78. }).concat(schema.additionalProperties ? ["..."] : []).join(", ")} }`;
  79. }
  80. if(schema.additionalProperties) {
  81. return `object { <key>: ${formatInnerSchema(schema.additionalProperties)} }`;
  82. }
  83. return "object";
  84. } else if(schema.type === "array") {
  85. return `[${formatInnerSchema(schema.items)}]`;
  86. }
  87. switch(schema.instanceof) {
  88. case "Function":
  89. return "function";
  90. case "RegExp":
  91. return "RegExp";
  92. }
  93. if(schema.$ref) return formatInnerSchema(getSchemaPart(schema.$ref), true);
  94. if(schema.allOf) return schema.allOf.map(formatInnerSchema).join(" & ");
  95. if(schema.oneOf) return schema.oneOf.map(formatInnerSchema).join(" | ");
  96. if(schema.anyOf) return schema.anyOf.map(formatInnerSchema).join(" | ");
  97. if(schema.enum) return schema.enum.map(item => JSON.stringify(item)).join(" | ");
  98. return JSON.stringify(schema, 0, 2);
  99. }
  100. static formatValidationError(err) {
  101. const dataPath = `configuration${err.dataPath}`;
  102. if(err.keyword === "additionalProperties") {
  103. const baseMessage = `${dataPath} has an unknown property '${err.params.additionalProperty}'. These properties are valid:\n${getSchemaPartText(err.parentSchema)}`;
  104. if(!err.dataPath) {
  105. switch(err.params.additionalProperty) {
  106. case "debug":
  107. return `${baseMessage}\n` +
  108. "The 'debug' property was removed in webpack 2.\n" +
  109. "Loaders should be updated to allow passing this option via loader options in module.rules.\n" +
  110. "Until loaders are updated one can use the LoaderOptionsPlugin to switch loaders into debug mode:\n" +
  111. "plugins: [\n" +
  112. " new webpack.LoaderOptionsPlugin({\n" +
  113. " debug: true\n" +
  114. " })\n" +
  115. "]";
  116. }
  117. return baseMessage + "\n" +
  118. "For typos: please correct them.\n" +
  119. "For loader options: webpack 2 no longer allows custom properties in configuration.\n" +
  120. " Loaders should be updated to allow passing options via loader options in module.rules.\n" +
  121. " Until loaders are updated one can use the LoaderOptionsPlugin to pass these options to the loader:\n" +
  122. " plugins: [\n" +
  123. " new webpack.LoaderOptionsPlugin({\n" +
  124. " // test: /\\.xxx$/, // may apply this only for some modules\n" +
  125. " options: {\n" +
  126. ` ${err.params.additionalProperty}: ...\n` +
  127. " }\n" +
  128. " })\n" +
  129. " ]";
  130. }
  131. return baseMessage;
  132. } else if(err.keyword === "oneOf" || err.keyword === "anyOf") {
  133. if(err.children && err.children.length > 0) {
  134. return `${dataPath} should be one of these:\n${getSchemaPartText(err.parentSchema)}\n` +
  135. `Details:\n${err.children.map(err => " * " + indent(WebpackOptionsValidationError.formatValidationError(err), " ", false)).join("\n")}`;
  136. }
  137. return `${dataPath} should be one of these:\n${getSchemaPartText(err.parentSchema)}`;
  138. } else if(err.keyword === "enum") {
  139. if(err.parentSchema && err.parentSchema.enum && err.parentSchema.enum.length === 1) {
  140. return `${dataPath} should be ${getSchemaPartText(err.parentSchema)}`;
  141. }
  142. return `${dataPath} should be one of these:\n${getSchemaPartText(err.parentSchema)}`;
  143. } else if(err.keyword === "allOf") {
  144. return `${dataPath} should be:\n${getSchemaPartText(err.parentSchema)}`;
  145. } else if(err.keyword === "type") {
  146. switch(err.params.type) {
  147. case "object":
  148. return `${dataPath} should be an object.`;
  149. case "string":
  150. return `${dataPath} should be a string.`;
  151. case "boolean":
  152. return `${dataPath} should be a boolean.`;
  153. case "number":
  154. return `${dataPath} should be a number.`;
  155. case "array":
  156. return `${dataPath} should be an array:\n${getSchemaPartText(err.parentSchema)}`;
  157. }
  158. return `${dataPath} should be ${err.params.type}:\n${getSchemaPartText(err.parentSchema)}`;
  159. } else if(err.keyword === "instanceof") {
  160. return `${dataPath} should be an instance of ${getSchemaPartText(err.parentSchema)}.`;
  161. } else if(err.keyword === "required") {
  162. const missingProperty = err.params.missingProperty.replace(/^\./, "");
  163. return `${dataPath} misses the property '${missingProperty}'.\n${getSchemaPartText(err.parentSchema, ["properties", missingProperty])}`;
  164. } else if(err.keyword === "minLength" || err.keyword === "minItems") {
  165. if(err.params.limit === 1)
  166. return `${dataPath} should not be empty.`;
  167. else
  168. return `${dataPath} ${err.message}`;
  169. } else if(err.keyword === "absolutePath") {
  170. const baseMessage = `${dataPath}: ${err.message}`;
  171. if(dataPath === "configuration.output.filename") {
  172. return `${baseMessage}\n` +
  173. "Please use output.path to specify absolute path and output.filename for the file name.";
  174. }
  175. return baseMessage;
  176. } else {
  177. // eslint-disable-line no-fallthrough
  178. return `${dataPath} ${err.message} (${JSON.stringify(err, 0, 2)}).\n${getSchemaPartText(err.parentSchema)}`;
  179. }
  180. }
  181. }
  182. module.exports = WebpackOptionsValidationError;