nestjs / config

Configuration module for Nest framework (node.js) 🍓

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`validationOptions.stripUnknown` not working as expected

emadzz opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When I set stripUnknown to true in validationOptions, I am still able to access environment variables that are not defined in the validationSchema.

Minimum reproduction code

https://gist.github.com/e-madz/372db892ea8eb3093e681e878339c66b

Steps to reproduce

app.module.ts

ConfigModule.forRoot({
  ignoreEnvFile: true,
  isGlobal: true,
  load: [appConfig],
  cache: true,
  validationSchema: Joi.object({
    // Nest.js
    NEST_DEBUG: Joi.boolean().default(false),

    // App
    // APP_PORT: Joi.number().default(3000),
    APP_ENV: Joi.string()
      .required()
      .valid('development', 'staging', 'production'), // 'test', 'provision'),
  }),
  validationOptions: {
    // allowUnknown: true,
    abortEarly: false,
    stripUnknown: true,
  },
}),

app.confing.ts (appConfig)

export default () => ({
  app: {
    port: parseInt(process.env.APP_PORT, 10) || 3000,
    env: process.env.APP_ENV,
  },
});

Expected behavior

Accessing process.env.APP_PORT inside the appConfig factory function should evaluate to undefined, and console.log(process.env) within the same function should only write environment variables that are defined in validationSchema.

Package version

2.0.0

NestJS version

8.0.0

Node.js version

17.6.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

joi version
17.6.0

I believe this is expected otherwise we'll need to iterate over every prop of process.env object just to delete those there aren't in the validatedConfig object below

const { error, value: validatedConfig } =
options.validationSchema.validate(config, validationOptions);