Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Worksapce]Add blank check for workspace name #7512

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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
Copy link
Member

@SuZhou-Joe SuZhou-Joe Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if the error message is clear enough for customers to understand what happens here, and from the description of the input, it seems space are valid input though the name can not be pure spaces.

And should we wrap it with i18n?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

field name will prepend to actual message, the whole error message will be

{
 "statusCode": 400,
 "error": "Bad Request",
 "message": "[request body.attributes.name]: can't be empty or blank."
}

And should we wrap it with i18n?

as the default validation message of schema.string()haven't been wrapped by i18n. i think we can keep it same

}
},
});

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
Loading