Skip to content

Commit 8fee016

Browse files
grenhallMattias Grenhall
andauthored
fix: update email regex to support special characters (#12181)
### What? It's impossible to create a user with special characters in their email in Payload CMS 3.35.0. The issue is that currently the regex looks like this: ...payload/packages/payload/src/fields/validations.ts (line 202-203): const emailRegex = /^(?!.*\.\.)[\w.%+-]+@[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)*\.[a-z]{2,}$/i This allows users that have the following characters in their email to be created: %, ., +, - The regex needs to get updated to the following: const emailRegex = /^(?!.*\.\.)[\w!#$%&'*+/=?^{|}~.-]+@a-z0-9?(?:.a-z0-9?)*.[a-z]{2,}$/i` This way all special characters `!#$%&'*+/=?^_{|}~.-`` are hereby OK to have in the email. I've added more test-cases to cover a couple of more scenarios in the forked repo. ### Why? The regex is missing some special characters that are allowed according to standards. ### How? * Go to the admin ui and try to create a user with any of the newly added special characters meaning (!#$%&'*+/=?^_{|}~.-`) * You should get a validation error. However with the addition of the above code it should all check out. Fixes # #12180 --------- Co-authored-by: Mattias Grenhall <mattias.grenhall@assaabloy.com>
1 parent 1b17df9 commit 8fee016

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

packages/payload/src/fields/validations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export const email: EmailFieldValidation = (
200200
* Supports multiple subdomains (e.g., user@sub.domain.example.com)
201201
*/
202202
const emailRegex =
203-
/^(?!.*\.\.)[\w.%+-]+@[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)*\.[a-z]{2,}$/i
203+
/^(?!.*\.\.)[\w!#$%&'*+/=?^`{|}~-](?:[\w!#$%&'*+/=?^`{|}~.-]*[\w!#$%&'*+/=?^`{|}~-])?@[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)*\.[a-z]{2,}$/i
204204

205205
if ((value && !emailRegex.test(value)) || (!value && required)) {
206206
return t('validation:emailAddress')

test/auth/int.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,7 @@ describe('Auth', () => {
10161016
expect(emailValidation('user.name+alias@example.co.uk', mockContext)).toBe(true)
10171017
expect(emailValidation('user-name@example.org', mockContext)).toBe(true)
10181018
expect(emailValidation('user@ex--ample.com', mockContext)).toBe(true)
1019+
expect(emailValidation("user'payload@example.org", mockContext)).toBe(true)
10191020
})
10201021

10211022
it('should not allow emails with double quotes', () => {
@@ -1045,5 +1046,11 @@ describe('Auth', () => {
10451046
expect(emailValidation('user@-example.com', mockContext)).toBe('validation:emailAddress')
10461047
expect(emailValidation('user@example-.com', mockContext)).toBe('validation:emailAddress')
10471048
})
1049+
it('should not allow emails that start with dot', () => {
1050+
expect(emailValidation('.user@example.com', mockContext)).toBe('validation:emailAddress')
1051+
})
1052+
it('should not allow emails that have a comma', () => {
1053+
expect(emailValidation('user,name@example.com', mockContext)).toBe('validation:emailAddress')
1054+
})
10481055
})
10491056
})

tsconfig.base.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
}
3232
],
3333
"paths": {
34-
"@payload-config": ["./test/query-presets/config.ts"],
34+
"@payload-config": ["./test/_community/config.ts"],
3535
"@payloadcms/admin-bar": ["./packages/admin-bar/src"],
3636
"@payloadcms/live-preview": ["./packages/live-preview/src"],
3737
"@payloadcms/live-preview-react": ["./packages/live-preview-react/src/index.ts"],

0 commit comments

Comments
 (0)