Skip to content

Commit

Permalink
[Worksapce]Add blank check for workspace name (#7512) (#7663)
Browse files Browse the repository at this point in the history
* add workspace blank check



* Changeset file for PR #7512 created/updated

* add integ test



* Changeset file for PR #7512 created/updated

---------



(cherry picked from commit b5f4942)

Signed-off-by: Hailong Cui <ihailong@amazon.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 9, 2024
1 parent 3c90c37 commit bc91566
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/7512.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- [Workspace]add workspace name blank/empty check ([#7512](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7512))
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions src/plugins/workspace/server/integration_tests/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions src/plugins/workspace/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand Down

0 comments on commit bc91566

Please sign in to comment.