diff --git a/src/index.ts b/src/index.ts index 5d6ea385..c0c68e08 100644 --- a/src/index.ts +++ b/src/index.ts @@ -83,6 +83,7 @@ export * from './semver' export * from './web' export * from './abort' export * from './polyfill' +export * from './string/regex' export * from './zod/zod.util' export * from './zod/zod.shared.schemas' import { z, ZodSchema, ZodError, ZodIssue } from 'zod' diff --git a/src/string/regex.test.ts b/src/string/regex.test.ts new file mode 100644 index 00000000..5d218eb8 --- /dev/null +++ b/src/string/regex.test.ts @@ -0,0 +1,25 @@ +import { zEmail } from '../zod/zod.shared.schemas' +import { zIsValid } from '../zod/zod.util' +import { SIMPLE_EMAIL_REGEX } from './regex' + +test.each(['a@b.cc', 'kirill@naturalcycles.com', 'kirill@naturalcycles.co.uk'])( + 'email valid %', + s => { + expect(s).toMatch(SIMPLE_EMAIL_REGEX) + // cross-check with Zod + expect(zIsValid(s, zEmail)).toBe(true) + }, +) + +test.each([ + 'kirill@@naturalcycles.com', + 'kirill@naturalcycles..com', + 'kirill@naturalcyclescom', + 'kirillnaturalcycles.com', + '@kirillnaturalcycles.com', + 'kirill@naturalcycles.com@', +])('email invalid %', s => { + expect(s).not.toMatch(SIMPLE_EMAIL_REGEX) + // cross-check with Zod + expect(zIsValid(s, zEmail)).toBe(false) +}) diff --git a/src/string/regex.ts b/src/string/regex.ts new file mode 100644 index 00000000..4b1ef235 --- /dev/null +++ b/src/string/regex.ts @@ -0,0 +1,6 @@ +/** + * Simple, intentionally not exhaustive regex to match an email address. + * + * Source: https://regexr.com/3e48o + */ +export const SIMPLE_EMAIL_REGEX = /^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/