packages/config: added must* variant for reading required primitive values

This commit is contained in:
Patrik Oldsberg
2020-06-17 17:15:31 +02:00
parent 9793bebce4
commit 31585800a4
3 changed files with 61 additions and 0 deletions
+25
View File
@@ -49,6 +49,10 @@ function expectValidValues(config: ConfigReader) {
'string1',
'string2',
]);
expect(config.mustNumber('zero')).toBe(0);
expect(config.mustBoolean('true')).toBe(true);
expect(config.mustString('string')).toBe('string');
expect(config.mustStringArray('strings')).toEqual(['string1', 'string2']);
const [config1, config2, config3] = config.getConfigArray('nestlings');
expect(config1.getBoolean('boolean')).toBe(true);
@@ -57,6 +61,9 @@ function expectValidValues(config: ConfigReader) {
}
function expectInvalidValues(config: ConfigReader) {
expect(() => config.getBoolean('string')).toThrow(
'Invalid type in config for key string, got string, wanted boolean',
);
expect(() => config.getNumber('string')).toThrow(
'Invalid type in config for key string, got string, wanted number',
);
@@ -87,6 +94,18 @@ function expectInvalidValues(config: ConfigReader) {
expect(() => config.getConfigArray('one')).toThrow(
'Invalid type in config for key one, got number, wanted object-array',
);
expect(() => config.mustBoolean('missing')).toThrow(
"Missing required config value at 'missing'",
);
expect(() => config.mustNumber('missing')).toThrow(
"Missing required config value at 'missing'",
);
expect(() => config.mustString('missing')).toThrow(
"Missing required config value at 'missing'",
);
expect(() => config.mustStringArray('missing')).toThrow(
"Missing required config value at 'missing'",
);
}
describe('ConfigReader', () => {
@@ -210,6 +229,12 @@ describe('ConfigReader with fallback', () => {
// Config arrays aren't merged either
expect(config.getConfigArray('merged.configs').length).toBe(1);
expect(config.getConfigArray('merged.configs')[0].getString('a')).toBe('a');
expect(config.getConfigArray('merged.configs')[0].mustString('a')).toBe(
'a',
);
expect(() =>
config.getConfigArray('merged.configs')[0].mustString('missing'),
).toThrow("Missing required config value at 'missing'");
expect(
config.getConfigArray('merged.configs')[0].getString('b'),
).toBeUndefined();
+32
View File
@@ -92,6 +92,14 @@ export class ConfigReader implements Config {
return (configs ?? []).map(obj => new ConfigReader(obj));
}
mustNumber(key: string): number {
const value = this.getNumber(key);
if (value === undefined) {
throw new Error(`Missing required config value at '${key}'`);
}
return value;
}
getNumber(key: string): number | undefined {
return this.readConfigValue(
key,
@@ -99,6 +107,14 @@ export class ConfigReader implements Config {
);
}
mustBoolean(key: string): boolean {
const value = this.getBoolean(key);
if (value === undefined) {
throw new Error(`Missing required config value at '${key}'`);
}
return value;
}
getBoolean(key: string): boolean | undefined {
return this.readConfigValue(
key,
@@ -106,6 +122,14 @@ export class ConfigReader implements Config {
);
}
mustString(key: string): string {
const value = this.getString(key);
if (value === undefined) {
throw new Error(`Missing required config value at '${key}'`);
}
return value;
}
getString(key: string): string | undefined {
return this.readConfigValue(
key,
@@ -114,6 +138,14 @@ export class ConfigReader implements Config {
);
}
mustStringArray(key: string): string[] {
const value = this.getStringArray(key);
if (value === undefined) {
throw new Error(`Missing required config value at '${key}'`);
}
return value;
}
getStringArray(key: string): string[] | undefined {
return this.readConfigValue(key, values => {
if (!Array.isArray(values)) {
+4
View File
@@ -32,10 +32,14 @@ export type Config = {
getConfigArray(key: string): Config[];
getNumber(key: string): number | undefined;
mustNumber(key: string): number;
getBoolean(key: string): boolean | undefined;
mustBoolean(key: string): boolean;
getString(key: string): string | undefined;
mustString(key: string): string;
getStringArray(key: string): string[] | undefined;
mustStringArray(key: string): string[];
};