forked from Yara724/api
Initial commit after migration to gitea
This commit is contained in:
29
src/utils/pipes/parse-json.pipe.ts
Normal file
29
src/utils/pipes/parse-json.pipe.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import {
|
||||
PipeTransform,
|
||||
Injectable,
|
||||
BadRequestException,
|
||||
ArgumentMetadata,
|
||||
} from "@nestjs/common";
|
||||
|
||||
@Injectable()
|
||||
export class ParseJsonPipe implements PipeTransform {
|
||||
transform(value: string | undefined, metadata: ArgumentMetadata): any {
|
||||
if (value === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (typeof value !== "string") {
|
||||
throw new BadRequestException(
|
||||
`Validation failed: Expected a stringified JSON for query parameter '${metadata.data}'.`,
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(value);
|
||||
} catch (e) {
|
||||
throw new BadRequestException(
|
||||
`Invalid JSON format in query parameter '${metadata.data}'.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user