All files / src/ruleHandlers generalRules.js

100% Statements 30/30
100% Branches 4/4
100% Functions 8/8
100% Lines 23/23
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        9x         9x         9x         9x         9x         9x         9x         9x   8x   8x   3x 3x 3x   2x 2x 2x   2x 2x 2x   1x 1x 1x     8x          
// @ts-check
 
import is from 'is_js';
 
export const inArray = (name, value, schema) => ({
  isValid: is.inArray(value, schema.inArray),
  errorText: `Value of ${name} should be within [${schema.inArray.toString()}].`
});
 
export const matchRegex = (name, value, schema) => ({
  isValid: schema.matchRegex.test(value),
  errorText: `Value of ${name} is not valid.`
});
 
export const isEmail = (name, value) => ({
  isValid: is.email(value),
  errorText: `${name} should be email.`
});
 
export const isUrl = (name, value) => ({
  isValid: is.url(value),
  errorText: `${name} should be a URL.`
});
 
export const isCreditCard = (name, value) => ({
  isValid: is.creditCard(value * 1),
  errorText: `${name} should be a credit card number.`
});
 
export const isHexColor = (name, value) => ({
  isValid: is.hexColor(value),
  errorText: `${name} should be a hex color.`
});
 
export const notEmpty = (name, value) => ({
  isValid: is.not.empty(value),
  errorText: `${name} should not be empty.`
});
 
export const isIP = (name, value, schema) => {
  let isValid;
  let ipString = '';
 
  switch (schema.isIP) {
    case 'v4':
      isValid = is.ipv4(value);
      ipString = 'IPv4';
      break;
    case 'v6':
      isValid = is.ipv6(value);
      ipString = 'IPv6';
      break;
    case 'all':
      isValid = is.ip(value);
      ipString = 'IP';
      break;
    default:
      isValid = is.ip(value);
      ipString = 'IP';
      break;
  }
 
  return {
    isValid,
    errorText: `${name} should be ${ipString} address.`
  };
};