All files / ee/app/assets/javascripts/geo_settings validations.js

100% Statements 23/23
100% Branches 18/18
100% Functions 5/5
100% Lines 21/21

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      3x                 3x 12x   18x   16x     3x 11x 4x   7x 1x   6x 4x     2x     3x 18x 4x   14x 2x   12x 6x     6x    
import validateIpAddress from 'ee/validators/ip_address';
import { s__ } from '~/locale';
 
const i18n = {
  timeoutBlankError: s__("Geo|Connection timeout can't be blank"),
  timeoutNanError: s__('Geo|Connection timeout must be a number'),
  timeoutLengthError: s__('Geo|Connection timeout should be between 1-120'),
  allowedIpBlankError: s__("Geo|Allowed Geo IP can't be blank"),
  allowedIpLengthError: s__('Geo|Allowed Geo IP should be between 1 and 255 characters'),
  allowedIpFormatError: s__('Geo|Allowed Geo IP should contain valid IP addresses'),
};
 
const validateIP = (data) => {
  let addresses = data.replace(/\s/g, '').split(',');
 
  addresses = addresses.map((address) => validateIpAddress(address));
 
  return !addresses.some((a) => !a);
};
 
export const validateTimeout = (data) => {
  if (!data && data !== 0) {
    return i18n.timeoutBlankError;
  }
  if (data && Number.isNaN(Number(data))) {
    return i18n.timeoutNanError;
  }
  if (data < 1 || data > 120) {
    return i18n.timeoutLengthError;
  }
 
  return '';
};
 
export const validateAllowedIp = (data) => {
  if (!data) {
    return i18n.allowedIpBlankError;
  }
  if (data.length > 255) {
    return i18n.allowedIpLengthError;
  }
  if (!validateIP(data)) {
    return i18n.allowedIpFormatError;
  }
 
  return '';
};