Skip to content

Commit 2533d04

Browse files
committed
Wizard: Validate partition name
This adds validation to name fields for volume group and logical volumes.
1 parent a0783fe commit 2533d04

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

src/Components/CreateImageWizard/steps/FileSystem/components/PartitionName.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React from 'react';
22

3-
import { TextInput } from '@patternfly/react-core';
4-
53
import { useAppDispatch } from '../../../../../store/hooks';
64
import { VolumeGroup } from '../../../../../store/imageBuilderApi';
75
import { changePartitionName } from '../../../../../store/wizardSlice';
6+
import { useFilesystemValidation } from '../../../utilities/useValidation';
7+
import { ValidatedInputAndTextArea } from '../../../ValidatedInput';
88
import { FscDiskPartitionBase, LogicalVolumeWithBase } from '../fscTypes';
99

1010
type PartitionNamePropTypes = {
@@ -17,12 +17,12 @@ const PartitionName = ({
1717
customization,
1818
}: PartitionNamePropTypes) => {
1919
const dispatch = useAppDispatch();
20+
const stepValidation = useFilesystemValidation();
2021

2122
return (
22-
<TextInput
23-
aria-label='Partition name input'
23+
<ValidatedInputAndTextArea
24+
ariaLabel='Partition name input'
2425
value={partition.name || ''}
25-
type='text'
2626
onChange={(event, name) => {
2727
dispatch(
2828
changePartitionName({
@@ -32,6 +32,8 @@ const PartitionName = ({
3232
}),
3333
);
3434
}}
35+
stepValidation={stepValidation}
36+
fieldName={`lvname-${partition.id}`}
3537
/>
3638
);
3739
};

src/Components/CreateImageWizard/steps/FileSystem/components/VolumeGroups.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
Flex,
1010
FlexItem,
1111
FormGroup,
12-
TextInput,
1312
} from '@patternfly/react-core';
1413
import { PlusCircleIcon, TimesIcon } from '@patternfly/react-icons';
1514
import { v4 as uuidv4 } from 'uuid';
@@ -25,6 +24,8 @@ import {
2524
changeDiskPartitionName,
2625
removeDiskPartition,
2726
} from '../../../../../store/wizardSlice';
27+
import { useFilesystemValidation } from '../../../utilities/useValidation';
28+
import { ValidatedInputAndTextArea } from '../../../ValidatedInput';
2829
import { FscDiskPartition, FscDiskPartitionBase } from '../fscTypes';
2930

3031
type VolumeGroupsType = {
@@ -33,6 +34,7 @@ type VolumeGroupsType = {
3334

3435
const VolumeGroups = ({ volumeGroups }: VolumeGroupsType) => {
3536
const dispatch = useAppDispatch();
37+
const stepValidation = useFilesystemValidation();
3638

3739
const handleAddLogicalVolume = (vgId: string) => {
3840
const id = uuidv4();
@@ -67,15 +69,15 @@ const VolumeGroups = ({ volumeGroups }: VolumeGroupsType) => {
6769
/>
6870
<CardBody>
6971
<FormGroup label='Volume group name'>
70-
<TextInput
71-
aria-label='Volume group name input'
72+
<ValidatedInputAndTextArea
73+
ariaLabel='Volume group name input'
7274
value={vg.name || ''}
73-
type='text'
7475
onChange={(event, name) =>
7576
dispatch(changeDiskPartitionName({ id: vg.id, name: name }))
7677
}
7778
placeholder='Add volume group name'
78-
className='pf-v6-u-w-25'
79+
stepValidation={stepValidation}
80+
fieldName={`name-${vg.id}`}
7981
/>
8082
</FormGroup>
8183
<FormGroup label='Minimum volume group size'>

src/Components/CreateImageWizard/utilities/useValidation.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
selectBlueprintDescription,
2323
selectBlueprintId,
2424
selectBlueprintName,
25+
selectDiskPartitions,
2526
selectFilesystemPartitions,
2627
selectFirewall,
2728
selectFirstBootScript,
@@ -59,6 +60,7 @@ import {
5960
isKernelNameValid,
6061
isMountpointMinSizeValid,
6162
isNtpServerValid,
63+
isPartitionNameValid,
6264
isPortValid,
6365
isServiceValid,
6466
isSnapshotValid,
@@ -302,6 +304,7 @@ export function useAAPValidation(): StepValidation {
302304
export function useFilesystemValidation(): StepValidation {
303305
const fscMode = useAppSelector(selectFscMode);
304306
const filesystemPartitions = useAppSelector(selectFilesystemPartitions);
307+
const diskPartitions = useAppSelector(selectDiskPartitions);
305308
let disabledNext = false;
306309

307310
const errors: { [key: string]: string } = {};
@@ -323,6 +326,27 @@ export function useFilesystemValidation(): StepValidation {
323326
disabledNext = true;
324327
}
325328
}
329+
330+
for (const partition of diskPartitions) {
331+
if (
332+
'name' in partition &&
333+
partition.name &&
334+
!isPartitionNameValid(partition.name)
335+
) {
336+
errors[`name-${partition.id}`] = 'Partition name is invalid';
337+
disabledNext = true;
338+
}
339+
340+
if (partition.type === 'lvm' && partition.logical_volumes.length > 0) {
341+
for (const lv of partition.logical_volumes) {
342+
if (lv.name && !isPartitionNameValid(lv.name)) {
343+
errors[`lvname-${lv.id}`] = 'Volume name is invalid';
344+
disabledNext = true;
345+
}
346+
}
347+
}
348+
}
349+
326350
return { errors, disabledNext };
327351
}
328352

src/Components/CreateImageWizard/validators.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ export const isMountpointMinSizeValid = (minSize: string) => {
4949
return /^\d+$/.test(minSize) && parseInt(minSize) > 0;
5050
};
5151

52+
export const isPartitionNameValid = (name: string) => {
53+
return /^[a-zA-Z0-9+_.-]+$/.test(name);
54+
};
55+
5256
export const isBlueprintNameValid = (blueprintName: string) =>
5357
blueprintName.length >= 2 &&
5458
blueprintName.length <= 100 &&

0 commit comments

Comments
 (0)