@nestjs/common#ValidationError TypeScript Examples

The following examples show how to use @nestjs/common#ValidationError. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: validation.exceptions.ts    From nest-js-boilerplate with MIT License 7 votes vote down vote up
function transform(errors: ValidationError[]) {
  return errors.map((error) => {
    return {
      detail: `${error.property} validation error`,
      source: { pointer: `data/attributes/${error.property}` },
      meta: error.constraints ? Object.values(error.constraints) : null,
    };
  });
}
Example #2
Source File: _main.ts    From nest-js-boilerplate with MIT License 6 votes vote down vote up
async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  app.useGlobalPipes(new ValidationPipe({
    exceptionFactory: (errors: ValidationError[]) => new ValidationExceptions(errors),
  }));
  app.useGlobalFilters(new AllExceptionsFilter());

  const configService = app.get(ConfigService);
  const port = configService.get<number>('SERVER_POR') || 3000;

  const options = new DocumentBuilder()
    .setTitle('Api v1')
    .setDescription('The boilerplate API for nestjs devs')
    .setVersion('1.0')
    .addBearerAuth({ in: 'header', type: 'http' })
    .build();
  const document = SwaggerModule.createDocument(app, options);

  SwaggerModule.setup('api', app, document);

  await app.listen(port, async () => {
    console.log(`The server is running on ${port} port: http://localhost:${port}/api`);
  });
}
Example #3
Source File: local.strategy.ts    From nest-js-boilerplate with MIT License 6 votes vote down vote up
async validate(req: ExpressRequest, email: string, password: string): Promise<ValidateUserOutput> {
    const errors = await validate(new SignInDto(req.body)) as ValidationError[];

    if (errors.length > 0) {
      throw new ValidationExceptions(errors);
    }

    const user = await this.authService.validateUser(email, password);

    if (!user) {
      throw new UnauthorizedException();
    }

    return user;
  }
Example #4
Source File: validation.exceptions.ts    From nest-js-boilerplate with MIT License 5 votes vote down vote up
constructor(public validationErrors: ValidationError[]) {
    super({ errorType: 'ValidationError', errors: transform(validationErrors) });
  }