import { SchemaOf, array, boolean, mixed, object, string } from 'yup'; import { buildUniquenessTest } from '@@/form-components/validate-unique'; import { ConfigurationFormValues } from './types'; export function configurationsValidationSchema( validationData?: ConfigurationFormValues[] ): SchemaOf { return array( object({ overriden: boolean().required(), // skip validation for selectedConfiguration because it comes directly from a select dropdown // eslint-disable-next-line @typescript-eslint/no-explicit-any selectedConfiguration: object({} as any).required(), overridenKeys: array( object({ key: string().required(), path: string().when('type', { is: 'FILESYSTEM', then: string() .test( 'No duplicates globally', 'This path is already used.', (path?: string) => { const allPaths = validationData ?.flatMap((configmap) => configmap.overridenKeys) .map((k) => k.path); if (!allPaths) return true; return ( allPaths.filter((p) => p === path && p !== '').length <= 1 ); } ) .required('Path is required.'), }), type: mixed().oneOf(['NONE', 'ENVIRONMENT', 'FILESYSTEM']), }) ) .test( 'No duplicates', 'This path is already used.', buildUniquenessTest(() => 'This path is already used.', 'path') ) .required(), }) ); }