mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-01-13 14:08:51 +01:00
* fix: email-regex realtime updates and looser validation * feat: add zod validation to email address input * refactor: emailValidation to email
28 lines
803 B
TypeScript
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'
|
|
);
|
|
};
|