6

Seems like a real pain in the brain...

There is a huge thread about this on github and other sites, many of them come down to using useContainer from the 'class-validator' but it does not work for me.

async function bootstrap() {
  const app = await NestFactory.create(ApplicationModule);
  useContainer(app, { fallback: true });
  await app.listen(3000);
}
bootstrap();

Here's the injectable:

@ValidatorConstraint({ name: 'uniqueOnDatabase', async: true })
@Injectable()
export class UniqueOnDatabase implements ValidatorConstraintInterface {
    constructor(
        private readonly userService: UserService,
    ) {}
    public async validate(val: any, args: ValidationArguments): Promise<boolean> {
        const user = await this.userService.retrieveOneByEmail(val);
        return !user;
    }

    public defaultMessage(args: ValidationArguments): string {
        return `User with such an email address already exists in the DB`;
    }
}

All I want to do is use my userService inside that UniqueOnDatabase class.

Here is the module where I am providing the UniqueOnDatabase:

import { Module, CacheModule } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { CacheConfigService } from 'src/config/cache/config.service';
import { CacheService } from './services/cache.service';
import { CodeGenService } from './services/code-gen.service';
import { UserExistanceValidationPipe } from './pipes/user-existance.validation.pipe';
import { UsersModule } from 'src/users/users.module';
import { UniqueOnDatabase } from './validators/unique-on-database.validator';

@Module({
  providers: [
    CacheService,
    CodeGenService,
    UniqueOnDatabase,
  ],
  imports: [
    CacheModule.registerAsync({
      imports: [ConfigModule],
      useClass: CacheConfigService,
    }),
    UsersModule,
  ],
  exports: [
    CacheService,
    CodeGenService,
    UniqueOnDatabase,
  ],
})
export class SharedModule {}
8
  • How is it not working for you? Do you get a certain error? Is it not working at start time, build time, run time? Commented May 29, 2020 at 14:00
  • 1
    @JayMcDoniel For example, it throws the following error Nest could not find Validator element (this provider does not exist in the current context). I'm importing useContainer from class-validator and then using that UniqueOnDatabase on a DTO located in a different module. Commented May 29, 2020 at 14:09
  • @JayMcDoniel What validator element does it mean? Commented May 29, 2020 at 14:09
  • Can you show the module where you are providing the UniqueOnDatabase validator? Commented May 29, 2020 at 14:21
  • 8
    @JayMcDoniel Aaah, seems like I've figured out the solution. I should have used useContainer(app.select(SharedModule), { fallbackOnErrors: true }); instead of what I did at first... Commented May 29, 2020 at 14:42

1 Answer 1

4

Thanks @Albert for answering your question.

Adding @Albert's answer just in case someone misses the comments:

@JayMcDoniel Aaah, seems like I've figured out the solution. I should have used useContainer(app.select(SharedModule), { fallbackOnErrors: true }); instead of what I did at first...

Thanks again @Albert

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.