feat: handle string representation of boolean values in the config
Signed-off-by: Sayak Mukhopadhyay <mukhopadhyaysayak@gmail.com>
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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} */
|
||||
|
||||
Reference in New Issue
Block a user