All files / app/identity/validators/confirm-match confirm-match.validator.ts

100% Statements 12/12
100% Branches 5/5
100% Functions 2/2
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41        1x                               1x   69x 352x 1x     351x 351x   351x 1x   350x 1x       349x    
import { FormGroup } from '@angular/forms';
import type { AbstractControl, ValidationErrors, ValidatorFn } from '@angular/forms';
 
/** ValidationError property name for `confirmMatch` Validator. */
export const ERROR_NAME = 'confirmmatch';
 
/**
 * Requires that two controls have the same value to validate the FormGroup.
 *
 * Usage:
 * ```
 *   new FormGroup(
 *    {
 *      password1: new FormControl(null, [ Validators.required, Validators.minLength(num), Validators.maxLength(num) ]),
 *      password2: new FormControl(null, [ Validators.required, Validators.minLength(num), Validators.maxLength(num) ]),
 *    },
 *    { validators: confirmMatch('password1', 'password2') },
 *  );
 * ```
 */
export const confirmMatch = (password1: string, password2: string): ValidatorFn =>
  // control: AbstractControl is required by the type ValidatorFn, but the actual expected type is FormGroup.
  (formGrp: AbstractControl): ValidationErrors | null => {
    if (!(formGrp instanceof FormGroup)) {
      throw new TypeError('Control must be an instance of FormGroup.');
    }
 
    const control1 = formGrp.get(password1);
    const control2 = formGrp.get(password2);
 
    if (!control1) {
      throw new Error(`Cannot find FormControl named '${password1}'.`);
    }
    if (!control2) {
      throw new Error(`Cannot find FormControl named '${password2}'.`);
    }
 
    // eslint-disable-next-line unicorn/no-null -- ValidatorFn returns null
    return control1.value === control2.value ? null : { [ERROR_NAME]: true };
  };