File size: 4,915 Bytes
919f56a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

const {mailRegexPattern,linkedinProfileURLRegexPattern,phoneNumberRegexPattern} = require("../src/constants");


// Regular expression for validating LinkedIn URL
function IsLinkedInURL(link) {
  const regex = linkedinProfileURLRegexPattern;
  return regex.test(link);
}

// Regular expression for validating phone number
function IsValidPhoneNumber(phoneNumber) {
  const phoneNumberRegex = phoneNumberRegexPattern// Regex for +91 897888 format
  return phoneNumberRegex.test(phoneNumber);
}
function IsValidEmail(email) {
  const regexPattern = mailRegexPattern;
  return regexPattern.test(email);
}

// Validate the fields in the request body
function ValidateFields(req) {
  const {
    template_id,
    personal_information,
    job_title,
    career_objective,
    skills,
    education,
    experience,
    achievements
  } = req.body;

  // Check for missing required fields
  if (
    !template_id ||
    !personal_information ||
    !job_title ||
    !career_objective ||
    !skills ||
    !education ||
    !experience ||
    !achievements
  ) {
    return { error: 'Missing required fields' };
  }

  // Check the data type of template_id
  if (typeof template_id !== 'string') {
    return { error: 'template_id must be a string.' };
  }

  // Validate personal_information object
  if (
    typeof personal_information !== 'object' ||
    !personal_information.name ||
    !personal_information.last_name ||
    !personal_information.email_address ||
    !personal_information.phone_number ||
    !personal_information.linkedin_url ||
    !IsValidEmail(personal_information.email_address)||
    !IsLinkedInURL(personal_information.linkedin_url) ||
    !IsValidPhoneNumber(personal_information.phone_number)
  ) {
    return {
      error: 'personal_information must be an object with name, last_name, email_address, valid phone_number, and a valid LinkedIn URL and valid email'
    };
  }

  // Check if all personal_information fields are strings
  const personalInfoFields = ['name', 'last_name', 'email_address', 'phone_number', 'linkedin_url'];
  for (const field of personalInfoFields) {
    if (typeof personal_information[field] !== 'string') {
      return { error: `personal_information.${field} must be a string.` };
    }
  }

  // Validate job_title field
  if (typeof job_title !== 'string') {
    return { error: 'job_title must be a string.' };
  }

  // Validate career_objective field
  if (typeof career_objective !== 'string') {
    return { error: 'career_objective must be a string.' };
  }

  // Validate skills array
  if (!Array.isArray(skills) || !skills.every(skill => typeof skill === 'string')) {
    return { error: 'skills must be an array of strings.' };
  }

  // Validate education array
  if (!Array.isArray(education) || !education.every(edu => ValidateEducation(edu))) {
    return {
      error: 'education must be an array of objects with school_name, passing_year, and description fields of valid types.'
    };
  }

  // Validate experience array
  if (!Array.isArray(experience) || !experience.every(exp => ValidateExperience(exp))) {
    return {
      error: 'experience must be an array of objects with company_name, passing_year, and responsibilities fields of valid types.'
    };
  }

  // Validate achievements array
  if (!Array.isArray(achievements) || !achievements.every(ach => ValidateAchievement(ach))) {
    return {
      error: 'achievements must be an array of objects with field and awards fields of valid types.'
    };
  }

  return null; // No validation error
}

// Validate the education object
function ValidateEducation(edu) {
  if (
    typeof edu === 'object' &&
    edu.school_name &&
    edu.passing_year &&
    edu.description &&
    typeof edu.school_name === 'string' &&
    /^\d{4}-\d{4}$/.test(edu.passing_year) &&
    typeof edu.description === 'string'
  ) {
    return true;
  }
  return false;
}

// Validate the experience object
function ValidateExperience(exp) {
  if (
    typeof exp === 'object' &&
    exp.company_name &&
    exp.passing_year &&
    exp.responsibilities &&
    typeof exp.company_name === 'string' &&
    /^\d{4}-\d{4}$/.test(exp.passing_year) &&
    typeof exp.responsibilities === 'string'
  ) {
    return true;
  }
  return false;
}

// Validate the achievement object
function ValidateAchievement(ach) {
  if (
    typeof ach === 'object' &&
    ach.field &&
    ach.awards &&
    typeof ach.field === 'string' &&
    typeof ach.awards === 'string'
  ) {
    return true;
  }
  return false;
}

// Validate the headers in the request
function ValidateHeaders(req) {
  if (
    !req.headers['accept'] ||
    req.headers['accept'] !== 'application/pdf' ||
    !req.headers['content-type'] ||
    req.headers['content-type'] !== 'application/json'
  ) {
    return { error: 'Invalid headers' };
  }

  return null; // No validation error
}

module.exports = { ValidateFields, ValidateHeaders };