All files / ip-input address-validators.ts

92% Statements 23/25
74.07% Branches 20/27
100% Functions 4/4
92% Lines 23/25

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 76 77 78 79 80              1x     1x     1x       1x                       1x     1x           1x       12x 327x   327x 84x 243x   243x 243x           327x             1x 131x 131x 131x           1x     1x 1x 1x    
/**
 * 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]))$/;
 
// Matches 1:2:3:4:5:6:7:8
const ipV6Regex = /^(([0-9A-Fa-f]{1,4}:){7})([0-9A-Fa-f]{1,4})$/;
 
// Matches range from 1:2:3:4:5:6:7:8/1 to 1:2:3:4:5:6:7:8/128
const ipV6CIDRRegex =
  /^(([0-9A-Fa-f]{1,4}:){7})([0-9A-Fa-f]{1,4})\/([1-9]|[1-9][0-9]|10[0-9]|11[0-9]|12[0-8])$/;
 
// Matches
// 1:2:3:4:5:6:7:8
// 1::             (1:0:0:0:0:0:0:0)
// 1:2:3:4:5:6:7:: (1:2:3:4:5:6:7:0)
// 1::8            (1:0:0:0:0:0:0:8)
// 1:2:3:4:5:6::8  (1:2:3:4:5:6:0:8)
// 1::7:8          (1:0:0:0:0:0:7:8)
// 1:2:3:4:5::7:8  (1:2:3:4:5:0:7:8)
// ...
const ipV6ZeroCompressionRegex =
  /^(([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,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,7}|:)$/;
 
const ipV6ZeroCompressionCIDRRegex =
  /^((([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,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,7}|:))\/([1-9]|[1-9][0-9]|10[0-9]|11[0-9]|12[0-8])$/;
 
/**
 * Validator factory for a IPV6 address.
 
 */
export const ipV6Validator = (options: {
  zeroCompression?: boolean;
  cidr?: boolean;
}): ValidatorFn => {
  return (control: AbstractControl): ValidationErrors | null => {
    const value = control.value;
    let valid: boolean | RegExpMatchArray | null | undefined;
    if (options.cidr && options.zeroCompression) {
      valid = value === '' || value?.match(ipV6ZeroCompressionCIDRRegex);
    } else Iif (options.cidr) {
      valid = value === '' || value?.match(ipV6CIDRRegex);
    } else if (options.zeroCompression) {
      valid =
        (value === '' || value?.toString().match(ipV6ZeroCompressionRegex)) &&
        value?.split('::').length <= 2;
    } else E{
      valid = value === '' || value?.toString().match(ipV6Regex);
    }
    return valid ? null : { ipv6Address: true };
  };
};
 
/**
 * 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 };
};