Skip to content

Commit 797f663

Browse files
authored
fix: use a better email re pulled from zod closes #4585 (#4595)
1 parent a18c19f commit 797f663

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

.changeset/violet-apes-tan.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vee-validate/rules": patch
3+
---
4+
5+
fix: use a better email re pulled from zod closes #4585

packages/rules/src/email.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
/* eslint-disable no-useless-escape */
22
import { isEmpty } from './utils';
33

4+
// https://github.com/colinhacks/zod/blob/master/src/types.ts#L567
5+
const emailRE = /^(?!\.)(?!.*\.\.)([A-Z0-9_+-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
6+
47
const emailValidator = (value: unknown) => {
58
if (isEmpty(value)) {
69
return true;
710
}
8-
const re =
9-
/^(([^<>()\[\]\\.,;:\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,}))$/;
1011

1112
if (Array.isArray(value)) {
12-
return value.every(val => re.test(String(val)));
13+
return value.every(val => emailRE.test(String(val)));
1314
}
1415

15-
return re.test(String(value));
16+
return emailRE.test(String(value));
1617
};
1718

1819
export default emailValidator;

packages/rules/tests/email.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ test('validates that the string is a valid email address', () => {
44
expect(validate('[email protected]')).toBe(true);
55
expect(validate('[email protected]')).toBe(true);
66
expect(validate('[email protected]')).toBe(true);
7-
expect(validate('Pelé@example.com')).toBe(true);
87
expect(validate('[email protected]')).toBe(true);
98
expect(validate('[email protected]')).toBe(true);
109
expect(validate('[email protected]')).toBe(true);
@@ -15,6 +14,7 @@ test('validates that the string is a valid email address', () => {
1514
expect(validate([])).toBe(true);
1615

1716
// invalid
17+
expect(validate('Pelé@example.com')).toBe(false);
1818
expect(validate('@example.com')).toBe(false);
1919
expect(validate('@example')).toBe(false);
2020
expect(validate('undefined')).toBe(false);

0 commit comments

Comments
 (0)