feat: handle string representation of boolean values in the config

Signed-off-by: Sayak Mukhopadhyay <mukhopadhyaysayak@gmail.com>
This commit is contained in:
Sayak Mukhopadhyay
2022-12-25 20:32:09 +05:30
parent 8e489d089e
commit ac1e91c252
2 changed files with 22 additions and 3 deletions
+4 -1
View File
@@ -87,8 +87,11 @@ function expectValidValues(config: ConfigReader) {
}
function expectInvalidValues(config: ConfigReader) {
expect(() => config.getBoolean('zero')).toThrow(
"Invalid type in config for key 'zero' in 'ctx', got number, wanted boolean",
);
expect(() => config.getBoolean('string')).toThrow(
"Invalid type in config for key 'string' in 'ctx', got string, wanted boolean",
"Unable to convert config value for key 'string' in 'ctx' to a boolean",
);
expect(() => config.getNumber('string')).toThrow(
"Unable to convert config value for key 'string' in 'ctx' to a number",
+18 -2
View File
@@ -280,10 +280,26 @@ export class ConfigReader implements Config {
/** {@inheritdoc Config.getOptionalBoolean} */
getOptionalBoolean(key: string): boolean | undefined {
return this.readConfigValue(
const value = this.readConfigValue<string | boolean>(
key,
value => typeof value === 'boolean' || { expected: 'boolean' },
val =>
typeof val === 'boolean' ||
typeof val === 'string' || { expected: 'boolean' },
);
if (typeof value === 'boolean' || value === undefined) {
return value;
}
let boolean;
if (value === 'true') {
boolean = true;
} else if (value === 'false') {
boolean = false;
} else {
throw new Error(
errors.convert(this.fullKey(key), this.context, 'boolean'),
);
}
return boolean;
}
/** {@inheritdoc Config.getString} */