mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-06 15:22:12 +03:00
Extracted validateBody, isValidationFailure, and loginSchema from the 935-line schemas.ts barrel file into a dedicated helpers.ts module. Updated 70 API route files to import directly from helpers.ts. Root cause: webpack on certain environments fails to resolve exports from the bottom of large barrel files (schemas.ts), causing '(0, O.Jb) is not a function' errors in production builds. Fix: Split validation helpers into a small dedicated module (helpers.ts) so webpack can correctly resolve all exports regardless of file size. - TypeScript compiles with 0 errors - All API routes updated to import from helpers.ts - schemas.ts re-exports from helpers.ts for backward compatibility
61 lines
1.4 KiB
TypeScript
61 lines
1.4 KiB
TypeScript
import { z } from "zod";
|
|
|
|
export const loginSchema = z.object({
|
|
password: z.string().min(1, "Password is required").max(200),
|
|
});
|
|
|
|
type ValidationErrorDetail = {
|
|
field: string;
|
|
message: string;
|
|
};
|
|
|
|
type ValidationErrorPayload = {
|
|
message: string;
|
|
details: ValidationErrorDetail[];
|
|
};
|
|
|
|
type ValidationSuccess<TData> = {
|
|
success: true;
|
|
data: TData;
|
|
};
|
|
|
|
type ValidationFailure = {
|
|
success: false;
|
|
error: ValidationErrorPayload;
|
|
};
|
|
|
|
export type ValidationResult<TData> = ValidationSuccess<TData> | ValidationFailure;
|
|
|
|
// ──── Helper ────
|
|
|
|
/**
|
|
* Parse and validate request body with a Zod schema.
|
|
* Returns { success: true, data } or { success: false, error }.
|
|
*/
|
|
export function validateBody<TSchema extends z.ZodTypeAny>(
|
|
schema: TSchema,
|
|
body: unknown
|
|
): ValidationResult<z.infer<TSchema>> {
|
|
const result = schema.safeParse(body);
|
|
if (result.success) {
|
|
return { success: true, data: result.data };
|
|
}
|
|
const issues = Array.isArray(result.error?.issues) ? result.error.issues : [];
|
|
return {
|
|
success: false,
|
|
error: {
|
|
message: "Invalid request",
|
|
details: issues.map((e) => ({
|
|
field: e.path.join("."),
|
|
message: e.message,
|
|
})),
|
|
},
|
|
};
|
|
}
|
|
|
|
export function isValidationFailure<TData>(
|
|
validation: ValidationResult<TData>
|
|
): validation is ValidationFailure {
|
|
return validation.success === false;
|
|
}
|