Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safer config access #496

Closed
LoicPoullain opened this issue Jul 29, 2019 · 1 comment
Closed

Safer config access #496

LoicPoullain opened this issue Jul 29, 2019 · 1 comment

Comments

@LoicPoullain
Copy link
Member

LoicPoullain commented Jul 29, 2019

Issues

1. Empty config values

In some situations, we may want the Config class to throw an error if a configuration value is not specified. For example, when calling Config.get with "mongodb.uri", we probably want to throw an error if we did not provide the database URI.

2. Strict type checking

The Config.get method coerces the configuration value when possible (for example 'true' becomes true). But it does not guarantee that the type of the configuration value is correct.

For example with the below configuration, Config.get<boolean>('settings.debug') returns a string whereas its TypeScript type is boolean.

settings:
  debug: 'an incorrect value'

Solution

Add two new methods getOrThrow and get2:

// If the config value is true, then return true.
// If the config value is false, then return false.
// If the config value is "true", then return true.
// If the config value is "false", then return false.
// If the config value is "xxx", then throw an error.
// If the config value is not defined, then return the default value (true).
config.get2('settings.foobar', 'boolean', true): boolean;

// Same as above except that if the value is not defined, the method throws an error.
config.getOrThrow('settings.foobar', 'boolean'): boolean;

// Same as above except that if the value is not defined, the method returns undefined.
config.get2('settings.foobar', 'boolean'): boolean | undefined;

// Same with numbers
config.get2('settings.foobar', 'number', 3): number;
config.getOrThrow('settings.foobar', 'number'): number;
config.get2('settings.foobar', 'number'): number | undefined;

// Same with strings
config.get2('settings.foobar', 'string', 'a'): string;
config.getOrThrow('settings.foobar', 'string'): string;
config.get2('settings.foobar', 'string'): string | undefined;

// "Same" with any
config.get2('settings.foobar', 'any', 'a'): any;
config.getOrThrow('settings.foobar', 'any'): any;
config.get2('settings.foobar', 'any'): any;
@LoicPoullain LoicPoullain added this to Backlog in Issue tracking via automation Jul 29, 2019
@LoicPoullain LoicPoullain mentioned this issue Jul 29, 2019
8 tasks
@LoicPoullain LoicPoullain moved this from Backlog to To Do This Release (August) in Issue tracking Jul 29, 2019
@LoicPoullain LoicPoullain moved this from To Do This Release (August) to Maybe / Pending / To think about in Issue tracking Aug 13, 2019
@LoicPoullain LoicPoullain moved this from Maybe / Pending / To think about to Work In Progress in Issue tracking Feb 15, 2020
@LoicPoullain LoicPoullain mentioned this issue Feb 28, 2020
5 tasks
@LoicPoullain LoicPoullain moved this from Work In Progress to Done / Closed This Release in Issue tracking Mar 4, 2020
@LoicPoullain
Copy link
Member Author

Version 1.7 published

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Issue tracking
  
Done / Closed This Release
Development

No branches or pull requests

1 participant