Comments (13)
Is this considered a bad way of doing things?
Yes. There are 2 issues here:
- you should never import
ConfigModule#forRoot()
twice (as the name suggests, "forRoot") - you should never define
ConfigService
(from@nestjs/config
) as a provider
from config.
Yes, if it is a bug and I figure out how to fix it. Based on it's current setup I don't imagine anyone is using allowUnknown: false
because they would have to specify (in the schema) the other 227+ default environment variables that they may not be interacting with. Is this a bug or is this intended behavior?
process.env
is hardcoded here. To prevent affecting functionality in other code interacting with config
, it seems a good option would be for the user to pass a validation file here and have it default to .env
. Does that make any sense or is it better to make a change on loadEnvFile
and try to preserve the behavior of when users add custom environment files other than .env
? I'm not familiar with nestjs
codebase yet. My first nest new project-name
was the day I opened the ticket.
from config.
What if someone wants to validate the actual env variables (and their presence), not only those specified in the .env
file?
from config.
This is expected behavior, not a bug.
Firstly, joi
is a data validator for JavaScript, not only for nestjs, @nestjs/config
is just a usage scene.
Secondly, the validationOptions
option receives options for the validate
method of the joi
library. If validationOptions
is not provided, @nestjs/config
will return {abortEarly: false,allowUnknown: true}
.
- For the
allowUnknown
option, it defaults totrue
because thejoi
library'sallowUnknown
option defaults tofalse
and process.env has many environment variables. - For the
abortEarly
option, it defaults tofalse
because thejoi
library'sabortEarly
option defaults totrue
and to see which environment variables do not pass validation.
So it is not recommended to set the allowUnknown
option to false in this scenario.
The environment variables come from the env file and the runtime environment. So the best practice is to throw an exception during application startup if required environment variables haven't been provided or if they don't meet certain validation rules.
Don't bother with additional environment variables.
from config.
Would you like to create a PR for this issue?
from config.
@jtlindsey Yes, I realized the purpose of allowUnknown
was only relevant to singular ConfigModule
settings.
If you have multiple, obviously others will be unknown for it. So I stopped using it.
IMO, we should possibly store an array of all the validated env variables from all the different places where we used ConfigModule
and then check for there is still an unknown variable inside the .env
. But again, this setting must only correspond to the global module rather than all modules.
Not sure if I make sense here? 😅 CC: @kamilmysliwiec
from config.
What if someone wants to validate the actual env variables (and their presence), not only those specified in the
.env
file?
Is this directed to me?
According to me if let's say we have 2 modules SessionConfigModule
(eg SESSION_TTL
) & CacheConfigModule
(eg CACHE_TTL
), setting { allowUnknown: false }
in either of the modules should not throw any errors because both modules have the variables defined & validation added for them.
If let's say I add another variable QUEUE_LIMIT
, then allowUnknown
should throw an error, saying unknowns are not allowed.
Currently, setting { allowUnknown: false }
in either of the modules will throw an error, which isn't right I feel.
So as suggested in my previous message, if we are able to create a list of all the defined variables & validate all (run this) in one go instead of separately for each module, that should resolve the issue I think.
from config.
According to me if let's say we have 2 modules SessionConfigModule (eg SESSION_TTL) & CacheConfigModule (eg CACHE_TTL), setting { allowUnknown: false } in either of the modules should not throw any errors because both modules have the variables defined & validation added for them.
Not sure what you mean. ConfigModule#forRoot
should never be called more than once within a single application.
from config.
According to me if let's say we have 2 modules SessionConfigModule (eg SESSION_TTL) & CacheConfigModule (eg CACHE_TTL), setting { allowUnknown: false } in either of the modules should not throw any errors because both modules have the variables defined & validation added for them.
Not sure what you mean.
ConfigModule#forRoot
should never be called more than once within a single application.
@kamilmysliwiec How do you propose we allow creation of multiple config modules? For eg: AppConfigService
or CacheConfigService
?
AppConfigService
will only support env variables prefixed with APP_
& CacheConfigService
will only support env variables prefixed with CACHE_
?
from config.
@kamilmysliwiec How do you propose we allow creation of multiple config modules?
Create wrapper services that use the built-in ConfigService
under the hood.
from config.
@kamilmysliwiec Is this considered a bad way of doing things?
This helps me import: [CacheConfigModule]
or import: [AppConfigModule]
only where it is is required, rather than globally importing the ConfigModule
inside AppModule
.
from config.
@kamilmysliwiec Understood. Thanks.
from config.
What if someone wants to validate the actual env variables (and their presence), not only those specified in the
.env
file?
@kamilmysliwiec I am currently experiencing this issue and so I opted not to use the validation option anymore. It would be nice if we can have a flag that tells the config module to do a strict validation only to env
files or with .env
by default and add additional validation with env vars not included in any env
file/s if a user insist as you mentioned to validate an actual env var not included in any env
file/s.
@prateekkathal I happen to read about your blog about nestJS folder structuring as I am also new to the framework. I did a little tweak to avoid issues mentioned by kam (but I am not sure if this is correct too). I only imported ConfigModule
for each config module (app, db etc...) so I can inject ConfigService
in the each service and had the ConfigModule#forRoot
imported to my root config module which I am importing to the app module. This works for me, but I am not sure if this is the best structure.
from config.
Related Issues (20)
- Using optional environment variables leads to 'undefined' string value and incorrectly inferred type HOT 11
- Loaded config not validated HOT 4
- Update Type For "propertyPath" params in ConfigService.get HOT 1
- Allow nested properties be overridden by environment variables HOT 1
- Broken HTTP headers after upgrading from 2.3.2 to 2.3.3 HOT 1
- ConfigModule with validationSchema does not allow numbers HOT 6
- Class based configuration using decorators like @Configuration, @ConfigurationKey HOT 3
- Readonly ConfigType HOT 2
- Optional Registration using the ConfigModule HOT 9
- Eager loading of dotenv/.env HOT 4
- Support alternate parsers
- expandVariables option does not working correctly. HOT 1
- The function 'get' with default value and infer=true still returns undefined HOT 1
- Handle JS runtimes .env autoloading HOT 2
- Non-Reactive Behavior of NestJS ConfigService with Dynamic Environment Variable Updates HOT 3
- Validation is not performed on config loaded with custom loader HOT 2
- Feature to disable all configurations merge one object HOT 1
- ConfigType injection fails when used in a Module factory method. HOT 1
- ConditionalModule registerWhen support ConfigModule value HOT 1
- Add functionality to avoid support for .env files HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from config.