import { AbstractControl, ValidationErrors, ValidatorFn } from '@angular/forms';
import { globalRuleForm } from '../types/global-rule';
export function uniqueMainChildConditionValidator(): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
if (!Array.isArray(control.value)) {
return null;
}
const mainObjects: globalRuleForm[] = control.value;
for (let i = 0; i < mainObjects.length; i++) {
const mainObj = mainObjects[i];
// Ensure the current main object has child conditions to check
if (mainObj.ChildCondition && mainObj.ChildCondition.length > 0) {
const seenChildConditions = new Set<string>();
// Check each child condition for duplication
for (const [index, child] of mainObj.ChildCondition.entries()) {
const childConditionKey = `${child.Field}-${child.Condition}-${child.ConditionText}`;
// Check if the child condition matches the parent condition
const parentConditionKey = `${mainObj.Field}-${mainObj.Condition}-${mainObj.ConditionText}`;
if (childConditionKey === parentConditionKey) {
return { conditionMismatch: `${index} ${i} Child condition in main object ${i + 1} matches the parent condition.` };
}
// Check if the child condition has been seen before (duplicate check)
if (seenChildConditions.has(childConditionKey)) {
return { conditionMismatch: `${index} ${i} Child condition ${childConditionKey} is duplicated in main object ${i + 1}.` };
}
// Add the child condition to the set of seen conditions
seenChildConditions.add(childConditionKey);
}
}
for (let j = 0; j < mainObjects.length; j++) {
if (i !== j) {
const otherMainObj = mainObjects[j];
if (!otherMainObj.Logic &&
mainObj.Field === otherMainObj.Field &&
mainObj.Condition === otherMainObj.Condition &&
mainObj.ConditionText === otherMainObj.ConditionText
) {
return { conditionMismatch: `Main object ${i + 1} has matching properties with main object ${j + 1}.` };
}
}
}
}
return null;
};
}
can you make this validator more efficient the performance is slow