You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
portainer/app/react/edge/edge-stacks/CreateView/NameField.tsx

52 lines
1.2 KiB

import { FormikErrors } from 'formik';
import { SchemaOf, string } from 'yup';
import { STACK_NAME_VALIDATION_REGEX } from '@/react/constants';
import { FormControl } from '@@/form-components/FormControl';
import { Input } from '@@/form-components/Input';
import { EdgeStack } from '../types';
export function NameField({
onChange,
value,
errors,
}: {
onChange(value: string): void;
value: string;
errors?: FormikErrors<string>;
}) {
return (
<FormControl inputId="name-input" label="Name" errors={errors} required>
<Input
id="name-input"
onChange={(e) => onChange(e.target.value)}
value={value}
required
data-cy="edge-stack-create-name-input"
/>
</FormControl>
);
}
export function nameValidation(
stacks: Array<EdgeStack>,
isComposeStack: boolean | undefined
): SchemaOf<string> {
let schema = string()
.required('Name is required')
.test('unique', 'Name should be unique', (value) =>
stacks.every((s) => s.Name !== value)
);
if (isComposeStack) {
schema = schema.matches(
new RegExp(STACK_NAME_VALIDATION_REGEX),
"This field must consist of lower case alphanumeric characters, '_' or '-' (e.g. 'my-name', or 'abc-123')."
);
}
return schema;
}