Skip to content

Commit eaeaad0

Browse files
authored
Add Character Limit Validation in Name field in Catalog Settings (#2137)
* add source name char limit Signed-off-by: Taj010 <rsheen003@gmail.com> * address comments Signed-off-by: Taj010 <rsheen003@gmail.com> --------- Signed-off-by: Taj010 <rsheen003@gmail.com>
1 parent 38f24ca commit eaeaad0

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

clients/ui/frontend/src/app/pages/modelCatalogSettings/components/SourceDetailsSection.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ import { UpdateObjectAtPropAndValue } from 'mod-arch-shared';
1313
import FormFieldset from '~/app/pages/modelRegistry/screens/components/FormFieldset';
1414
import FormSection from '~/app/pages/modelRegistry/components/pf-overrides/FormSection';
1515
import { ManageSourceFormData } from '~/app/pages/modelCatalogSettings/useManageSourceData';
16-
import { validateSourceName } from '~/app/pages/modelCatalogSettings/utils/validation';
16+
import {
17+
validateSourceName,
18+
isSourceNameEmpty,
19+
} from '~/app/pages/modelCatalogSettings/utils/validation';
1720
import {
1821
FORM_LABELS,
1922
SOURCE_TYPE_LABELS,
2023
VALIDATION_MESSAGES,
24+
SOURCE_NAME_CHARACTER_LIMIT,
2125
} from '~/app/pages/modelCatalogSettings/constants';
2226
import { CatalogSourceType } from '~/app/modelCatalogTypes';
2327

@@ -34,6 +38,7 @@ const SourceDetailsSection: React.FC<SourceDetailsSectionProps> = ({
3438
}) => {
3539
const [isNameTouched, setIsNameTouched] = React.useState(false);
3640
const isNameValid = validateSourceName(formData.name);
41+
const hasNameError = isNameTouched && !isNameValid;
3742

3843
const nameInput = (
3944
<TextInput
@@ -46,19 +51,23 @@ const SourceDetailsSection: React.FC<SourceDetailsSectionProps> = ({
4651
value={formData.name}
4752
onChange={(_event, value) => setData('name', value)}
4853
onBlur={() => setIsNameTouched(true)}
49-
validated={isNameTouched && !isNameValid ? 'error' : 'default'}
54+
validated={hasNameError ? 'error' : 'default'}
5055
/>
5156
);
5257

5358
return (
5459
<FormSection>
5560
<FormGroup label={FORM_LABELS.NAME} isRequired fieldId="source-name">
5661
<FormFieldset component={nameInput} field="Name" data-testid="source-name-readonly" />
57-
{isNameTouched && !isNameValid && (
62+
{hasNameError && (
5863
<FormHelperText>
5964
<HelperText>
6065
<HelperTextItem variant="error" data-testid="source-name-error">
61-
{VALIDATION_MESSAGES.NAME_REQUIRED}
66+
{isSourceNameEmpty(formData.name)
67+
? VALIDATION_MESSAGES.NAME_REQUIRED
68+
: formData.name.length > SOURCE_NAME_CHARACTER_LIMIT
69+
? `Cannot exceed ${SOURCE_NAME_CHARACTER_LIMIT} characters`
70+
: null}
6271
</HelperTextItem>
6372
</HelperText>
6473
</FormHelperText>

clients/ui/frontend/src/app/pages/modelCatalogSettings/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export const SOURCE_TYPE_LABELS = {
2323
YAML: 'YAML file',
2424
} as const;
2525

26+
export const SOURCE_NAME_CHARACTER_LIMIT = 238;
27+
2628
export const VALIDATION_MESSAGES = {
2729
NAME_REQUIRED: 'Name is required',
2830
ORGANIZATION_REQUIRED: 'Organization is required',

clients/ui/frontend/src/app/pages/modelCatalogSettings/utils/validation.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { CatalogSourceType } from '~/app/modelCatalogTypes';
22
import { ManageSourceFormData } from '~/app/pages/modelCatalogSettings/useManageSourceData';
3+
import { SOURCE_NAME_CHARACTER_LIMIT } from '~/app/pages/modelCatalogSettings/constants';
34

45
const isNonEmptyString = (value: string): boolean => value.trim().length > 0;
56

6-
export const validateSourceName = (name: string): boolean => isNonEmptyString(name);
7+
export const validateSourceName = (name: string): boolean =>
8+
isNonEmptyString(name) && name.length <= SOURCE_NAME_CHARACTER_LIMIT;
9+
10+
export const isSourceNameEmpty = (name: string): boolean => !isNonEmptyString(name);
711

812
export const validateOrganization = (organization: string): boolean =>
913
isNonEmptyString(organization);

0 commit comments

Comments
 (0)