@cloudstrytech/validations 1.0.2 → 1.0.4
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 +1 -1
- package/src/csv/csvValidator.js +55 -47
package/package.json
CHANGED
package/src/csv/csvValidator.js
CHANGED
|
@@ -3,62 +3,70 @@ 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
|
-
const MANDATORY_FIELDS =
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
|
|
6
|
+
const MANDATORY_FIELDS = {
|
|
7
|
+
"First name": {
|
|
8
|
+
message: "First name is required",
|
|
9
|
+
validate: (v) => !!v
|
|
10
|
+
},
|
|
11
|
+
|
|
12
|
+
"Last name": {
|
|
13
|
+
message: "Last name is required",
|
|
14
|
+
validate: (v) => !!v
|
|
15
|
+
},
|
|
16
|
+
|
|
17
|
+
"email": {
|
|
18
|
+
message: "Email is required or invalid",
|
|
19
|
+
validate: (v) => !!v && isValidEmail(v)
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
"Mobile Number": {
|
|
23
|
+
message: "Mobile number is required or invalid",
|
|
24
|
+
validate: (v) => !!v && isValidIndianMobile(v)
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
"Company": {
|
|
28
|
+
message: "Company is required",
|
|
29
|
+
validate: (v) => !!v
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
"badge1": {
|
|
33
|
+
message: "Badge ID is required or must be a 5-digit number",
|
|
34
|
+
validate: (v) => !!v && /^\d{5}$/.test(String(v).trim())
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
"issuedate-b1": {
|
|
38
|
+
message: "Issue date is required or invalid",
|
|
39
|
+
validate: (v) => !!v && isValidDate(v)
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
"location-b1": {
|
|
43
|
+
message: "Location is required",
|
|
44
|
+
validate: (v) => !!v
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
15
48
|
|
|
16
49
|
export async function validateCSV(csvInput) {
|
|
17
50
|
const rows = await parseCSV(csvInput);
|
|
18
|
-
const errors = [];
|
|
19
51
|
|
|
20
|
-
rows.
|
|
21
|
-
const
|
|
52
|
+
const resultRows = rows.map((row) => {
|
|
53
|
+
const messages = [];
|
|
22
54
|
|
|
23
|
-
|
|
55
|
+
for (const field in MANDATORY_FIELDS) {
|
|
24
56
|
const value = row[field];
|
|
57
|
+
const { validate, message } = MANDATORY_FIELDS[field];
|
|
25
58
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
errors.push({
|
|
29
|
-
row: rowNumber,
|
|
30
|
-
column: field,
|
|
31
|
-
error: "Mandatory field is empty"
|
|
32
|
-
});
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// Field-specific validations
|
|
37
|
-
if (field === "Email" && !isValidEmail(value)) {
|
|
38
|
-
errors.push({
|
|
39
|
-
row: rowNumber,
|
|
40
|
-
column: field,
|
|
41
|
-
error: "Invalid email format"
|
|
42
|
-
});
|
|
59
|
+
if (!validate(value)) {
|
|
60
|
+
messages.push(message);
|
|
43
61
|
}
|
|
62
|
+
}
|
|
44
63
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (field === "IssueDate" && !isValidDate(value)) {
|
|
54
|
-
errors.push({
|
|
55
|
-
row: rowNumber,
|
|
56
|
-
column: field,
|
|
57
|
-
error: "Invalid date format"
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
});
|
|
64
|
+
return {
|
|
65
|
+
...row,
|
|
66
|
+
__status: messages.length > 0 ? "ERROR" : "OK",
|
|
67
|
+
__message: messages.length > 0 ? messages.join(", ") : "N/A"
|
|
68
|
+
};
|
|
61
69
|
});
|
|
62
70
|
|
|
63
|
-
return
|
|
71
|
+
return { rows: resultRows };
|
|
64
72
|
}
|