All files / @tech/validate index.js

70% Statements 35/50
49.12% Branches 28/57
47.06% Functions 8/17
90.32% Lines 28/31

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 81 82 83 84 85 86 87              1x   1x 2x 2x 2x 2x 2x 1x     1x     4x       2x   1x       1x       1x           1x       4x       5x         11x       1x 3x 3x 3x 1x   2x     3x       1x         1x       1x            
/* eslint-disable max-len */
/* eslint-disable no-restricted-syntax */
 
import moment from 'moment';
import { translate } from '@tech/intl';
import { isValidNumber } from 'libphonenumber-js';
 
const hasOwnProperty = Object.prototype.hasOwnProperty;
 
export const isEmpty = (obj) => {
  Iif (obj == null) return true;
  Iif (obj.length > 0) return false;
  Iif (obj.length === 0) return true;
  Iif (typeof obj !== 'object') return true;
  for (const key in obj) {
    Eif (hasOwnProperty.call(obj, key)) return false;
  }
 
  return true;
};
 
export const isError = code => code < 200 || (code >= 300 && code !== 304);
 
// Input validations
 
export const requiredValidator = value => (value ? undefined : translate('required'));
 
export const dayValidator = value => (value && !/^0[1-9]|1\d|2\d|3[01]$/.test(value)
  ? translate('invalid')
  : undefined);
 
export const monthValidator = value => (value && !/^0[1-9]|1[0-2]$/.test(value)
  ? translate('invalid')
  : undefined);
 
export const yearValidator = value => (value && !/^(19|20)\d{2}$/.test(value)
  ? translate('invalid')
  : undefined);
 
// Matches at least 1 lowercase 2 uppercase 1 digit between 8 and 16 => Uncomment the day we 'll stop using bullshit access policies...
// export const passwordValidator = value => (value && !/^(?=[^\s]*[a-z]{1})(?=[^\s]*[A-Z]{1})(?=[^\s]*\d{1})[^\s]{8,16}$/.test(value)
export const passwordValidator = value => (value && !/^[^\s]{8,16}$/.test(value)
  ? translate('invalid_password')
  : undefined);
 
export const clientIdValidator = value => (value && !/^\d{2,7}$/.test(value)
  ? translate('invalid_client_id')
  : undefined);
 
export const emailValidator = value => (value && !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(value)
  ? translate('invalid_email')
  : undefined);
 
 
export const dateValidator = value => (value && !/^(0[1-9]|1\d|2\d|3[01]).(0[1-9]|1[0-2]).(19|20)\d{2}$/.test(value)
  ? translate('invalid_date')
  : undefined);
 
export const legalAgeValidator = (value) => {
  const birthDate = moment(value, 'DD.MM.YYYY');
  const age = birthDate.diff(moment(), 'days');
  if (age > 0) {
    return translate('invalid_legal_age');
  }
  return Math.abs(age) > Math.abs(18 * 365.25) ? undefined : translate('invalid_legal_age');
};
 
export const phoneValidator = value => (!isValidNumber(value)
  ? translate('invalid_phone_number')
  : undefined);
 
export const atLeast = min => value => (value?.length >= min
  ? undefined
  : translate('invalid_length_atleast', min)
);
 
export const ibanValidator = value => (value && !/^(CH|LI)[a-zA-Z0-9]{2}[ ][a-zA-Z0-9]{4}[ ][a-zA-Z0-9]{4}[ ][a-zA-Z0-9]{4}[ ][a-zA-Z0-9]{4}[ ][a-zA-Z0-9]{1}$|^(CH|LI)[a-zA-Z0-9]{19}$/i.test(value)
  ? translate('invalid_iban')
  : undefined);
 
export const requiredAndCustomValidator = validator => (value) => {
  if (requiredValidator(value) === undefined) {
    return validator(value);
  }
  return requiredValidator(value);
};