LibreChat/client/src/utils/email.ts
Dustin Healy 639c7ad6ad
📬 feat: Agent Support Email Address Validation (#9128)
* fix: email-regex realtime updates and looser validation

* feat: add zod validation to email address input

* refactor: emailValidation to email
2025-08-19 11:07:01 -04:00

28 lines
803 B
TypeScript

import { z } from 'zod';
/**
* Zod email validation schema
* Uses Zod's built-in email validation which is more robust than simple regex
* Based on: https://zod.dev/api?id=emails
*/
export const emailSchema = z.string().email();
/**
* Validates an email address using Zod
* @param email - The email address to validate
* @param errorMessage - Optional custom error message (defaults to Zod's message)
* @returns true if valid, error message if invalid
*/
export const validateEmail = (email: string, errorMessage?: string): true | string => {
if (!email || email.trim() === '') {
return true;
}
const result = emailSchema.safeParse(email);
return (
result.success ||
errorMessage ||
result.error.errors[0]?.message ||
'Please enter a valid email address'
);
};