#Class-validator with Dynamic Data Source in NestJS

1 messages · Page 1 of 1 (latest)

empty summit
#

Hi everyone,
I recently switched to a dynamic data source approach in my NestJS application. However, I'm encountering an issue with custom validators that were previously relying on a static ORM setup.
When one of these validators is triggered, I get the following error:
TypeError: Cannot read properties of undefined (reading 'getRepository')
at EntityExistsConstraint.validate (/app/src/shared/validators/entity-exists.validator.ts:24:40)

To address this, I tried injecting the EntityManager with a REQUEST scope in the validator, similar to how I handled services. Unfortunately, the error persists

Has anyone faced a similar issue or can offer advice on how to correctly configure class-validator to work in the context of a dynamic data source?

Thanks in advance for any help!

empty summit
#

Solution:
In main wrap AppModule in useContainer which allows to integrate class-validators with Dependency Injection system in NestJS. That helps us to get context of request inside of validator. We need to add DataSource to <ValidationArguments> which are base informations injected to validator.