-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Limit VM name length to max k8s label length
- Loading branch information
Showing
8 changed files
with
111 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/utils/components/VMNameValidationHelperText/VMNameValidationHelperText.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React, { FC } from 'react'; | ||
|
||
import FormGroupHelperText from '@kubevirt-utils/components/FormGroupHelperText/FormGroupHelperText'; | ||
import { | ||
getVMNameValidationMessage, | ||
validateVMName, | ||
} from '@kubevirt-utils/components/VMNameValidationHelperText/utils/utils'; | ||
|
||
type VMNameValidationHelperTextProps = { | ||
showDefaultHelperText?: boolean; | ||
vmName: string; | ||
}; | ||
|
||
const VMNameValidationHelperText: FC<VMNameValidationHelperTextProps> = ({ | ||
showDefaultHelperText = false, | ||
vmName, | ||
}) => { | ||
const vmNameValidated = validateVMName(vmName); | ||
const vmNameValidationMessage = getVMNameValidationMessage(vmName, showDefaultHelperText); | ||
|
||
return ( | ||
<FormGroupHelperText validated={vmNameValidated}>{vmNameValidationMessage}</FormGroupHelperText> | ||
); | ||
}; | ||
|
||
export default VMNameValidationHelperText; |
30 changes: 30 additions & 0 deletions
30
src/utils/components/VMNameValidationHelperText/utils/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { t } from '@kubevirt-utils/hooks/useKubevirtTranslation'; | ||
import { MAX_K8S_NAME_LENGTH } from '@kubevirt-utils/utils/constants'; | ||
import { isEmpty } from '@kubevirt-utils/utils/utils'; | ||
import { ValidatedOptions } from '@patternfly/react-core'; | ||
|
||
export const vmNameLengthExceedsMaxLength = (vmName: string) => | ||
vmName?.length > MAX_K8S_NAME_LENGTH; | ||
|
||
export const isValidVMName = (vmName: string): boolean => { | ||
const nameMissing = isEmpty(vmName); | ||
const nameTooLong = vmNameLengthExceedsMaxLength(vmName); | ||
|
||
return !nameMissing && !nameTooLong; | ||
}; | ||
|
||
export const validateVMName = (vmName: string): ValidatedOptions => | ||
isValidVMName(vmName) ? ValidatedOptions.default : ValidatedOptions.error; | ||
|
||
export const getVMNameValidationMessage = (vmName: string, showDefaultMessage: boolean): string => { | ||
const nameMissing = isEmpty(vmName); | ||
const nameTooLong = vmNameLengthExceedsMaxLength(vmName); | ||
|
||
if (nameMissing) return t('VirtualMachine name can not be empty.'); | ||
if (nameTooLong) | ||
return t('Maximum name length is {{ maxNameLength }} characters', { | ||
maxNameLength: MAX_K8S_NAME_LENGTH, | ||
}); | ||
|
||
return showDefaultMessage ? t('Please provide name to VirtualMachine.') : ''; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters