Files
nuxtfiber/lib/validations/auth.ts
Beyhan Oğur 7b2b27a42c first commit
2026-04-26 22:18:17 +03:00

43 lines
1.9 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { z } from 'zod'
export const loginSchema = z.object({
email: z.string().min(1, 'E-posta gerekli').email('Geçerli bir e-posta girin'),
password: z.string().min(1, 'Şifre gerekli'),
})
export const registerSchema = z.object({
email: z.string().min(1, 'E-posta gerekli').email('Geçerli bir e-posta girin'),
first_name: z.string().min(1, 'Ad gerekli').max(100, 'Ad en fazla 100 karakter olabilir'),
last_name: z.string().min(1, 'Soyad gerekli').max(100, 'Soyad en fazla 100 karakter olabilir'),
username: z.string().min(1, 'Kullanıcı adı gerekli').max(150, 'Kullanıcı adı en fazla 150 karakter olabilir'),
password: z.string().min(6, 'Şifre en az 6 karakter olmalı'),
password_confirm: z.string().min(1, 'Şifre tekrarı gerekli'),
}).refine((data) => data.password === data.password_confirm, {
message: 'Şifreler eşleşmiyor',
path: ['password_confirm'],
})
export type LoginInput = z.infer<typeof loginSchema>
export type RegisterInput = z.infer<typeof registerSchema>
export function getFirstZodError (err: z.ZodError): string {
const issues = (err as { issues?: Array<{ message?: string }>; errors?: Array<{ message?: string }> }).issues
?? (err as { errors?: Array<{ message?: string }> }).errors
?? []
const first = issues[0]
return first ? (first.message || 'Doğrulama hatası') : 'Doğrulama hatası'
}
/** Map Zod error to field-level errors for form display */
export function getFieldErrors (err: z.ZodError): Record<string, string> {
const issues = (err as { issues?: Array<{ path: unknown[]; message?: string }>; errors?: Array<{ path: unknown[]; message?: string }> }).issues
?? (err as { errors?: Array<{ path: unknown[]; message?: string }> }).errors
?? []
const out: Record<string, string> = {}
for (const e of issues) {
const path = e.path?.[0]
if (path != null && typeof path === 'string' && e.message) out[path] = e.message
}
return out
}