Giter Site home page Giter Site logo

Comments (6)

Nikaple avatar Nikaple commented on June 7, 2024

The main concept of this module is that you can connect a Schema class with actual config. But under your situation, how would you want to consume your configurations? I mean, if I have two files, but both of them are type: A, which config the following code would inject?

class Service {
  constructor(private aConfig: AConfig) {}
}

Please further illustrate your use case, and I guess if you can define your config schema with class-transformer, it will be easy to integrate with nest-typed-config.

from nest-typed-config.

phhoef avatar phhoef commented on June 7, 2024

Thanks for your quick reply and pointing out the main concept of your module.
That's really helpful for me to understand - it's the very first time I am using nest-typed-config

I want to have different storage configurations.
For example, one is S3, another one filesystem, another one is FTP and so on.
All configs are for the same purpose, but they can differ depending on their type.

Would it be better to do it like this:

- name: 'test1'
  type: A
  endPoint: endpoint1.com
  port: 9000
  anyProp: 'abc'

- name: 'test2'
  type: B
  endPoint: endpoint2.com
  port: 443
  path: /path/to/anything/

Then it would be an array of these types. But again, each type can repeated as there might be several storages of type B. That is a problem, when I got you right, as the IoC container could not resolve the config

from nest-typed-config.

Nikaple avatar Nikaple commented on June 7, 2024

Then it's a class-transformer question, rather than a question of nest-typed-config, here's some useful information:

I found them from a class-transformer issue

from nest-typed-config.

phhoef avatar phhoef commented on June 7, 2024

Thanks for pointing this out.
For now, I've simplified the config so, that the structure is the same for all array elements.
It is a nested config now

export class AConfig{
    @Allow()
    type: 'A'

    @Allow()
    @IsUrl()
    endPoint: string

    @Allow()
    @IsNumber()
    port: number

    @Allow()
    @IsNotEmpty()
    anyProp: string
}

export class RootConfig {
    @Allow()
    @IsNotEmpty()
    @IsArray()
    @ValidateNested({ each: true, message: 'blabla'})
    @Type(() => AConfig)
     configs: AConfig[]
};

I am getting the following error message:

  - config config.0.undefined does not match the following rules:
  - unknownValue: an unknown value was passed to the validate function, current config is `undefined`

I think that the elements of the array are plain js objects are not converted to the according type.
Could that be possible? The @Type() annotation should take care of that, right?

from nest-typed-config.

phhoef avatar phhoef commented on June 7, 2024

When I define the validate function like you did in docs, it is working for some reason.
I do not understand why...?

Module import now looks like this

    TypedConfigModule.forRoot({
      schema: RootConfig,
      load: directoryLoader({ directory: BASE_PATH }),
      validate: (rawConfig: any) => {
        const config = plainToClass(RootConfig, rawConfig);
        const schemaErrors = validateSync(config, {
          forbidUnknownValues: true,
          whitelist: true,
        });

        if (schemaErrors.length) {
          throw new Error(TypedConfigModule.getConfigErrorMessage(schemaErrors));
        }

        return config as RootConfig;
      },
    }),

from nest-typed-config.

Nikaple avatar Nikaple commented on June 7, 2024

This library is pretty straight forward. It only contains three main steps (after config is loaded):

  1. normalize the config with normalize function
  2. validate it against schema with validateWithClassValidator function
  3. return all providers.

That said, if you can call validateSync(plainToClass(ConfigSchema, configObject)) with your own ConfigSchema and configObject, it's done. I've followed the docs of class-transformer for union types, and integrated it with nest-typed-config very smoothly.

import { plainToClass } from 'class-transformer';
import { validateSync } from 'class-validator';

If you have any further questions, please provide a minimum reproduction repo, you can create one via CodeSandbox

from nest-typed-config.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.