Skip to content

Commit d9777b2

Browse files
committed
fix: add export parseBody function to adminforth core
AdminForth/1777/image
1 parent 3606db3 commit d9777b2

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

adminforth/modules/utils.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import crypto from 'crypto';
66
import { AdminForthConfig, AdminForthResource, AdminForthResourceColumnInputCommon,Filters, IAdminForth, Predicate } from '../index.js';
77
import { RateLimiterMemory, RateLimiterAbstract } from "rate-limiter-flexible";
88
import { PERIOD_UNITS, type PeriodString, type PeriodUnit } from '../types/Back.js';
9+
import { z } from "zod";
10+
911
// @ts-ignore-next-line
1012

1113

@@ -644,4 +646,21 @@ export function applyRegexValidation(value, validation) {
644646
${horizontal}
645647
${RESET}
646648
`;
647-
}
649+
}
650+
651+
652+
export function parseBody<T>(
653+
schema: z.ZodType<T>,
654+
body: unknown,
655+
response: { setStatus: (code: number, message: string) => void },
656+
): { ok: true; data: T } | { ok: false; error: { error: string; details: unknown } } {
657+
const parsed = schema.safeParse(body ?? {});
658+
if (!parsed.success) {
659+
response.setStatus(400, '');
660+
return {
661+
ok: false,
662+
error: { error: 'Request body validation failed', details: parsed.error.issues },
663+
};
664+
}
665+
return { ok: true, data: parsed.data };
666+
}

0 commit comments

Comments
 (0)