All quizzesMedium
Custom Pipeline Elements
Preview — 3 of 10 questions
Which interface must a custom pipe implement, and what method does it provide?
javascript
import { PipeTransform, Injectable, ArgumentMetadata } from '@nestjs/common';
@Injectable()
export class TrimPipe implements PipeTransform<string, string> {
transform(value: string, _metadata: ArgumentMetadata): string {
return typeof value === 'string' ? value.trim() : value;
}
}APipeTransform with transform()
BCanActivate with canActivate()
CNestInterceptor with intercept()
DExceptionFilter with catch()
In transform(value, metadata), what does the second argument metadata describe?
javascript
import { PipeTransform, ArgumentMetadata, Injectable } from '@nestjs/common';
@Injectable()
export class InspectPipe implements PipeTransform {
transform(value: unknown, metadata: ArgumentMetadata): unknown {
// metadata.type === 'body' | 'query' | 'param' | 'custom'
// metadata.metatype === e.g. CreateUserDto
// metadata.data === e.g. 'id'
return value;
}
}AThe argument being processed: its type, metatype, and optional data key
BThe HTTP response headers
CThe list of other pipes in the chain
DThe authenticated user
A pipe should only validate when it runs on the request body, not on params/query. Which metadata field do you check?
javascript
import { PipeTransform, ArgumentMetadata, Injectable } from '@nestjs/common';
@Injectable()
export class BodyOnlyPipe implements PipeTransform {
transform(value: unknown, metadata: ArgumentMetadata): unknown {
if (metadata.type !== 'body') return value; // skip params/query
// validate body here
return value;
}
}Ametadata.data === 'body'
Bmetadata.metatype === 'body'
Cmetadata.type === 'body'
Dmetadata.source === 'body'
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.