forked from babel/babel
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
validator.ts
80 lines (75 loc) · 2.17 KB
/
validator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { findSuggestion } from "./find-suggestion";
export class OptionValidator {
declare descriptor: string;
constructor(descriptor: string) {
this.descriptor = descriptor;
}
/**
* Validate if the given `options` follow the name of keys defined in the `TopLevelOptionShape`
*
* @param {Object} options
* @param {Object} TopLevelOptionShape
* An object with all the valid key names that `options` should be allowed to have
* The property values of `TopLevelOptionShape` can be arbitrary
* @memberof OptionValidator
*/
validateTopLevelOptions(options: Object, TopLevelOptionShape: Object): void {
const validOptionNames = Object.keys(TopLevelOptionShape);
for (const option of Object.keys(options)) {
if (!validOptionNames.includes(option)) {
throw new Error(
this.formatMessage(`'${option}' is not a valid top-level option.
- Did you mean '${findSuggestion(option, validOptionNames)}'?`),
);
}
}
}
// note: we do not consider rewrite them to high order functions
// until we have to support `validateNumberOption`.
validateBooleanOption<T>(
name: string,
value?: boolean,
defaultValue?: T,
): boolean | T {
if (value === undefined) {
return defaultValue;
} else {
this.invariant(
typeof value === "boolean",
`'${name}' option must be a boolean.`,
);
}
return value;
}
validateStringOption<T>(
name: string,
value?: string,
defaultValue?: T,
): string | T {
if (value === undefined) {
return defaultValue;
} else {
this.invariant(
typeof value === "string",
`'${name}' option must be a string.`,
);
}
return value;
}
/**
* A helper interface copied from the `invariant` npm package.
* It throws given `message` when `condition` is not met
*
* @param {boolean} condition
* @param {string} message
* @memberof OptionValidator
*/
invariant(condition: boolean, message: string): void {
if (!condition) {
throw new Error(this.formatMessage(message));
}
}
formatMessage(message: string): string {
return `${this.descriptor}: ${message}`;
}
}