1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-21 14:29:40 +02:00
portainer/app/react/kubernetes/applications/components/NameFormSection/nameValidation.ts
Ali 4e7d1c7088
Some checks are pending
ci / build_images (map[arch:amd64 platform:linux]) (push) Waiting to run
ci / build_images (map[arch:amd64 platform:windows version:1809]) (push) Waiting to run
ci / build_images (map[arch:amd64 platform:windows version:ltsc2022]) (push) Waiting to run
ci / build_images (map[arch:arm64 platform:linux]) (push) Waiting to run
ci / build_manifests (push) Blocked by required conditions
/ triage (push) Waiting to run
Lint / Run linters (push) Waiting to run
Test / test-client (push) Waiting to run
Test / test-server (map[arch:amd64 platform:linux]) (push) Waiting to run
Test / test-server (map[arch:amd64 platform:windows version:1809]) (push) Waiting to run
Test / test-server (map[arch:amd64 platform:windows version:ltsc2022]) (push) Waiting to run
Test / test-server (map[arch:arm64 platform:linux]) (push) Waiting to run
refactor(app): migrate remaining form sections [EE-6231] (#10938)
2024-01-11 15:13:28 +13:00

43 lines
1.4 KiB
TypeScript

import { SchemaOf, string as yupString } from 'yup';
type ValidationData = {
existingNames: string[];
isEdit: boolean;
originalName?: string;
};
export function appNameValidation(
validationData?: ValidationData
): SchemaOf<string> {
return yupString()
.required('This field is required.')
.test(
'is-unique',
'An application with the same name already exists inside the selected namespace.',
(appName) => {
if (!validationData || !appName) {
return true;
}
// if creating, check if the name is unique
if (!validationData.isEdit) {
return !validationData.existingNames.includes(appName);
}
// if editing, the original name will be in the list of existing names
// remove it before checking if the name is unique
const updatedExistingNames = validationData.existingNames.filter(
(name) => name !== validationData.originalName
);
return !updatedExistingNames.includes(appName);
}
)
.test(
'is-valid',
"This field must consist of lower case alphanumeric characters or '-', contain at most 63 characters, start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name', or 'abc-123').",
(appName) => {
if (!appName) {
return true;
}
return /^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$/g.test(appName);
}
);
}