validator.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.OptionValidator = void 0;
  6. var _findSuggestion = require("./find-suggestion.js");
  7. class OptionValidator {
  8. constructor(descriptor) {
  9. this.descriptor = descriptor;
  10. }
  11. validateTopLevelOptions(options, TopLevelOptionShape) {
  12. const validOptionNames = Object.keys(TopLevelOptionShape);
  13. for (const option of Object.keys(options)) {
  14. if (!validOptionNames.includes(option)) {
  15. throw new Error(this.formatMessage(`'${option}' is not a valid top-level option.
  16. - Did you mean '${(0, _findSuggestion.findSuggestion)(option, validOptionNames)}'?`));
  17. }
  18. }
  19. }
  20. validateBooleanOption(name, value, defaultValue) {
  21. if (value === undefined) {
  22. return defaultValue;
  23. } else {
  24. this.invariant(typeof value === "boolean", `'${name}' option must be a boolean.`);
  25. }
  26. return value;
  27. }
  28. validateStringOption(name, value, defaultValue) {
  29. if (value === undefined) {
  30. return defaultValue;
  31. } else {
  32. this.invariant(typeof value === "string", `'${name}' option must be a string.`);
  33. }
  34. return value;
  35. }
  36. invariant(condition, message) {
  37. if (!condition) {
  38. throw new Error(this.formatMessage(message));
  39. }
  40. }
  41. formatMessage(message) {
  42. return `${this.descriptor}: ${message}`;
  43. }
  44. }
  45. exports.OptionValidator = OptionValidator;
  46. //# sourceMappingURL=validator.js.map