feat: pagina contatti e api email con validazione testata

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-02 01:26:49 +02:00
parent be4d933ca3
commit e9840105a3
5 changed files with 170 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
export interface ContactData {
firstName: string; lastName: string; phone: string;
email: string; message: string; website: string;
}
const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
function str(v: unknown, max: number): string {
return typeof v === 'string' ? v.trim().slice(0, max + 1) : '';
}
export function validateContact(data: unknown):
| { ok: true; value: ContactData }
| { ok: false; error: string } {
if (typeof data !== 'object' || data === null) return { ok: false, error: 'Dati non validi.' };
const d = data as Record<string, unknown>;
const value: ContactData = {
firstName: str(d.firstName, 100),
lastName: str(d.lastName, 100),
phone: str(d.phone, 40),
email: str(d.email, 200),
message: str(d.message, 5000),
website: str(d.website, 200),
};
if (value.website) return { ok: false, error: 'Richiesta non valida.' };
if (!value.firstName) return { ok: false, error: 'Il nome è obbligatorio.' };
if (!EMAIL_RE.test(value.email)) return { ok: false, error: 'Inserisci una e-mail valida.' };
if (!value.message) return { ok: false, error: 'Il messaggio è obbligatorio.' };
if (value.message.length > 5000) return { ok: false, error: 'Messaggio troppo lungo.' };
return { ok: true, value };
}