#How to print environment schema

4 messages · Page 1 of 1 (latest)

tacit lodge
#

I am using joi to validate my the env variables that I need which works pretty well like:

@Module({
    imports: [
        ConfigModule.forRoot({
            validationSchema: Joi.object({
                FOLDER: Joi.string().default("../../tmp"),
                LOG_LEVEL: Joi.string()
                    .valid("trace", "debug", "info", "warn", "error", "fatal")
                    .default(
                        process.env.NODE_ENV === "production"
                            ? "warn"
                            : "debug",
                    ),
                CONFIG_IMPORT: Joi.boolean().default(false),
                CONFIG_IMPORT_FORCE: Joi.boolean().default(false),
                CONFIG_FOLDER: Joi.string().default("../../assets/config"),
                ...AUTH_VALIDATION_SCHEMA,                
                LOG_ENABLE_HTTP_LOGGER: Joi.boolean().default(false),
                LOG_ENABLE_SESSION_LOGGER: Joi.boolean().default(false),
                LOG_DEBUG_MODE: Joi.boolean().default(false),
                LOG_FORMAT: Joi.string()
                    .valid("json", "pretty")
                    .default(
                        process.env.NODE_ENV === "production"
                            ? "json"
                            : "pretty",
                    ),
            }),
            isGlobal: true,
            expandVariables: true,
        }),

However it would be really cool to print some description which would it make easier for devs to understand the meaning of a variable (like it can be done with json schemas).

Is there a way when I add my values with Joi.boolean().description("will enable logging") that will print me out the description and also the dependancies (only important when x is equal to y)

drifting oyster
#

I personally like nestjs-typed-config. You can have the config schema (ts) file set up and use jsdoc comments, if you wish to explain or describe the config properties. Something like:

// src/config/app.config.ts
import { IsNumber, IsString, IsEnum, IsUrl } from 'class-validator';

export enum Environment {
  Development = 'development',
  Production = 'production',
  Test = 'test',
}

/**
 * A class defining the application's configuration.
 *
 * This class is used by the `nest-typed-config` module to validate
 * and provide a type-safe interface for your environment variables.
 */
export class AppConfig {
  /**
   * The port on which the NestJS application will run.
   */
  @IsNumber()
  APP_PORT: number;

  /**
   * The environment in which the application is running.
   * Valid values are 'development', 'production', or 'test'.
   */
  @IsEnum(Environment)
  NODE_ENV: Environment;

  /**
   * The connection URL for the database.
   */
  @IsString()
  @IsUrl({ require_tld: false, protocols: ['postgres', 'mysql', 'mongodb'] })
  DATABASE_URL: string;
}

The other usual best practice is to offer a .env.example file, which also has comments about what a property is all about and dummy values that need to be replaced.

# The port on which the NestJS application will run.
APP_PORT=3000

# The environment in which the application is running (e.g., development, production).
# Valid values are: development, production, test.
NODE_ENV=development

# The connection URL for the database.
# Example: postgres://user:password@localhost:5432/mydatabase
DATABASE_URL=postgres://root:root@localhost:5432/mydatabase

And lastly, to answer your question, when would you want to show the descriptions? You could store the joi object in a variable and use onModuleInit to output the information about the joi object with the .describe() method during bootup theoretically.

tacit lodge
#

Thanks for the response @drifting oyster
I want it for my documentation. So with the same approach of class validators and the openapi lib, that I can also print the condititoins to the user like

=== config ===
CONFIG_PRINT_FORMAT          = text                      # Format for printing config
CONFIG_IMPORT                = false                     # Run one-off config import on startup
CONFIG_IMPORT_FORCE          = false                     # Force overwrite on config import
CONFIG_FOLDER                = ../../assets/config       # Path to config import folder

=== auth ===
OIDC                         = (unset)                   # Enable OIDC mode  [optional]
OIDC_INTERNAL_ISSUER_URL     = (unset)                   # Internal issuer URL in OIDC mode [when OIDC is set → then required]

I used the describe() and meta() function of joi and then comsumed the joi objects to construct the output like above. The config approach with yml files looks also nice, but I liked the approach of env variables so I can e.g. share the same variable from my .env file with my docker compose db and also pass it to the nestjs instacte

drifting oyster
#

Ok. So now I'm uncertain about what you need or are asking us for. It sounds like you have the problem solved already?