From ffbcb0022e9deea2e83845d64c0dc71a7f389ed9 Mon Sep 17 00:00:00 2001 From: Hailong Cui Date: Thu, 18 Jul 2024 10:24:04 +0800 Subject: [PATCH 1/4] add workspace blank check Signed-off-by: Hailong Cui --- .../public/components/workspace_form/utils.test.ts | 12 ++++++++++++ .../public/components/workspace_form/utils.ts | 2 +- src/plugins/workspace/server/routes/index.ts | 12 ++++++++++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/plugins/workspace/public/components/workspace_form/utils.test.ts b/src/plugins/workspace/public/components/workspace_form/utils.test.ts index 1f8b7ee04fd2..3256f255b0a4 100644 --- a/src/plugins/workspace/public/components/workspace_form/utils.test.ts +++ b/src/plugins/workspace/public/components/workspace_form/utils.test.ts @@ -161,6 +161,18 @@ describe('validateWorkspaceForm', () => { message: 'Name is required. Enter a name.', }); }); + it('should return error if name is empty string', () => { + expect(validateWorkspaceForm({ name: '' }, false).name).toEqual({ + code: WorkspaceFormErrorCode.WorkspaceNameMissing, + message: 'Name is required. Enter a name.', + }); + }); + it('should return error if name is blank string', () => { + expect(validateWorkspaceForm({ name: ' ' }, false).name).toEqual({ + code: WorkspaceFormErrorCode.WorkspaceNameMissing, + message: 'Name is required. Enter a name.', + }); + }); it('should return error if name is invalid', () => { expect(validateWorkspaceForm({ name: '~' }, false).name).toEqual({ code: WorkspaceFormErrorCode.InvalidWorkspaceName, diff --git a/src/plugins/workspace/public/components/workspace_form/utils.ts b/src/plugins/workspace/public/components/workspace_form/utils.ts index 9e714a268614..7588178d8c94 100644 --- a/src/plugins/workspace/public/components/workspace_form/utils.ts +++ b/src/plugins/workspace/public/components/workspace_form/utils.ts @@ -309,7 +309,7 @@ export const validateWorkspaceForm = ( ) => { const formErrors: WorkspaceFormErrors = {}; const { name, permissionSettings, features, selectedDataSources } = formData; - if (name) { + if (name && name.trim()) { if (!isValidFormTextInput(name)) { formErrors.name = { code: WorkspaceFormErrorCode.InvalidWorkspaceName, diff --git a/src/plugins/workspace/server/routes/index.ts b/src/plugins/workspace/server/routes/index.ts index e76ce412d261..b57b0a529d1c 100644 --- a/src/plugins/workspace/server/routes/index.ts +++ b/src/plugins/workspace/server/routes/index.ts @@ -46,13 +46,21 @@ const workspaceOptionalAttributesSchema = { reserved: schema.maybe(schema.boolean()), }; +const workspaceNameSchema = schema.string({ + validate(value) { + if (!value || value.trim().length === 0) { + return "can't be empty or blank."; + } + }, +}); + const createWorkspaceAttributesSchema = schema.object({ - name: schema.string(), + name: workspaceNameSchema, ...workspaceOptionalAttributesSchema, }); const updateWorkspaceAttributesSchema = schema.object({ - name: schema.maybe(schema.string()), + name: schema.maybe(workspaceNameSchema), ...workspaceOptionalAttributesSchema, }); From 060aaee68178239f1927c898dd6352f614397b3c Mon Sep 17 00:00:00 2001 From: "opensearch-changeset-bot[bot]" <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> Date: Fri, 26 Jul 2024 07:47:19 +0000 Subject: [PATCH 2/4] Changeset file for PR #7512 created/updated --- changelogs/fragments/7512.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/7512.yml diff --git a/changelogs/fragments/7512.yml b/changelogs/fragments/7512.yml new file mode 100644 index 000000000000..f2a96125bc61 --- /dev/null +++ b/changelogs/fragments/7512.yml @@ -0,0 +1,2 @@ +fix: +- Add workspace blank check ([#7512](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7512)) \ No newline at end of file From 9a852f77db5868e62c8e631f8b616abe58ac5cba Mon Sep 17 00:00:00 2001 From: Hailong Cui Date: Mon, 29 Jul 2024 13:46:29 +0800 Subject: [PATCH 3/4] add integ test Signed-off-by: Hailong Cui --- .../server/integration_tests/routes.test.ts | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/plugins/workspace/server/integration_tests/routes.test.ts b/src/plugins/workspace/server/integration_tests/routes.test.ts index fdcbde636429..e3de40309e72 100644 --- a/src/plugins/workspace/server/integration_tests/routes.test.ts +++ b/src/plugins/workspace/server/integration_tests/routes.test.ts @@ -83,6 +83,29 @@ describe('workspace service api integration test', () => { expect(result.body.success).toEqual(true); expect(typeof result.body.result.id).toBe('string'); }); + it('create with empty/blank name', async () => { + let result = await osdTestServer.request + .post(root, `/api/workspaces`) + .send({ + attributes: { name: '' }, + }) + .expect(400); + + expect(result.body.message).toEqual( + "[request body.attributes.name]: can't be empty or blank." + ); + + result = await osdTestServer.request + .post(root, `/api/workspaces`) + .send({ + attributes: { name: ' ' }, + }) + .expect(400); + + expect(result.body.message).toEqual( + "[request body.attributes.name]: can't be empty or blank." + ); + }); it('create workspace failed when name duplicate', async () => { let result: any = await osdTestServer.request From 6188f35f52caf94c8981c7afd628d4a013246fb3 Mon Sep 17 00:00:00 2001 From: "opensearch-changeset-bot[bot]" <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> Date: Tue, 30 Jul 2024 05:27:04 +0000 Subject: [PATCH 4/4] Changeset file for PR #7512 created/updated --- changelogs/fragments/7512.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/7512.yml b/changelogs/fragments/7512.yml index f2a96125bc61..a248d7644293 100644 --- a/changelogs/fragments/7512.yml +++ b/changelogs/fragments/7512.yml @@ -1,2 +1,2 @@ fix: -- Add workspace blank check ([#7512](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7512)) \ No newline at end of file +- [Workspace]add workspace name blank/empty check ([#7512](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7512)) \ No newline at end of file