I have a custom pipe that original was used to parse a string from a route param such as “###-###” to an object with two properties equaling the values parsed.
When we use this to parse a single property of a request body, I get what I think is weird results for the transform method’s value.
If foo is provided in the body then the transform method’s value is a string with the correct body’s value.
But if foo is not provided in the body, the value when logged out is an instance of CustomPipeReturnType
The literal console log looks like this.
Is this expected behavior? I would have expected an undefined/null value. I asked on the nestjs discord but have received no answer there.
CustomPipeReturnType {
}
Below are the simplified classes involved. The pipe should take the SomeDto.foo property and return a CustomPipeReturnType with baz and fooz
export class SomeDto {
foo?: string;
bar: number;
}
export class CustomPipeReturnType {
baz: number;
fooz: number;
}
@Injectable()
export class CartIdentifier implements PipeTransform {
transform(value: any): CustomPipeReturnType {
if (!validString(value)) {
throw new BadRequestException();
}
const parts = value.split('-').map(Number);
const c = new CustomPipeReturnType();
const [baz, fooz] = parts;
c.baz = baz;
c.fooz = fooz;
return c;
}
}
@ApiTags('sometag')
@Controller({
path: 'somepath',
version: '1',
})
export class SomeController {
@Post('/:someparam/path')
someEndpoint(
@Param('someparam', CustomPipe) someParam: CustomPipeReturnType,
@Body() body: SomeDto,
@Body('foo', CustomPipe) pipeReturn?: CustomPipeReturnType
): Observable<void> {
return of();
}
}