Skip to content

Commit

Permalink
add validations for edit-deployment form
Browse files Browse the repository at this point in the history
  • Loading branch information
divyanshiGupta committed May 4, 2021
1 parent 117838b commit 05ade62
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ const TextColumnField: React.FC<TextColumnFieldProps> = (props) => {
label={label}
validated={isValid ? ValidatedOptions.default : ValidatedOptions.error}
isRequired={required}
helperText={helpText}
>
<div className="pf-c-form__helper-text">{helpText}</div>
{dndEnabled ? (
<DragAndDrop>
{rowValues.map((v, idx) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import * as yup from 'yup';
import { TFunction } from 'i18next';
import {
DeploymentStrategy,
EditDeploymentData,
EditDeploymentFormData,
LifecycleHookFormData,
} from './edit-deployment-types';
import { EditorType } from '@console/shared/src/components/synced-editor/editor-toggle';
import { DeploymentStrategyType, LifecycleAction } from '../deployment-strategy/utils/types';

export const lchValidationSchema = (lch: LifecycleHookFormData, t: TFunction) =>
yup.object().shape({
action: yup.string().required(t('devconsole~Required')),
lch: yup.object().shape({
failurePolicy: yup.string().required(t('devconsole~Required')),
...(lch.action === LifecycleAction.execNewPod
? {
execNewPod: yup.object().shape({
containerName: yup.string().required(t('devconsole~Required')),
command: yup
.array()
.of(yup.string().required(t('devconsole~Required')))
.required(t('devconsole~Required')),
volumes: yup.string(),
}),
}
: {}),
}),
});

export const lchImageStreamDataSchema = (action: string, t: TFunction) => {
return action === LifecycleAction.tagImages
? yup.object().shape({
containerName: yup.string().required(t('devconsole~Required')),
imageStream: yup.object({
namespace: yup.string().required(t('devconsole~Required')),
image: yup.string().required(t('devconsole~Required')),
tag: yup.string().required(t('devconsole~Required')),
}),
})
: null;
};

export const deploymentStrategySchema = (strategy: DeploymentStrategy, t: TFunction) => {
switch (strategy.type) {
case DeploymentStrategyType.recreateParams: {
const { pre, mid, post } = strategy.recreateParams;
return yup.object().shape({
type: yup.string(),
recreateParams: yup.object().shape({
timeoutSeconds: yup.number(),
...(pre?.isAddingLch ? { pre: lchValidationSchema(pre, t) } : {}),
...(mid?.isAddingLch ? { mid: lchValidationSchema(mid, t) } : {}),
...(post?.isAddingLch ? { post: lchValidationSchema(post, t) } : {}),
}),
imageStreamData: yup.object().shape({
...(pre?.isAddingLch ? { pre: lchImageStreamDataSchema(pre.action, t) } : {}),
...(mid?.isAddingLch ? { mid: lchImageStreamDataSchema(mid.action, t) } : {}),
...(post?.isAddingLch ? { post: lchImageStreamDataSchema(post.action, t) } : {}),
}),
});
}
case DeploymentStrategyType.customParams: {
return yup.object().shape({
type: yup.string(),
customParans: yup.object().shape({
command: yup.array().of(yup.string()),
image: yup.string(),
}),
});
}
case DeploymentStrategyType.rollingParams: {
const { pre, post } = strategy.rollingParams;
return yup.object().shape({
type: yup.string(),
rollingParams: yup.object().shape({
timeoutSeconds: yup.number(),
...(pre?.isAddingLch ? { pre: lchValidationSchema(pre, t) } : {}),
...(post?.isAddingLch ? { post: lchValidationSchema(post, t) } : {}),
updatePeriodSeconds: yup.number(),
intervalSeconds: yup.number(),
maxSurge: yup.string(),
maxUnavailable: yup.string(),
}),
imageStreamData: yup.object().shape({
...(pre?.isAddingLch ? { pre: lchImageStreamDataSchema(pre.action, t) } : {}),
...(post?.isAddingLch ? { post: lchImageStreamDataSchema(post.action, t) } : {}),
}),
});
}
case DeploymentStrategyType.rollingUpdate: {
return yup.object().shape({
type: yup.string(),
rollingUpdate: yup.object().shape({
maxSurge: yup.string(),
maxUnavailable: yup.string(),
}),
});
}
default:
return null;
}
};

export const editDeploymentFormSchema = (formValues: EditDeploymentFormData, t: TFunction) =>
yup.object({
deploymentStrategy: deploymentStrategySchema(formValues.deploymentStrategy, t),
fromImageStreamTag: yup.boolean(),
...(!formValues.fromImageStreamTag
? { imageName: yup.string().required(t('devconsole~Required')) }
: {}),
imageStream: yup.object().when('fromImageStreamTag', {
is: true,
then: yup.object({
namespace: yup.string().required(t('devconsole~Required')),
image: yup.string().required(t('devconsole~Required')),
tag: yup.string().required(t('devconsole~Required')),
}),
}),
});

export const validationSchema = (t: TFunction) =>
yup.mixed().test({
test(formValues: EditDeploymentData) {
const formYamlDefinition = yup.object({
editorType: yup.string().oneOf(Object.values(EditorType)),
yamlData: yup.string(),
formData: yup.mixed().when('editorType', {
is: EditorType.Form,
then: editDeploymentFormSchema(formValues.formData, t),
}),
});

return formYamlDefinition.validate(formValues, { abortEarly: false });
},
});

0 comments on commit 05ade62

Please sign in to comment.