Adds safe json parse of the body

It ensures that the body complies with the safe parsing of the JSON so that if the body sent is not a valid JSON, we convert it into an empty JSON. Would improve in handling the response of such cases as a follow up for improved UX
pull/9078/head
Syed Ali Shahbaz 2022-10-07 12:46:35 +05:30 committed by GitHub
parent 12f54773f9
commit 36a0ebfbad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 1 deletions

View File

@ -2,6 +2,7 @@ import type { NextApiRequest, NextApiResponse } from "next";
import { getAvailabilityFromSchedule, DEFAULT_SCHEDULE } from "@calcom/lib/availability"; import { getAvailabilityFromSchedule, DEFAULT_SCHEDULE } from "@calcom/lib/availability";
import safeParseJSON from "@lib/helpers/safeParseJSON";
import { withMiddleware } from "@lib/helpers/withMiddleware"; import { withMiddleware } from "@lib/helpers/withMiddleware";
import { ScheduleResponse, SchedulesResponse } from "@lib/types"; import { ScheduleResponse, SchedulesResponse } from "@lib/types";
import { import {
@ -14,7 +15,7 @@ async function createOrlistAllSchedules(
{ method, body, userId, isAdmin, prisma }: NextApiRequest, { method, body, userId, isAdmin, prisma }: NextApiRequest,
res: NextApiResponse<SchedulesResponse | ScheduleResponse> res: NextApiResponse<SchedulesResponse | ScheduleResponse>
) { ) {
const safe = schemaScheduleBodyParams.safeParse(body); const safe = schemaScheduleBodyParams.safeParse(safeParseJSON(body));
if (!safe.success) { if (!safe.success) {
res.status(400).json({ message: "Bad request" }); res.status(400).json({ message: "Bad request" });