I have an auth module and the routing auth module contains the following routes:
const authRoutes: Routes = [
{
path: 'sign-in', component: SignInComponent
}
];
Run Code Online (Sandbox Code Playgroud)
The dashboard routing module contains the following routes:
const dashboardRoutes: Routes = [
{ path: 'home', component: HomeComponent, canActivate: [IsAuthenticatedGuard] }
];
Run Code Online (Sandbox Code Playgroud)
The app (root) routing module contains the following route configurations:
const appRoutes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: '**', component: PageNotFoundComponent }
];
Run Code Online (Sandbox Code Playgroud)
So when …
阅读 Nestjs 官方文档,我发现了以下实现ValidationPipe:
import { PipeTransform, Injectable, ArgumentMetadata, BadRequestException } from '@nestjs/common';
import { validate } from 'class-validator';
import { plainToClass } from 'class-transformer';
@Injectable()
export class ValidationPipe implements PipeTransform<any> {
async transform(value: any, { metatype }: ArgumentMetadata) {
if (!metatype || !this.toValidate(metatype)) {
return value;
}
const object = plainToClass(metatype, value);
const errors = await validate(object);
if (errors.length > 0) {
throw new BadRequestException('Validation failed');
}
return value;
}
private toValidate(metatype: Function): boolean {
const types: Function[] …Run Code Online (Sandbox Code Playgroud)