All files / ip-input address-validators.ts

96.96% Statements 32/33
86.95% Branches 20/23
100% Functions 6/6
96.87% Lines 31/32

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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75              1x     1x     1x       1x       33x 381x 381x 56x   325x 325x 325x 96x 84x     229x 3x     238x     238x 151x     87x             1x 131x 131x 131x           1x     1x 1x 1x     1x 17x 17x     238x  
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { AbstractControl, ValidationErrors, ValidatorFn } from '@angular/forms';
 
const ipv4Regex =
  /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
 
const ipv4CIDRRegex =
  /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\/([1-9]|1[0-9]|2[0-9]|3[0-2]))$/;
 
const ipV6Regex =
  /^((?:[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}|(?:[0-9A-Fa-f]{1,4}:){1,7}:|:(?::[0-9A-Fa-f]{1,4}){1,7}|(?:[0-9A-Fa-f]{1,4}:){1,6}:[0-9A-Fa-f]{1,4}|(?:[0-9A-Fa-f]{1,4}:){1,5}(?::[0-9A-Fa-f]{1,4}){1,2}|(?:[0-9A-Fa-f]{1,4}:){1,4}(?::[0-9A-Fa-f]{1,4}){1,3}|(?:[0-9A-Fa-f]{1,4}:){1,3}(?::[0-9A-Fa-f]{1,4}){1,4}|(?:[0-9A-Fa-f]{1,4}:){1,2}(?::[0-9A-Fa-f]{1,4}){1,5}|[0-9A-Fa-f]{1,4}:(?:(?::[0-9A-Fa-f]{1,4}){1,6})|:(?:(?::[0-9A-Fa-f]{1,4}){1,6}))$/;
/**
 * Validator factory for a IPV6 address.
 */
export const ipV6Validator = (options: {
  zeroCompression?: boolean;
  cidr?: boolean;
}): ValidatorFn => {
  return (control: AbstractControl): ValidationErrors | null => {
    const value = control.value;
    if (!value) {
      return null;
    }
    const parts = value.split('/');
    const error = { ipv6Address: true };
    if (options.cidr) {
      if (parts.length < 2 || !validateSubnet(parts[1])) {
        return error;
      }
    } else {
      if (parts.length > 1) {
        return error;
      }
    }
    Iif (parts[0].split('::').length > (options.zeroCompression ? 2 : 1)) {
      return error;
    }
    if (!matchIpV6(parts[0])) {
      return error;
    }
 
    return null;
  };
};
 
/**
 * Validates a IPV4 address.
 */
export const ipV4Validator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
  const v = control.value;
  const valid = v === '' || v?.toString().match(ipv4Regex);
  return valid ? null : { ipv4Address: true };
};
 
/**
 * Validates a IPV4 address including CIDR.
 */
export const ipV4CIDRValidator: ValidatorFn = (
  control: AbstractControl
): ValidationErrors | null => {
  const v = control.value;
  const valid = v === '' || v?.toString().match(ipv4CIDRRegex);
  return valid ? null : { ipv4Address: true };
};
 
const validateSubnet = (cidr: string): boolean => {
  const subnet = parseInt(cidr, 10);
  return subnet > 0 && subnet <= 128;
};
 
const matchIpV6 = (ip: string): boolean => !!ip.match(ipV6Regex) && !!URL.parse(`http://[${ip}]`);