import { AbstractControl, ValidationErrors, ValidatorFn } from '@angular/forms';

export const matchOtherValidator = (otherControlName: string): ValidatorFn => {
  return (control: AbstractControl): ValidationErrors | null => {
    const parent = control.parent;
    if (!parent) return null;

    const otherControl = parent.get(otherControlName);
    if (!otherControl) return null;

    if (control.value === otherControl.value) return null;

    return { matchOther: true };
  };
};
