1 import { MedusaError } from "./errors"
2
3 const EMAIL_REGEX =
4 /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
5
6 /**
7 * Check whether provided string is an email.
8 * @param email - string to check
9 */
10 function isEmail(email: string) {
11 return email.toLowerCase().match(EMAIL_REGEX)
12 }
13
14 /**
15 * Used to validate user email.
16 * @param {string} email - email to validate
17 * @return {string} the validated email
18 */
19 export function validateEmail(email: string): string {
20 const validatedEmail = isEmail(email)
21
22 if (!validatedEmail) {
23 throw new MedusaError(
24 MedusaError.Types.INVALID_DATA,
25 "The email is not valid"
26 )
27 }
28
29 return email.toLowerCase()
30 }