fix(edge/updates): validate amount of environments [EE-5053] (#9014)

pull/9115/head
Chaim Lev-Ari 2023-06-22 21:13:57 +07:00 committed by GitHub
parent 4c8af378af
commit ea2f752a4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import { useRouter } from '@uirouter/react';
import { notifySuccess } from '@/portainer/services/notifications';
import { withLimitToBE } from '@/react/hooks/useLimitToBE';
import { useEdgeGroups } from '@/react/edge/edge-groups/queries/useEdgeGroups';
import { PageHeader } from '@@/PageHeader';
import { Widget } from '@@/Widget';
@ -36,7 +37,7 @@ function CreateView() {
}),
[]
);
const edgeGroupsQuery = useEdgeGroups();
const schedulesQuery = useList();
const createMutation = useCreateMutation();
@ -75,7 +76,9 @@ function CreateView() {
initialValues={initialValues}
onSubmit={handleSubmit}
validateOnMount
validationSchema={() => validation(schedules)}
validationSchema={() =>
validation(schedules, edgeGroupsQuery.data)
}
>
{({ isValid, setFieldValue, values, handleBlur, errors }) => (
<FormikForm className="form-horizontal">

View File

@ -5,6 +5,8 @@ import { object, SchemaOf } from 'yup';
import { notifySuccess } from '@/portainer/services/notifications';
import { withLimitToBE } from '@/react/hooks/useLimitToBE';
import { useEdgeGroups } from '@/react/edge/edge-groups/queries/useEdgeGroups';
import { EdgeGroup } from '@/react/edge/edge-groups/types';
import { PageHeader } from '@@/PageHeader';
import { Widget } from '@@/Widget';
@ -32,6 +34,7 @@ function ItemView() {
} = useCurrentStateAndParams();
const id = parseInt(idParam, 10);
const edgeGroupsQuery = useEdgeGroups();
if (!idParam || Number.isNaN(id)) {
throw new Error('id is a required path param');
@ -113,7 +116,12 @@ function ItemView() {
}}
validateOnMount
validationSchema={() =>
updateValidation(item.id, schedules, isScheduleActive)
updateValidation(
item.id,
schedules,
edgeGroupsQuery.data,
isScheduleActive
)
}
>
{({ isValid, setFieldValue, values, handleBlur, errors }) => (
@ -171,9 +179,10 @@ function ItemView() {
function updateValidation(
itemId: EdgeUpdateSchedule['id'],
schedules: EdgeUpdateSchedule[],
edgeGroups: Array<EdgeGroup> | undefined,
isScheduleActive: boolean
): SchemaOf<{ name: string } | FormValues> {
return !isScheduleActive
? validation(schedules, itemId)
? validation(schedules, edgeGroups, itemId)
: object({ name: nameValidation(schedules, itemId) });
}

View File

@ -1,6 +1,7 @@
import { array, object, SchemaOf, string } from 'yup';
import { array, object, SchemaOf, string, number } from 'yup';
import { parseIsoDate } from '@/portainer/filters/filters';
import { EdgeGroup } from '@/react/edge/edge-groups/types';
import { EdgeUpdateSchedule, ScheduleType } from '../types';
@ -10,10 +11,24 @@ import { FormValues } from './types';
export function validation(
schedules: EdgeUpdateSchedule[],
edgeGroups: Array<EdgeGroup> | undefined,
currentId?: EdgeUpdateSchedule['id']
): SchemaOf<FormValues> {
return object({
groupIds: array().min(1, 'At least one group is required'),
groupIds: array()
.of(number().default(0))
.min(1, 'At least one group is required')
.test(
'At least one group must have endpoints',
(groupIds) =>
!!(
groupIds &&
edgeGroups &&
groupIds?.flatMap(
(id) => edgeGroups?.find((group) => group.Id === id)?.Endpoints
).length > 0
)
),
name: nameValidation(schedules, currentId),
type: typeValidation(),
scheduledTime: string()
@ -28,5 +43,6 @@ export function validation(
// rollback
otherwise: (schema) => schema.required('No rollback options available'),
}),
registryId: number().default(0),
});
}