Skip to content

Commit

Permalink
fix: CQDG-817 set and filter name regex
Browse files Browse the repository at this point in the history
  • Loading branch information
adipaul1981 committed Aug 2, 2024
1 parent 634c41e commit a741716
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/db/models/SavedFilter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DataTypes, Model } from 'sequelize';

import { NAME_REGEX, UUID_VERSION } from '../../utils/constants';
import { SET_FILTER_NAME_REGEX, UUID_VERSION } from '../../utils/constants';
import { handleUniqueName } from '../../utils/savedFilters';
import sequelizeConnection from '../config';

Expand Down Expand Up @@ -53,7 +53,7 @@ SavedFilterModel.init(
title: {
type: DataTypes.TEXT,
validate: {
is: NAME_REGEX,
is: SET_FILTER_NAME_REGEX,
},
},
tag: DataTypes.TEXT,
Expand Down
4 changes: 2 additions & 2 deletions src/db/models/UserSets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DataTypes, Model } from 'sequelize';

import { NAME_REGEX, UUID_VERSION } from '../../utils/constants';
import { SET_FILTER_NAME_REGEX, UUID_VERSION } from '../../utils/constants';
import sequelizeConnection from '../config';

interface IUserSetAttributes {
Expand Down Expand Up @@ -50,7 +50,7 @@ UserSetModel.init(
type: DataTypes.STRING,
allowNull: false,
validate: {
is: NAME_REGEX,
is: SET_FILTER_NAME_REGEX,
},
},
sharedpublicly: {
Expand Down
1 change: 1 addition & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const UUID_VERSION = 4;
export const NAME_REGEX = /^[\u0027-\u0029\u002F-\u0039\u0040\u0061-\u007A\u00C0-\uFFFF ,.'\-_]+$/iu; // see regex.test.ts to understand the regex
export const SET_FILTER_NAME_REGEX = /^[\w\s()\-_,.|:'[\]]{1,200}$/iu; // see regex.test.ts to understand the regex
export const LINKEDIN_REGEX = /^(http(s)?:\/\/)?([\w]+\.)?linkedin\.com\/(pub|in|profile)\/([-a-zA-Z0-9]+)\/*/iu;
export const MAX_LENGTH_PER_ROLE = 100;
39 changes: 38 additions & 1 deletion src/utils/regex.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NAME_REGEX } from './constants';
import { NAME_REGEX, SET_FILTER_NAME_REGEX } from './constants';

describe('NAME_REGEX', () => {
it('should allow alphanumeric lower and uppercase', () => {
Expand Down Expand Up @@ -47,3 +47,40 @@ describe('NAME_REGEX', () => {
expect(NAME_REGEX.test('a~a')).toBeFalsy();
});
});

describe('SET_NAME_REGEX', () => {
it('should allow alphanumeric lower and uppercase', () => {
expect(SET_FILTER_NAME_REGEX.test('ThisIsAlphanumeric123')).toBeTruthy();
});
it(`should allow / @ ( ) ' - _ , . space`, () => {
expect(SET_FILTER_NAME_REGEX.test(`( ) ' - _ , . [ ] : |`)).toBeTruthy();
});
it(`should not allow ; { } ^ ! " # $ % & * + < = > ? \\ ~`, () => {
expect(SET_FILTER_NAME_REGEX.test('a;a')).toBeFalsy();
expect(SET_FILTER_NAME_REGEX.test('a{a')).toBeFalsy();
expect(SET_FILTER_NAME_REGEX.test('a}a')).toBeFalsy();
expect(SET_FILTER_NAME_REGEX.test('a^a')).toBeFalsy();
expect(SET_FILTER_NAME_REGEX.test('a!a')).toBeFalsy();
expect(SET_FILTER_NAME_REGEX.test('a"a')).toBeFalsy();
expect(SET_FILTER_NAME_REGEX.test('a#a')).toBeFalsy();
expect(SET_FILTER_NAME_REGEX.test('a$a')).toBeFalsy();
expect(SET_FILTER_NAME_REGEX.test('a%a')).toBeFalsy();
expect(SET_FILTER_NAME_REGEX.test('a&a')).toBeFalsy();
expect(SET_FILTER_NAME_REGEX.test('a*a')).toBeFalsy();
expect(SET_FILTER_NAME_REGEX.test('a+a')).toBeFalsy();
expect(SET_FILTER_NAME_REGEX.test('a<a')).toBeFalsy();
expect(SET_FILTER_NAME_REGEX.test('a=a')).toBeFalsy();
expect(SET_FILTER_NAME_REGEX.test('a>a')).toBeFalsy();
expect(SET_FILTER_NAME_REGEX.test('a?a')).toBeFalsy();
expect(SET_FILTER_NAME_REGEX.test('a\\a')).toBeFalsy();
expect(SET_FILTER_NAME_REGEX.test('a~a')).toBeFalsy();
});
it(`should not allow more than 200 character names`, () => {
expect(
SET_FILTER_NAME_REGEX.test(
'aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa ' +
'aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa aaaaaaaaaa',
),
).toBeFalsy();
});
});

0 comments on commit a741716

Please sign in to comment.