[Best Practice] Boolean Values in Config Files
See original GitHub issueDear all,
thanks for this wonderful package. I recently stumbled upon an issue where i would like to get your input on how to deal with this properly (e.g., best practice).
Consider the following config file:
// config/debug.ts
export default {
debug_mode: process.env.APP_DEBUG || false,
};
Basically, it reads the .env
file to get the APP_DEBUG
config value, otherwise it is set to false
.
Now, if i want to use it like this in a service:
if (configService.get('debug.debug_mode', false) === true) {
// do something that needs to be done, if the debug mode is enabled, e.g., extensive logging
}
this is never executed, because the value, that is read from the .env
file is interpreted as string
(e.g., typeof configService.get('debug.debug_mode')
is string
and not boolean
as one would guess.
How do you deal with boolean values in .env
files specifically?
For now, the best approach for me is as follows
// "cast" it as boolean
const enabled = !! configService.get('debug.debug_mode', false);
if (enabled === true) { /* ... */ }
What would you suggest? All the best
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
How to set boolean values in an INI configuration file?
I've seen a variety of ways used to set boolean values in INI files: variable = ...
Read more >How to Write a Configuration file in Python | by Xiaoxu Gao
In this article, I want to share with you some good practices of ... one is Boolean type as it's able to recognize...
Read more >Guidelines for changing configuration files - IBM
General guidelines. Use the following general guidelines when you change the configuration settings: ; Default values. Use the following guidelines when changing ...
Read more >What is a best practice to represent a boolean value in a shell ...
In general when a script interprets an environment variable to be either true or false, it will interpret any value at all to...
Read more >Tips on naming boolean variables - Cleaner Code
Boolean that verifies that every case is true · Boolean that verifies that one of many cases is true · Avoid custom prefixes...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi guys! Let me share my approach if you don’t mind 😃
Cheers!
i just tried the
env-var
approach and it works like a charm! I really (!) like it!