import { FormikErrors } from 'formik'; import { SchemaOf, boolean, object } from 'yup'; import { file, withFileSize } from '@@/form-components/yup-file-validation'; import { FileUploadField } from '@@/form-components/FileUpload'; import { SwitchField } from '@@/form-components/SwitchField'; import { FormControl } from '@@/form-components/FormControl'; import { TLSConfig } from './types'; interface Props { values: TLSConfig; onChange: (value: Partial) => void; errors?: FormikErrors; } export function TLSFieldset({ values, onChange, errors }: Props) { return ( <>
handleChange({ tls: checked })} labelClass="col-sm-3 col-lg-2" />
{values.tls && ( <>
handleChange({ skipVerify: checked })} labelClass="col-sm-3 col-lg-2" />
{!values.skipVerify && ( <> handleChange({ caCertFile: file })} value={values.caCertFile} /> handleChange({ certFile: file })} value={values.certFile} /> handleChange({ keyFile: file })} value={values.keyFile} /> )} )} ); function handleChange(partialValue: Partial) { onChange(partialValue); } } const MAX_FILE_SIZE = 5_242_880; // 5MB function certValidation(optional?: boolean) { return withFileSize(file(), MAX_FILE_SIZE).when(['tls', 'skipVerify'], { is: (tls: boolean, skipVerify: boolean) => tls && !skipVerify && !optional, then: (schema) => schema.required('File is required'), }); } export function tlsConfigValidation({ optionalCert, }: { optionalCert?: boolean } = {}): SchemaOf { return object({ tls: boolean().default(false), skipVerify: boolean().default(false), caCertFile: certValidation(optionalCert), certFile: certValidation(optionalCert), keyFile: certValidation(optionalCert), }); }