There is a nest.js project, where in the request body we expect an object, one property of this object contains stringified JSON value. The idea is to convert this string to an object, validate it and pass to controller as an object ValidationPipe set up:
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
transform: true,
}),
);
DTO:
@Transform(parseJson, { toClassOnly: true })
@Type(() => AdditionalInfo)
@IsNotEmptyObject()
@ValidateNested()
additionalInfo: AdditionalInfo;
parseJson function
export function parseJson(options: {
key: string;
value: string;
obj: string | Record<string, any>;
}): Record<string, any> {
try {
return JSON.parse(options.value);
} catch (e) {
throw new BadRequestException(`${options.key} contains invalid JSON `);
}
}
For some reason in the controller the parsed value gets lost, and we receive an empty object.
Looks like
@Transform
works well with primitives only. Decided to createParseJsonPipe
and use it instead. Usage (in the controller):ParseJsonPipe:
ValidationPipe
implementsPipeTransform
from@nestjs/common
,transform
function looks like that: