Pipes
A pipe is a class which implements the PipeTransform
interface.
Pipes have two typical use cases:
- transformation: transform input data to the desired form (e.g., from string to integer)
- validation: evaluate input data and if valid, simply pass it through unchanged; otherwise, throw an exception
The built-in ValidationPipe
The ValidationPipe
is provided by Fastwa out-of-the-box. The built-in ValidationPipe
offers more options than the sample we built in this chapter, which has been kept basic for the sake of illustrating the mechanics of a custom-built pipe.
Class validator
Let's look at an alternate implementation for our validation technique.
1import { IsString, IsEmail } from 'class-validator';
2
3export class CreateUserDto {
4 @IsString()
5 name: string;
6
7 @IsEmail()
8 email: string;
9}
Good to know: Read more about the class-validator decorators here.
Global scoped pipes
We can realize it's full utility by setting it up as a global-scoped pipe so that it is applied to every command handler across the entire application.
1async function bootstrap() {
2 const { version } = await fetchLatestBaileysVersion();
3 const { state, saveCreds } = await useMultiFileAuthState('./sessions');
4
5 const app = await WAFactory.create(AppModule, {
6 version,
7 auth: state,
8 printQRInTerminal: true,
9 saveCreds
10 });
11
12 app.useGlobalPipes(new ValidationPipe());
13
14 await app.listen();
15}
16bootstrap();