Skip to content

Commit

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

Signed-off-by: Hailong Cui <[email protected]>

* Changeset file for PR #7512 created/updated

* add integ test

Signed-off-by: Hailong Cui <[email protected]>

* Changeset file for PR #7512 created/updated

---------

Signed-off-by: Hailong Cui <[email protected]>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
(cherry picked from commit b5f4942)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent d7f3765 commit b275e0a
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.";

Check warning on line 52 in src/plugins/workspace/server/routes/index.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/workspace/server/routes/index.ts#L52

Added line #L52 was not covered by tests
}
},
});

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 b275e0a

Please sign in to comment.