SPB Git forge

spb/hilmacorp

Public
1commits 1branches 0releases
29.2 MBsize
maindefault branch
19 days agolast push
TypeScript 93.3% JavaScript 4.4% CSS 2.3%
1.4 KB · 48 lines typescript
Raw Blame History
1/**2 * Author: Simon-Pierre Boucher3 * Contact: contact@spboucher.ai4 * Project: Hilmacorp.ai — Web Platform5 * File: lib/validate.ts6 * Description: Pure validation logic for the contact form (shared by the API route and tests)7 */89export interface ContactPayload {10  name: string;11  organization: string;12  email: string;13  message: string;14}1516export interface ValidationResult {17  ok: boolean;18  errors: Partial<Record<keyof ContactPayload, string>>;19  data?: ContactPayload;20}2122const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;23const MAX_FIELD = 500;24const MAX_MESSAGE = 5000;2526function clean(value: unknown, max: number): string {27  if (typeof value !== "string") return "";28  return value.trim().slice(0, max);29}3031export function validateContact(input: unknown): ValidationResult {32  const raw = (input ?? {}) as Record<string, unknown>;33  const data: ContactPayload = {34    name: clean(raw.name, MAX_FIELD),35    organization: clean(raw.organization, MAX_FIELD),36    email: clean(raw.email, MAX_FIELD),37    message: clean(raw.message, MAX_MESSAGE),38  };3940  const errors: ValidationResult["errors"] = {};41  if (data.name.length < 2) errors.name = "invalid_name";42  if (!EMAIL_RE.test(data.email)) errors.email = "invalid_email";43  if (data.message.length < 10) errors.message = "invalid_message";4445  const ok = Object.keys(errors).length === 0;46  return ok ? { ok, errors, data } : { ok, errors };47}48