Skip to content

Commit 0f901b8

Browse files
committed
refactor: use zod schema for request payload validation
1 parent 7e7428f commit 0f901b8

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

apps/webapp/app/routes/admin.feature-flags.tsx

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useEffect, useState } from "react";
44
import stableStringify from "json-stable-stringify";
55
import { json } from "@remix-run/server-runtime";
66
import { redirect, typedjson, useTypedLoaderData } from "remix-typedjson";
7+
import { z } from "zod";
78
import { prisma } from "~/db.server";
89
import { requireUser } from "~/services/session.server";
910
import {
@@ -58,20 +59,13 @@ export const action = async ({ request }: ActionFunctionArgs) => {
5859
}
5960

6061
const body = await request.json();
61-
62-
if (
63-
typeof body !== "object" ||
64-
body === null ||
65-
Array.isArray(body) ||
66-
typeof body.flags !== "object" ||
67-
body.flags === null ||
68-
Array.isArray(body.flags)
69-
) {
62+
const payloadSchema = z.object({ flags: z.record(z.unknown()) });
63+
const parsed = payloadSchema.safeParse(body);
64+
if (!parsed.success) {
7065
return json({ error: "Invalid payload" }, { status: 400 });
7166
}
7267

73-
const newFlags = body.flags as Record<string, unknown>;
74-
const validationResult = validatePartialFeatureFlags(newFlags);
68+
const validationResult = validatePartialFeatureFlags(parsed.data.flags);
7569
if (!validationResult.success) {
7670
return json(
7771
{ error: "Invalid feature flags", details: validationResult.error.issues },

0 commit comments

Comments
 (0)