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