How to use the class-validator.useContainer function in class-validator

To help you get started, we’ve selected a few class-validator examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zerothstack / zeroth / src / common / validation / index.ts View on Github external
public get(staticClass: InjectableClass): T {
    try {
      if (!this.injector) {
        return this.noDependencyGet(staticClass);
      }

      return this.injector.get(staticClass);
    } catch (e) {
      return this.noDependencyGet(staticClass);
    }
  }
}

const hybridInjectorInstance: HybridInjector = new HybridInjector();
useContainer(hybridInjectorInstance);

/**
 * Get the (singleton) validator instance and assign the dependency injector so custom validators
 * can use the DI service.
 * @param injector
 * @returns {Validator}
 */
export function getValidator(injector: Injector): Validator {
  hybridInjectorInstance.injector = injector;

  return hybridInjectorInstance.get(Validator);
}

// This common export is used so that both the core and the implementing modules
// use the same instance. This is necessary as class-validator does a single export
// of the MetadataStorage class which is used as a singleton. Otherwise, the models
github xmlking / ngx-starter-kit / apps / api / src / main.hmr.ts View on Github external
async function bootstrap() {
  const app = await NestFactory.create(AppModule, { cors: true });
  const config: ConfigService = app.get(ConfigService);
  app.use(helmet());
  app.useGlobalPipes(
    new ValidationPipe({
      transform: true,
      whitelist: true,
      forbidNonWhitelisted: true,
      skipMissingProperties: false,
      forbidUnknownValues: true,
    }),
  );

  // Link DI container to class-validator
  useContainer(app.select(AppModule), { fallbackOnErrors: true });

  // for uploaded images
  // app.useStaticAssets(join(__dirname, './../public'));

  const openIdConf = await config.getOpenIdConfiguration();
  const options = new DocumentBuilder()
    .setTitle('Sumo API Docs')
    .setDescription('Sumo API for Ngx Starter Kit')
    .setExternalDoc('Github Repo', 'https://github.com/xmlking/ngx-starter-kit/tree/master/apps/api')
    .setVersion(config.getVersion())
    .addServer('/docs')
    // .addOAuth2(
    //   'implicit',
    //   openIdConf.authorization_endpoint,
    //   openIdConf.token_endpoint,
    // }
github xmlking / ngx-starter-kit / apps / api / src / main.ts View on Github external
async function bootstrap() {
  const app = await NestFactory.create(AppModule, { cors: true /* logger: ['log', 'error', 'warn'] */ });
  const config: ConfigService = app.get(ConfigService);
  app.use(helmet());
  app.useGlobalPipes(
    new ValidationPipe({
      transform: true,
      whitelist: true,
      forbidNonWhitelisted: true,
      skipMissingProperties: false,
      forbidUnknownValues: true,
    }),
  );

  // Link DI container to class-validator
  useContainer(app.select(AppModule), { fallbackOnErrors: true });

  // for uploaded images
  // app.useStaticAssets(join(__dirname, './../public'));

  const openIdConf = await config.getOpenIdConfiguration();
  const options = new DocumentBuilder()
    .setTitle('Sumo API Docs')
    .setDescription('Sumo API for Ngx Starter Kit')
    .setExternalDoc('Github Repo', 'https://github.com/xmlking/ngx-starter-kit/tree/master/apps/api')
    .setVersion(config.getVersion())
    .addOAuth2({
      type: 'oauth2',
      scheme: config.isProd() ? 'https' : 'http',
      flows: {
        implicit: {
          authorizationUrl: openIdConf.authorization_endpoint,
github w3tecch / express-typescript-boilerplate / src / loaders / iocLoader.ts View on Github external
export const iocLoader: MicroframeworkLoader = (settings: MicroframeworkSettings | undefined) => {

    /**
     * Setup routing-controllers to use typedi container.
     */
    routingUseContainer(Container);
    ormUseContainer(Container);
    classValidatorUseContainer(Container);
    typeGraphQLUseContainer(Container);
};
github neoteric-eu / nestjs-auth / src / app / app.dispatcher.ts View on Github external
private async createServer(): Promise {
		this.app = await NestFactory.create(AppModule, {
			logger: new AppLogger('Nest')
		});
		useContainer(this.app, {fallbackOnErrors: true});
		this.app.use(cors());
		if (config.isProduction) {
			this.app.use(helmet());
		}
		const options = new DocumentBuilder()
			.setTitle(config.name)
			.setDescription(config.description)
			.setVersion(config.version)
			.addBearerAuth()
			.build();

		const document = SwaggerModule.createDocument(this.app, options);
		SwaggerModule.setup('/swagger', this.app, document);
		this.logger.log(`Swagger is exposed at ${config.host}:${config.port}/swagger`);
	}