nest-typed-config icon indicating copy to clipboard operation
nest-typed-config copied to clipboard

env var substitutions break if the variable isn't set

Open aminnaggar opened this issue 2 years ago • 2 comments

I'm submitting a...


[ ] Regression 
[X] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request

Current behavior

If you use Environment Variable Substitution by setting ignoreEnvironmentVariableSubstitution to false and the environment variable is not set it throws an error crashing the app.

 Info  Webpack is building your sources...

webpack 5.73.0 compiled successfully in 416 ms
Type-checking in progress...

project/node_modules/nest-typed-config/lib/loader/file-loader.ts:90
      throw new Error(
            ^
Error: Environment variable is not set for variable name: 'NODE_ENV'
    at replace (project/node_modules/nest-typed-config/lib/loader/file-loader.ts:90:13)
    at String.replace (<anonymous>)
    at placeholderResolver (project/node_modules/nest-typed-config/lib/loader/file-loader.ts:100:19)
    at project/node_modules/nest-typed-config/lib/loader/file-loader.ts:147:30
    at Function.getRawConfig (project/node_modules/nest-typed-config/lib/typed-config.module.ts:76:12)
    at Function.forRoot (project/node_modules/nest-typed-config/lib/typed-config.module.ts:21:28)
    at Object../src/app.module.ts (project/dist/main.js:79:51)
    at __webpack_require__ (project/dist/main.js:937:42)
    at Object../src/main.ts (project/dist/main.js:700:22)
    at __webpack_require__ (project/dist/main.js:937:42)
No errors found.

Expected behavior

returning an undefined without throwing would be fantastic

Minimal reproduction of the problem with instructions

# .env.yml
testEnvs: ${NODE_ENV}
// in config schema
import { IsOptional, IsString } from 'class-validator';

export class Config {
  @IsString()
  @IsOptional()
  public readonly testEnvs?: string;
}
// in  app.module.ts module imports
TypedConfigModule.forRoot({
  schema: Config,
  load: fileLoader({
    ignoreEnvironmentVariableSubstitution: false,
  }),
  isGlobal: true,
}),

What is the motivation / use case for changing the behavior?

It would be better to respect the config schema, and allow optional fields to be treated as optional.

Environment


Nest version: 9.1.1

 
For Tooling issues:
- Node version: v18.8.0  
- Platform:  Linux Ubuntu

Others:

aminnaggar avatar Aug 30 '22 11:08 aminnaggar

In order to avoid breaking changes, I'd suggest make this feature opt-in by adding another optional property to FileLoaderOptions, such as allowUndefinedEnvironmentVariables. PRs are welcome.

Nikaple avatar Aug 30 '22 11:08 Nikaple

Lovely. Thank you!

aminnaggar avatar Aug 30 '22 12:08 aminnaggar

:tada: This issue has been resolved in version 2.4.6 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Sep 06 '22 08:09 github-actions[bot]