Skip to content

Commit 8a4e8d6

Browse files
committed
fix(hubble): address review comments on frontend validation
1 parent 51ab8b3 commit 8a4e8d6

5 files changed

Lines changed: 30 additions & 6 deletions

File tree

hugegraph-hubble/hubble-fe/src/i18n/resources/en-US/components/common.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"normal_name_rule": "Use Chinese characters, letters, numbers, or underscores only, up to 20 characters",
2828
"jdbc_rule": "Enter a valid JDBC URL, for example: jdbc:mysql://127.0.0.1:3306/db_name",
2929
"account_name_rule": "Account name must be within 16 characters and cannot start or end with an underscore",
30+
"favorite_name_rule": "Use Chinese characters, letters, numbers, or underscores only, up to 48 characters",
3031
"invalid_data_format": "Invalid data format"
3132
}
3233
},

hugegraph-hubble/hubble-fe/src/i18n/resources/zh-CN/components/common.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"normal_name_rule": "只能包含中文、字母、数字、_, 不能超过20个字符",
2828
"jdbc_rule": "请输入正确的jdbc url, 例如:jdbc:mysql://127.0.0.1:3306/db_name",
2929
"account_name_rule": "账号名不超过16个字符,且不能以下划线开始和结尾",
30+
"favorite_name_rule": "只能包含中文、字母、数字、_, 不能超过48个字符",
3031
"invalid_data_format": "非法的数据格式"
3132
}
3233
},

hugegraph-hubble/hubble-fe/src/modules/analysis/QueryBar/ContentCommon/index.test.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
/*
2+
*
23
* Licensed to the Apache Software Foundation (ASF) under one or more
3-
* contributor license agreements. See the NOTICE file distributed with
4-
* this work for additional information regarding copyright ownership.
5-
* The ASF licenses this file to You under the Apache License, Version 2.0.
4+
* contributor license agreements. See the NOTICE file distributed with this
5+
* work for additional information regarding copyright ownership. The ASF
6+
* licenses this file to You under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15+
* License for the specific language governing permissions and limitations
16+
* under the License.
617
*/
718

819
import {fireEvent, render, screen} from '@testing-library/react';
@@ -49,6 +60,7 @@ test('keeps favorite submission disabled until the name is backend-compatible',
4960
const input = screen.getByPlaceholderText('analysis.query.favorite_name_placeholder');
5061
const submit = screen.getAllByRole('button', {name: 'analysis.query.favorite'})
5162
.find(button => button.closest('.ant-popover'));
63+
expect(submit).toBeDefined();
5264

5365
fireEvent.change(input, {target: {value: 'query-name'}});
5466
expect(submit).toBeDisabled();

hugegraph-hubble/hubble-fe/src/utils/rules.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,15 @@ const isAccountName = msg => ({
158158
},
159159
});
160160

161-
const isValidFavoriteName = value => /^[A-Za-z0-9_]{1,48}$/.test(value);
161+
const isValidFavoriteName = value => typeof value === 'string' && /^[A-Za-z0-9_\u4e00-\u9fa5]{1,48}$/.test(value);
162162

163163
const isFavoriteName = msg => ({
164164
validator(_, value) {
165165
if (isValidFavoriteName(value)) {
166166
return Promise.resolve();
167167
}
168168
return Promise.reject(new Error(
169-
typeof msg === 'string' ? msg : 'Use letters, numbers, or underscores only'
169+
typeof msg === 'string' ? msg : validationMessage('favorite_name_rule')
170170
));
171171
},
172172
});

hugegraph-hubble/hubble-fe/src/utils/rules.test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ jest.mock('../i18n', () => ({
3838
'Enter a valid JDBC URL, for example: jdbc:mysql://127.0.0.1:3306/db_name',
3939
'common.validation.account_name_rule':
4040
'Account name must be within 16 characters and cannot start or end with an underscore',
41+
'common.validation.favorite_name_rule':
42+
'Use Chinese characters, letters, numbers, or underscores only, up to 48 characters',
4143
'common.validation.invalid_data_format': 'Invalid data format',
4244
},
4345
'zh-CN': {
@@ -53,6 +55,7 @@ jest.mock('../i18n', () => ({
5355
'common.validation.jdbc_rule':
5456
'请输入正确的jdbc url, 例如:jdbc:mysql://127.0.0.1:3306/db_name',
5557
'common.validation.account_name_rule': '账号名不超过16个字符,且不能以下划线开始和结尾',
58+
'common.validation.favorite_name_rule': '只能包含中文、字母、数字、_, 不能超过48个字符',
5659
'common.validation.invalid_data_format': '非法的数据格式',
5760
},
5861
};
@@ -146,10 +149,17 @@ describe('rules i18n defaults', () => {
146149
});
147150

148151
it('accepts only backend-compatible favorite names', async () => {
152+
await i18n.changeLanguage('en-US');
149153
await expect(rules.isFavoriteName().validator(null, 'query_2026'))
150154
.resolves.toBeUndefined();
155+
await expect(rules.isFavoriteName().validator(null, '我的查询_123'))
156+
.resolves.toBeUndefined();
151157
await expect(rules.isFavoriteName().validator(null, 'query-2026'))
152-
.rejects.toBeInstanceOf(Error);
158+
.rejects.toThrow('Use Chinese characters, letters, numbers, or underscores only, up to 48 characters');
159+
await expect(rules.isFavoriteName().validator(null, undefined))
160+
.rejects.toThrow('Use Chinese characters, letters, numbers, or underscores only, up to 48 characters');
161+
await expect(rules.isFavoriteName().validator(null, null))
162+
.rejects.toThrow('Use Chinese characters, letters, numbers, or underscores only, up to 48 characters');
153163
});
154164

155165
it('uses Chinese messages when the active language is Chinese', async () => {

0 commit comments

Comments
 (0)