it-gorillaz / configify

NestJS Config on Steroids
MIT License
61 stars 4 forks source link

Support for Zod validator instead of class-validator #26

Open arun-pirai opened 4 months ago

arun-pirai commented 4 months ago

Hi,

Great library to use with Nest JS. I would like to use zod through out the app as a validator library.

It would be great if this library can use zod schema for validating env variables.

tommelo commented 4 months ago

sure, we can think of adding support to zod. Do you already have an idea on how you would like to use it with zod or how to implement the support for it?

arun-pirai commented 4 months ago

I want to define zod schema with all validation logic for each group of env like

export const AppEnvSchema = z.object({
  NODE_ENV: EnvironmentsSchema.default('local'),
  APP_NAME: z.string().optional().default('Demo'),
  API_SERVICE_PORT: z.coerce.number().optional().default(3000),
  DEPLOYMENT_SERVICE_PORT: z.coerce.number().optional().default(3000),
  COSTING_SERVICE_PORT: z.coerce.number().optional().default(3000),
  CCM_SERVICE_PORT: z.coerce.number().optional().default(3000)
});

export type AppEnvType = z.infer<typeof AppEnvSchema>;

and I want to pass this schema either in @Configuration() decorator or in constructor of the class.

@Configuration()
export class ApplicationClientConfig {
constructor(){}
}

so that I can consume each env variable using config variable provided in dependency injection.

currently the parsing is done in ConfigModule as below.

@Module({
  imports: [
    ConfigModule.forRoot({
      envFilePath: getEnvFilePath((process.env['NODE_ENV'] as EnvironmentsType) || Environments.DEVELOPMENT),
      validate: (env) => AppEnvSchema.parse(env),
      isGlobal: true,
      expandVariables: true,
      cache: true,
    }),
  ],
  controllers: [],
  providers: [],
  exports: [],
})
export class CoreModule {}