Skip to content

Commit

Permalink
refactor: refine some regex (#3495)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmenko committed Apr 22, 2024
1 parent d92d890 commit 2431917
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 42 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-spies-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-frontend/constants': patch
---

Refine some regex
4 changes: 4 additions & 0 deletions packages/constants/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@
"dependencies": {
"@babel/runtime": "^7.22.15",
"@babel/runtime-corejs3": "^7.22.15"
},
"devDependencies": {
"@types/jest": "^29.5.4",
"jest": "29.7.0"
}
}
14 changes: 8 additions & 6 deletions packages/constants/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import type { TCustomView } from './types/generated/settings';

/**
* The entryPointUriPath may be between 2 and 36 characters and only contain alphabetic lowercase characters,
* non-consecutive underscores and hyphens. Leading and trailing underscore and hyphens are also not allowed.
* The project key must be between 2 and 36 characters long. It can only contain alphanumeric lowercase characters (a-z, 0-9),
* up to two consecutive underscores (_) and hyphens (-). Leading and trailing underscore and hyphens are also not allowed.
*/
export const PROJECT_KEY_REGEX = /^[^-_#]([0-9a-z]|[-_](?![-_])){0,34}[^-_#]$/g;
export const PROJECT_KEY_REGEX =
/^[^-_#\W]([0-9a-z]|([-_]{0,2})(?!([-_]))){0,34}[^-_#\W]$/g;

/**
* The entryPointUriPath may be between 2 and 64 characters and only contain alphabetic lowercase characters,
* non-consecutive underscores and hyphens. Leading and trailing underscore and hyphens are also not allowed.
*/
export const ENTRY_POINT_URI_PATH_REGEX =
/^[^-_#]([0-9a-z]|[-_](?![-_])){0,62}[^-_#]$/g;
/^[^-_#\W]([0-9a-z]|[-_](?![-_])){0,62}[^-_#\W]$/g;

/**
* The permission group name may be between 2 and 64 characters and only contain alphanumeric lowercase characters and non-consecutive hyphens. Leading and trailing hyphens are also not allowed.
* The permission group name may be between 2 and 64 characters and only contain alphanumeric lowercase characters and non-consecutive hyphens.
* Leading and trailing hyphens are also not allowed.
*/
export const PERMISSION_GROUP_NAME_REGEX =
/^[^-#]([a-z]|[-](?![-])){0,62}[^-#]$/g;
/^[^-#\W]([a-z]|[-](?![-])){0,62}[^-#\W]$/g;

// DOM elements
export const PORTALS_CONTAINER_ID = 'portals-container';
Expand Down
138 changes: 138 additions & 0 deletions packages/constants/src/validations.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import {
PROJECT_KEY_REGEX,
ENTRY_POINT_URI_PATH_REGEX,
PERMISSION_GROUP_NAME_REGEX,
} from './constants';

function generateRandomString(length: number) {
return Array.from({ length: length + 1 }).join('x');
}

describe.each`
projectKey | isValid
${'hello'} | ${true}
${'i-am-a-valid-project-key'} | ${true}
${'-invalid'} | ${false}
${'_invalid'} | ${false}
${'x'} | ${false}
${'xy'} | ${true}
${generateRandomString(36)} | ${true}
${generateRandomString(37)} | ${false}
${'__'} | ${false}
${'--'} | ${false}
${'..'} | ${false}
${'@@'} | ${false}
${'??'} | ${false}
${'&&'} | ${false}
${'##'} | ${false}
${'invalid-'} | ${false}
${'invalid_'} | ${false}
${'not valid'} | ${false}
${'this should not be valid'} | ${false}
${'this--is-valid'} | ${true}
${'this__is-valid'} | ${true}
${'this-_is-valid'} | ${true}
${'this_-is-valid'} | ${true}
${'this---is-not-valid'} | ${false}
${'this___is-not-valid'} | ${false}
${'this-_-is-not-valid'} | ${false}
${'This-Is-Not-VALID'} | ${false}
`(
'Validating project key "$projectKey"',
({ projectKey, isValid }: { projectKey: string; isValid: boolean }) => {
it(`should validate the key as ${isValid}`, () => {
const matched = projectKey.match(PROJECT_KEY_REGEX);
expect(Boolean(matched)).toBe(isValid);
});
}
);

describe.each`
entryPointUriPath | isValid
${'hello'} | ${true}
${'i-am-a-valid-entry-point'} | ${true}
${'-invalid'} | ${false}
${'_invalid'} | ${false}
${'x'} | ${false}
${'xy'} | ${true}
${generateRandomString(64)} | ${true}
${generateRandomString(65)} | ${false}
${'__'} | ${false}
${'--'} | ${false}
${'..'} | ${false}
${'@@'} | ${false}
${'??'} | ${false}
${'&&'} | ${false}
${'##'} | ${false}
${'invalid-'} | ${false}
${'invalid_'} | ${false}
${'not valid'} | ${false}
${'this should not be valid'} | ${false}
${'this--is-not-valid'} | ${false}
${'this__is-not-valid'} | ${false}
${'this-_is-not-valid'} | ${false}
${'this_-is-not-valid'} | ${false}
${'this---is-not-valid'} | ${false}
${'this___is-not-valid'} | ${false}
${'this-_-is-not-valid'} | ${false}
${'This-Is-Not-VALID'} | ${false}
`(
'Validating entry point uri path "$entryPointUriPath"',
({
entryPointUriPath,
isValid,
}: {
entryPointUriPath: string;
isValid: boolean;
}) => {
it(`should validate the key as ${isValid}`, () => {
const matched = entryPointUriPath.match(ENTRY_POINT_URI_PATH_REGEX);
expect(Boolean(matched)).toBe(isValid);
});
}
);

describe.each`
permissionGroup | isValid
${'hello'} | ${true}
${'i-am-a-valid-entry-point'} | ${true}
${'-invalid'} | ${false}
${'_valid'} | ${true}
${'x'} | ${false}
${'xy'} | ${true}
${generateRandomString(64)} | ${true}
${generateRandomString(65)} | ${false}
${'__'} | ${true}
${'--'} | ${false}
${'..'} | ${false}
${'@@'} | ${false}
${'??'} | ${false}
${'&&'} | ${false}
${'##'} | ${false}
${'invalid-'} | ${false}
${'valid_'} | ${true}
${'not valid'} | ${false}
${'this should not be valid'} | ${false}
${'this--is-not-valid'} | ${false}
${'this__is-not-valid'} | ${false}
${'this-_is-not-valid'} | ${false}
${'this_-is-not-valid'} | ${false}
${'this---is-not-valid'} | ${false}
${'this___is-not-valid'} | ${false}
${'this-_-is-not-valid'} | ${false}
${'This-Is-Not-VALID'} | ${false}
`(
'Validating permission group name "$permissionGroup"',
({
permissionGroup,
isValid,
}: {
permissionGroup: string;
isValid: boolean;
}) => {
it(`should validate the key as ${isValid}`, () => {
const matched = permissionGroup.match(PERMISSION_GROUP_NAME_REGEX);
expect(Boolean(matched)).toBe(isValid);
});
}
);
56 changes: 20 additions & 36 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2431917

Please sign in to comment.