feat: add Zod validation for auth endpoints

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
BOHA
2026-03-23 08:49:54 +01:00
parent 7689b28d6d
commit a4303b0188
6 changed files with 62 additions and 21 deletions

View File

@@ -0,0 +1,21 @@
import { z } from 'zod';
export const LoginSchema = z.object({
username: z.string().min(1, 'Uživatelské jméno je povinné'),
password: z.string().min(1, 'Heslo je povinné'),
remember_me: z.boolean().optional().default(false),
});
export const TotpVerifySchema = z.object({
login_token: z.string().min(1, 'Token je povinný'),
totp_code: z.string().length(6, 'Kód musí mít 6 číslic'),
});
export const TotpBackupSchema = z.object({
login_token: z.string().min(1, 'Token je povinný'),
backup_code: z.string().min(1, 'Záložní kód je povinný'),
});
export type LoginInput = z.infer<typeof LoginSchema>;
export type TotpVerifyInput = z.infer<typeof TotpVerifySchema>;
export type TotpBackupInput = z.infer<typeof TotpBackupSchema>;

12
src/schemas/common.ts Normal file
View File

@@ -0,0 +1,12 @@
import { ZodSchema, ZodError } from 'zod';
export function parseBody<T>(schema: ZodSchema<T>, body: unknown): { data: T } | { error: string } {
try {
return { data: schema.parse(body) };
} catch (e) {
if (e instanceof ZodError) {
return { error: e.errors.map(err => err.message).join(', ') };
}
return { error: 'Neplatný požadavek' };
}
}