All files / src/services validator.service.ts

93.65% Statements 59/63
86.84% Branches 33/38
100% Functions 8/8
92.98% Lines 53/57

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 88 89 90 91 92 93 94 95 96 97 98 99  1x 1x 1x   1x 2x   2x 2x 1x       1x 23x 23x 23x 1x   22x     22x 5x         1x 18x   18x 18x 1x   17x 3x       1x 6x   6x 5x     5x     5x 2x   3x     1x 12x 12x 9x 7x     7x     1x     1x 2x   2x 2x 1x       1x 6x   6x 1x   5x 1x   4x 2x   3x 2x            
import {Credentials, passwordConfirmPassword, userObjectTypes} from '../controllers/types';
import errorMessages from '../utils/errorMessages';
const {confirmPasswordMsg, enterPhoneNumber, fieldsMissingMessage, inValidImageType, mailInvalidContent, passwordInvalidContent, phoneInvalidContent, requestBodyIsInvalid, requiredFieldIsMissing} = errorMessages
const isImageURL = require('image-url-validator').default;
 
export const checkValidPhone = (phone: string) => {
  let methodName = "checkValidPhone"
 
  const phone_expression = /^[\s()+-]*([0-9][\s()+-]*){6,15}(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$/i // /^(\s|\-)([0|\+[0-9]{1,5})?([7-9][0-9]{6,15})$/
  if (!phone_expression.test(phone)) {
    throw {code: 422, message: phoneInvalidContent, methodName}
  }
}
 
export const checkValidEmail = (email: string) => {
  let methodName = "checkValidEmail"
  const email_expression = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if (email === "") {
    throw {code: 422, message: errorMessages.emptyEmail, methodName}
  }
  Iif (!email && email !== "") {
    throw {code: 422, message: requiredFieldIsMissing, methodName}
  }
  if (!email_expression.test(email)) {
    throw {code: 403, message: mailInvalidContent, methodName}
  }
 
}
 
export const checkValidPassword = (password: string) => {
  let methodName = "checkValidPassword"
 
  const password_expression = /(?=.*\d)(?=.*[A-Z])(?=.*[!@#$%^&*()+=-\?;,./{}|\":<>\[\]\\\' ~_]).{8,}/
  if (!password) {
    throw {code: 422, message: requiredFieldIsMissing, methodName}
  }
  if (!password_expression.test(password) || password === "") {
    throw {code: 422, message: passwordInvalidContent, methodName}
  }
}
 
export async function validatePasswordAndConfirmPassword(credentials: passwordConfirmPassword) {
  let methodName = "validatePasswordAndConfirmPassword"
 
  checkValidPassword(credentials.password)
  Iif (!credentials.password) {
    throw {code: 422, message: fieldsMissingMessage, methodName}
  }
  Iif (!credentials.confirmPassword) {
    throw {code: 422, message: confirmPasswordMsg, methodName}
  }
  if (credentials.password !== credentials.confirmPassword)
    throw {code: 422, message: confirmPasswordMsg, methodName}
    ;
  return true
}
 
export async function validateCredentials(credentials: Credentials) {
  let methodName = "validateCredentials"
  checkValidEmail(credentials.email)
  checkValidPassword(credentials.password)
  Iif (!credentials.password || !credentials.email) {
    throw {code: 422, message: fieldsMissingMessage, methodName}
  }
  return true
}
 
export const validateEmailExist = async () => {
  // console.log(UserRepository, "@@@@@@@")
}
export const validImageURL = async (url: string) => {
  let methodName = "validImageURL"
 
  let isValidImageURL = await isImageURL(url)
  if (!isValidImageURL && isValidImageURL !== "") {
    throw {code: 422, message: errorMessages.imageOnlyError, methodName}
  }
}
 
export const userFormValid = async (data: userObjectTypes) => {
  let methodName = "userFormValid"
 
  if (data.email === "") {
    throw {code: 422, message: errorMessages.emptyEmail, methodName}
  }
  if (data.email) {
    await checkValidEmail(data.email)
  }
  if (data.phone) {
    await checkValidPhone(data.phone)
  }
  if (data.image) {
    await validImageURL(data.image)
  }
 
}