@cloudstrytech/validations 1.0.6 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudstrytech/validations",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "JavaScript utility functions for validation",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -3,35 +3,42 @@ import { isValidEmail } from "../validators/emailValidator.js";
3
3
  import { isValidIndianMobile } from "../validators/mobileValidator.js";
4
4
  import { isValidDate } from "../validators/dateValidator.js";
5
5
 
6
+ /* Normalize CSV headers */
7
+ function normalizeKey(key = "") {
8
+ return key.trim().toLowerCase();
9
+ }
10
+
11
+ /* Mandatory fields mapped to REAL CSV headers */
6
12
  const MANDATORY_FIELDS = {
7
- "First name": {
13
+ "first name": {
8
14
  message: "First name is required",
9
- validate: (v) => !!v,
15
+ validate: (v) => !!v && String(v).trim() !== "",
10
16
  },
11
17
 
12
- "Last name": {
18
+ "last name": {
13
19
  message: "Last name is required",
14
- validate: (v) => !!v,
20
+ validate: (v) => !!v && String(v).trim() !== "",
15
21
  },
16
22
 
17
- email: {
23
+ "email": {
18
24
  message: "Email is required or invalid",
19
25
  validate: (v) => !!v && isValidEmail(v),
20
26
  },
21
27
 
22
- "Mobile Number": {
28
+ "mobile number": {
23
29
  message: "Mobile number is required or invalid",
24
30
  validate: (v) => !!v && isValidIndianMobile(v),
25
31
  },
26
32
 
27
- Company: {
33
+ "company": {
28
34
  message: "Company is required",
29
- validate: (v) => !!v,
35
+ validate: (v) => !!v && String(v).trim() !== "",
30
36
  },
31
37
 
32
- badge1: {
38
+ "badge1": {
33
39
  message: "Badge ID is required or must be a 5-digit number",
34
- validate: (v) => !!v && /^\d{5}$/.test(String(v).trim()),
40
+ validate: (v) =>
41
+ !!v && /^\d{5}$/.test(String(v).trim()),
35
42
  },
36
43
 
37
44
  "issuedate-b1": {
@@ -41,7 +48,7 @@ const MANDATORY_FIELDS = {
41
48
 
42
49
  "location-b1": {
43
50
  message: "Location is required",
44
- validate: (v) => !!v,
51
+ validate: (v) => !!v && String(v).trim() !== "",
45
52
  },
46
53
  };
47
54
 
@@ -51,9 +58,16 @@ export async function validateCSV(csvInput) {
51
58
  const resultRows = rows.map((row) => {
52
59
  const messages = [];
53
60
 
54
- for (const field in MANDATORY_FIELDS) {
55
- const value = row[field];
56
- const { validate, message } = MANDATORY_FIELDS[field];
61
+ // Normalize row keys once
62
+ const normalizedRow = {};
63
+ Object.keys(row).forEach((key) => {
64
+ normalizedRow[normalizeKey(key)] = row[key];
65
+ });
66
+
67
+ // Validate all mandatory fields
68
+ for (const fieldKey in MANDATORY_FIELDS) {
69
+ const { validate, message } = MANDATORY_FIELDS[fieldKey];
70
+ const value = normalizedRow[fieldKey];
57
71
 
58
72
  if (!validate(value)) {
59
73
  messages.push(message);
@@ -61,7 +75,7 @@ export async function validateCSV(csvInput) {
61
75
  }
62
76
 
63
77
  return {
64
- ...row,
78
+ ...row, // keep original CSV data for UI table
65
79
  __status: messages.length > 0 ? "ERROR" : "OK",
66
80
  __messages: messages.length > 0 ? messages : ["N/A"],
67
81
  };