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