From 014d1f17d6ed8c9f33cf4acfe7382afaf7192ba2 Mon Sep 17 00:00:00 2001 From: Confidence Okoghenun Date: Wed, 10 Nov 2021 14:59:24 +0100 Subject: [PATCH] fix: Disallows entity name to be any extra library name (#8755) Co-authored-by: sbalaji1192 --- app/client/cypress/support/commands.js | 4 ++-- .../editorComponents/ActionNameEditor.tsx | 2 +- .../src/pages/Editor/DataSourceEditor/FormTitle.tsx | 2 +- .../pages/Editor/JSEditor/JSObjectNameEditor.tsx | 2 +- app/client/src/sagas/PageSagas.tsx | 2 +- app/client/src/utils/DynamicBindingUtils.ts | 13 +++++++++++++ app/client/src/utils/helpers.tsx | 2 ++ 7 files changed, 21 insertions(+), 6 deletions(-) diff --git a/app/client/cypress/support/commands.js b/app/client/cypress/support/commands.js index e35f5a75bb..1a0629faef 100644 --- a/app/client/cypress/support/commands.js +++ b/app/client/cypress/support/commands.js @@ -996,7 +996,7 @@ Cypress.Commands.add("CreationOfUniqueAPIcheck", (apiname) => { .type(apiname, { force: true, delay: 500 }) .should("have.value", apiname); cy.get(".error-message").should(($x) => { - expect($x).contain(apiname.concat(" is already being used.")); + expect($x).contain(apiname.concat(" is already being used or is a restricted keyword.")); }); cy.get(apiwidget.apiTxt).blur(); }); @@ -1081,7 +1081,7 @@ Cypress.Commands.add("CreateApiAndValidateUniqueEntityName", (apiname) => { .type(apiname, { force: true }) .should("have.value", apiname); cy.get(".t--nameOfApi .error-message").should(($x) => { - expect($x).contain(apiname.concat(" is already being used.")); + expect($x).contain(apiname.concat(" is already being used or is a restricted keyword.")); }); }); diff --git a/app/client/src/components/editorComponents/ActionNameEditor.tsx b/app/client/src/components/editorComponents/ActionNameEditor.tsx index c28081c4ab..92a638d16a 100644 --- a/app/client/src/components/editorComponents/ActionNameEditor.tsx +++ b/app/client/src/components/editorComponents/ActionNameEditor.tsx @@ -120,7 +120,7 @@ export function ActionNameEditor(props: ActionNameEditorProps) { name !== currentActionConfig?.name && hasActionNameConflict(name) ) { - return `${name} is already being used.`; + return `${name} is already being used or is a restricted keyword.`; } return false; }, diff --git a/app/client/src/pages/Editor/DataSourceEditor/FormTitle.tsx b/app/client/src/pages/Editor/DataSourceEditor/FormTitle.tsx index ac5e3be1f7..48b3d85171 100644 --- a/app/client/src/pages/Editor/DataSourceEditor/FormTitle.tsx +++ b/app/client/src/pages/Editor/DataSourceEditor/FormTitle.tsx @@ -75,7 +75,7 @@ function FormTitle(props: FormTitleProps) { if (!name || name.trim().length === 0) { return "Please enter a valid name"; } else if (hasNameConflict(name)) { - return `${name} is already being used.`; + return `${name} is already being used or is a restricted keyword.`; } return false; }, diff --git a/app/client/src/pages/Editor/JSEditor/JSObjectNameEditor.tsx b/app/client/src/pages/Editor/JSEditor/JSObjectNameEditor.tsx index 7d7d334239..3990989427 100644 --- a/app/client/src/pages/Editor/JSEditor/JSObjectNameEditor.tsx +++ b/app/client/src/pages/Editor/JSEditor/JSObjectNameEditor.tsx @@ -107,7 +107,7 @@ export function JSObjectNameEditor(props: ActionNameEditorProps) { name !== currentJSObjectConfig?.name && hasNameConflict(name) ) { - return `${name} is already being used.`; + return `${name} is already being used or is a restricted keyword.`; } return false; }, diff --git a/app/client/src/sagas/PageSagas.tsx b/app/client/src/sagas/PageSagas.tsx index 1dbf51817c..0327d4a6b9 100644 --- a/app/client/src/sagas/PageSagas.tsx +++ b/app/client/src/sagas/PageSagas.tsx @@ -790,7 +790,7 @@ export function* updateWidgetNameSaga( type: ReduxActionErrorTypes.UPDATE_WIDGET_NAME_ERROR, payload: { error: { - message: `Entity name: ${action.payload.newName} is already being used.`, + message: `Entity name: ${action.payload.newName} is already being used or is a restricted keyword.`, }, }, }); diff --git a/app/client/src/utils/DynamicBindingUtils.ts b/app/client/src/utils/DynamicBindingUtils.ts index aa215cc33a..0592254b56 100644 --- a/app/client/src/utils/DynamicBindingUtils.ts +++ b/app/client/src/utils/DynamicBindingUtils.ts @@ -192,6 +192,19 @@ export const extraLibraries: ExtraLibrary[] = [ }, ]; +/** + * creates dynamic list of constants based on + * current list of extra libraries i.e lodash("_"), moment etc + * to be used in widget and entity name validations + */ +export const extraLibrariesNames = extraLibraries.reduce( + (prev: any, curr: any) => { + prev[curr.accessor] = curr.accessor; + return prev; + }, + {}, +); + export interface DynamicPath { key: string; value?: string; diff --git a/app/client/src/utils/helpers.tsx b/app/client/src/utils/helpers.tsx index 6cd9033ff9..192ae72471 100644 --- a/app/client/src/utils/helpers.tsx +++ b/app/client/src/utils/helpers.tsx @@ -22,6 +22,7 @@ import { getAppsmithConfigs } from "configs"; import { sha256 } from "js-sha256"; import moment from "moment"; import log from "loglevel"; +import { extraLibrariesNames } from "./DynamicBindingUtils"; const { cloudHosting, intercomAppID } = getAppsmithConfigs(); @@ -286,6 +287,7 @@ export const isNameValid = ( name in GLOBAL_FUNCTIONS || name in WINDOW_OBJECT_PROPERTIES || name in WINDOW_OBJECT_METHODS || + name in extraLibrariesNames || name in invalidNames ); };