Skip to content

Commit

Permalink
🌍 feat: Extend regex to support international usernames (#1918)
Browse files Browse the repository at this point in the history
* 🌍 Extend regex to support international usernames

* update validators.spec.js
  • Loading branch information
fuegovic authored Feb 28, 2024
1 parent 2f92b54 commit 057fcf6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
16 changes: 15 additions & 1 deletion api/strategies/validators.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
const { z } = require('zod');

const allowedCharactersRegex = /^[a-zA-Z0-9_.@#$%&*()\p{Script=Latin}\p{Script=Common}]+$/u;
const allowedCharactersRegex = new RegExp(
'^[' +
'a-zA-Z0-9_.@#$%&*()' + // Basic Latin characters and symbols
'\\p{Script=Latin}' + // Latin script characters
'\\p{Script=Common}' + // Characters common across scripts
'\\p{Script=Cyrillic}' + // Cyrillic script for Russian, etc.
'\\p{Script=Devanagari}' + // Devanagari script for Hindi, etc.
'\\p{Script=Han}' + // Han script for Chinese characters, etc.
'\\p{Script=Arabic}' + // Arabic script
'\\p{Script=Hiragana}' + // Hiragana script for Japanese
'\\p{Script=Katakana}' + // Katakana script for Japanese
'\\p{Script=Hangul}' + // Hangul script for Korean
']+$', // End of string
'u', // Use Unicode mode
);
const injectionPatternsRegex = /('|--|\$ne|\$gt|\$lt|\$or|\{|\}|\*|;|<|>|\/|=)/i;

const usernameSchema = z
Expand Down
3 changes: 0 additions & 3 deletions api/strategies/validators.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,6 @@ describe('Zod Schemas', () => {

it('should reject invalid usernames', () => {
const invalidUsernames = [
'Дмитрий', // Cyrillic characters
'محمد', // Arabic characters
'张伟', // Chinese characters
'john{doe}', // Contains `{` and `}`
'j', // Only one character
'a'.repeat(81), // More than 80 characters
Expand Down

0 comments on commit 057fcf6

Please sign in to comment.