Comments (6)
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.
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.
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.
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.
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.
This library is pretty straight forward. It only contains three main steps (after config is loaded):
- normalize the config with
normalize
function - validate it against schema with validateWithClassValidator function
- 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)
- Matching between environment variables & config file properties HOT 6
- @ValidateNested doesn't work without custom validate function HOT 11
- Default values are overriding loaders? HOT 2
- Bug/Feature request: only load environment variables that are specified in the config file HOT 2
- [peerDependencies] Incompatible with nestjs version 9 HOT 1
- Default value is ignored if property is undefined
- env var substitutions break if the variable isn't set HOT 3
- Nested configs won't initialize if no environment variable of that nested config is set HOT 2
- Webpack builds in monorepos are broken HOT 2
- Request to update readme / docs to use ValidateNested instead of Allow where it makes sense HOT 1
- External Configuration "Validity" Check. HOT 2
- Build failed with version 2.5.0 HOT 1
- v2.5.0 has a failing build HOT 1
- Expanding env variables is not working anymore in v2.5.1 HOT 4
- optionalDependencies should be peerDependencies HOT 4
- Add ability to specify a default value for environment variable in the yaml file HOT 1
- New Release for NestJS 10 support HOT 1
- How to: async subset config sub modules? HOT 1
- Different throw exception behaviour on config syntax error when an array of loaders is given HOT 1
- Dependency lodash.set has an unpatched CVE HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nest-typed-config.