Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
DEVPROD-933 Trim trailing and ending whitespace for project settings …
Browse files Browse the repository at this point in the history
…fields (#2267)
  • Loading branch information
khelif96 authored Feb 28, 2024
1 parent e02a2b2 commit 588a9a7
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/components/SpruceForm/customFormats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
validateJira,
validateJiraURL,
validateNoSpecialCharacters,
validateNoStartingOrTrailingWhitespace,
validatePercentage,
validateRegexp,
validateSlack,
Expand All @@ -17,6 +18,7 @@ export const customFormats = (jiraHost: string) => ({
noSpecialCharacters: validateNoSpecialCharacters,
// Permit empty string but disallow whitespace
noSpaces: /^$|^\S+$/,
noStartingOrTrailingWhitespace: validateNoStartingOrTrailingWhitespace,
validDuration: validateDuration,
validEmail: validateEmail,
validJiraTicket: validateJira,
Expand Down
5 changes: 5 additions & 0 deletions src/components/SpruceForm/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ export const transformErrors = (errors: AjvError[]) =>
...error,
message: "Value should not contain spaces.",
};
case "noStartingOrTrailingWhitespace":
return {
...error,
message: "Value should not start or end with whitespace.",
};
case "validDuration":
return {
...error,
Expand Down
2 changes: 2 additions & 0 deletions src/gql/generated/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2949,6 +2949,7 @@ export type UserSettings = {
region?: Maybe<Scalars["String"]["output"]>;
slackMemberId?: Maybe<Scalars["String"]["output"]>;
slackUsername?: Maybe<Scalars["String"]["output"]>;
timeFormat?: Maybe<Scalars["String"]["output"]>;
timezone?: Maybe<Scalars["String"]["output"]>;
useSpruceOptions?: Maybe<UseSpruceOptions>;
};
Expand All @@ -2964,6 +2965,7 @@ export type UserSettingsInput = {
region?: InputMaybe<Scalars["String"]["input"]>;
slackMemberId?: InputMaybe<Scalars["String"]["input"]>;
slackUsername?: InputMaybe<Scalars["String"]["input"]>;
timeFormat?: InputMaybe<Scalars["String"]["input"]>;
timezone?: InputMaybe<Scalars["String"]["input"]>;
useSpruceOptions?: InputMaybe<UseSpruceOptionsInput>;
};
Expand Down
2 changes: 1 addition & 1 deletion src/pages/projectSettings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const ProjectSettings: React.FC = () => {
projectData?.projectSettings?.projectRef?.repoRefId || identifier;

// Assign project type in order to show/hide elements that should only appear for repos, attached projects, etc.
let projectType;
let projectType: ProjectType;
if (isRepo) {
projectType = ProjectType.Repo;
} else if (projectData?.projectSettings?.projectRef?.repoRefId) {
Expand Down
1 change: 1 addition & 0 deletions src/pages/projectSettings/tabs/AccessTab/getFormSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const getFormSchema = (
title: "Username",
default: "",
minLength: 1,
format: "noStartingOrTrailingWhitespace",
},
},
},
Expand Down
4 changes: 4 additions & 0 deletions src/pages/projectSettings/tabs/GeneralTab/getFormSchema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const getFormSchema = (
branch: {
type: "string" as "string",
title: "Branch Name",
format: "noStartingOrTrailingWhitespace",
},
}),
other: {
Expand All @@ -75,6 +76,7 @@ export const getFormSchema = (
displayName: {
type: "string" as "string",
title: "Display Name",
format: "noStartingOrTrailingWhitespace",
},
...(projectType !== ProjectType.Repo && {
identifier: {
Expand All @@ -96,10 +98,12 @@ export const getFormSchema = (
remotePath: {
type: "string" as "string",
title: "Config File",
format: "noStartingOrTrailingWhitespace",
},
spawnHostScriptPath: {
type: "string" as "string",
title: "Spawn Host Script Path",
format: "noStartingOrTrailingWhitespace",
},
versionControlEnabled: {
type: ["boolean", "null"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export const getFormSchema = (
title: "Username",
default: "",
minLength: 1,
format: "noStartingOrTrailingWhitespace",
},
},
),
Expand All @@ -168,6 +169,7 @@ export const getFormSchema = (
title: "Team",
default: "",
minLength: 1,
format: "noStartingOrTrailingWhitespace",
},
},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,19 @@ export const getFormSchema = (
title: "Alias",
default: "",
minLength: 1,
format: "noStartingOrTrailingWhitespace",
},
childProjectIdentifier: {
type: "string" as "string",
title: "Project",
default: "",
minLength: 1,
format: "noStartingOrTrailingWhitespace",
},
parentAsModule: {
type: "string" as "string",
title: "Module",
format: "noStartingOrTrailingWhitespace",
},
status: {
type: "string" as "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const getFormSchema = (
title: "Config File",
minLength: 1,
default: "",
format: "noStartingOrTrailingWhitespace",
},
alias: {
type: "string" as "string",
Expand Down
2 changes: 2 additions & 0 deletions src/pages/projectSettings/tabs/PluginsTab/getFormSchema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export const getFormSchema = (
createProject: {
type: "string" as "string",
title: "",
format: "noStartingOrTrailingWhitespace",
},
},
},
Expand Down Expand Up @@ -204,6 +205,7 @@ export const getFormSchema = (
default: "",
minLength: 1,
maxLength: 40,
format: "noStartingOrTrailingWhitespace",
},
urlTemplate: {
type: "string" as "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ export const getFormSchema = (
title: "Project",
default: "",
minLength: 1,
format: "noStartingOrTrailingWhitespace",
},
configFile: {
type: "string" as "string",
title: "Config File",
default: "",
minLength: 1,
format: "noStartingOrTrailingWhitespace",
},
level: {
type: "string" as "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const getFormSchema = (
title: "Variable Name",
default: "",
minLength: 1,
format: "noStartingOrTrailingWhitespace",
},
varValue: {
type: "string" as "string",
Expand Down
11 changes: 11 additions & 0 deletions src/utils/validators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,24 @@ const validateNoSpecialCharacters = (str: string): boolean => {
return noSpecialCharacters.test(str);
};

/**
* `validateNoStartingOrTrailingWhitespace` tests if a provided string contains no starting or trailing whitespace
* @param str - The string to test.
* @returns - true if the string has no starting or trailing whitespace and false otherwise
*/
const validateNoStartingOrTrailingWhitespace = (str: string): boolean => {
const noStartingOrTrailingWhitespaceRegex = /^(?! ).*(?<! )$/;
return noStartingOrTrailingWhitespaceRegex.test(str);
};

export {
allowedSymbols,
validateDuration,
validateEmail,
validateJira,
validateJiraURL,
validateNoSpecialCharacters,
validateNoStartingOrTrailingWhitespace,
validateObjectId,
validatePercentage,
validateRegexp,
Expand Down
15 changes: 15 additions & 0 deletions src/utils/validators/validators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
validateJira,
validateJiraURL,
validateNoSpecialCharacters,
validateNoStartingOrTrailingWhitespace,
validateObjectId,
validateRegexp,
validateSlack,
Expand All @@ -24,6 +25,20 @@ describe("validateNoSpecialCharacters", () => {
});
});

describe("validateNoStartingOrTrailingWhitespace", () => {
it("returns true if string has no starting or trailing whitespace", () => {
expect(validateNoStartingOrTrailingWhitespace("")).toBe(true);
expect(validateNoStartingOrTrailingWhitespace("a")).toBe(true);
expect(validateNoStartingOrTrailingWhitespace("helloworld")).toBe(true);
expect(validateNoStartingOrTrailingWhitespace(" helloWorld123")).toBe(
false,
);
expect(validateNoStartingOrTrailingWhitespace("helloWorld123 ")).toBe(
false,
);
});
});

describe("validateObjectId", () => {
it("validates object ids", () => {
expect(validateObjectId("5f74d99ab2373627c047c5e5")).toBeTruthy();
Expand Down

0 comments on commit 588a9a7

Please sign in to comment.