feat: add Zod validation for auth endpoints
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
21
src/schemas/auth.schema.ts
Normal file
21
src/schemas/auth.schema.ts
Normal 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
12
src/schemas/common.ts
Normal 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' };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user