From b5f494256257991be8ebc7c6cebbf1bf13450284 Mon Sep 17 00:00:00 2001 From: Hailong Cui Date: Thu, 8 Aug 2024 10:36:23 +0800 Subject: [PATCH] [Worksapce]Add blank check for workspace name (#7512) * add workspace blank check Signed-off-by: Hailong Cui * Changeset file for PR #7512 created/updated * add integ test Signed-off-by: Hailong Cui * Changeset file for PR #7512 created/updated --------- Signed-off-by: Hailong Cui Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> --- changelogs/fragments/7512.yml | 2 ++ .../components/workspace_form/utils.test.ts | 12 ++++++++++ .../public/components/workspace_form/utils.ts | 2 +- .../server/integration_tests/routes.test.ts | 23 +++++++++++++++++++ src/plugins/workspace/server/routes/index.ts | 12 ++++++++-- 5 files changed, 48 insertions(+), 3 deletions(-) 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..a248d7644293 --- /dev/null +++ b/changelogs/fragments/7512.yml @@ -0,0 +1,2 @@ +fix: +- [Workspace]add workspace name blank/empty check ([#7512](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7512)) \ No newline at end of file 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/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 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, });