@cloudstrytech/validations 1.2.1 → 1.2.3

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.2.1",
3
+ "version": "1.2.3",
4
4
  "description": "JavaScript utility functions for validation",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -5,6 +5,7 @@ import { isValidEmail } from "../validators/emailValidator.js";
5
5
  import { isValidIndianMobile } from "../validators/mobileValidator.js";
6
6
  import { isValidDate } from "../validators/dateValidator.js";
7
7
  import { normalizeDate } from "../validators/normalizeDate.js";
8
+ import { normalizeExcelDate } from "../validators/normalizeExcelDate.js";
8
9
 
9
10
  /* Normalize CSV headers */
10
11
  function normalizeKey(key = "") {
@@ -14,42 +15,42 @@ function normalizeKey(key = "") {
14
15
  /* Mandatory fields mapped to REAL CSV headers */
15
16
  const MANDATORY_FIELDS = {
16
17
  "first name": {
17
- message: "First name is required",
18
+ message: "First name is required and cannot be empty",
18
19
  validate: (v) => !!v && String(v).trim() !== "",
19
20
  },
20
21
 
21
22
  "last name": {
22
- message: "Last name is required",
23
+ message: "Last name is required and cannot be empty",
23
24
  validate: (v) => !!v && String(v).trim() !== "",
24
25
  },
25
26
 
26
27
  email: {
27
- message: "Email is required or invalid",
28
+ message: "A valid email address is required",
28
29
  validate: (v) => !!v && isValidEmail(v),
29
30
  },
30
31
 
31
32
  "mobile number": {
32
- message: "Mobile number is required or invalid",
33
+ message: "A valid Indian mobile number is required",
33
34
  validate: (v) => !!v && isValidIndianMobile(v),
34
35
  },
35
36
 
36
37
  // "company": {
37
- // message: "Company is required",
38
+ // message: "Company name is required",
38
39
  // validate: (v) => !!v && String(v).trim() !== "",
39
40
  // },
40
41
 
41
42
  badge1: {
42
- message: "Badge ID is required or must be a 5-digit number",
43
+ message: "Badge ID is required and must be exactly 5 digits",
43
44
  validate: (v) => !!v && /^\d{5}$/.test(String(v).trim()),
44
45
  },
45
46
 
46
47
  "issuedate-b1": {
47
- message: "Issue date is required or invalid",
48
+ message: "Issue date is required and must be in DD/MM/YYYY format",
48
49
  validate: (v) => !!v && isValidDate(v),
49
50
  },
50
51
 
51
52
  // "location-b1": {
52
- // message: "Location is required",
53
+ // message: "Location is required and cannot be empty",
53
54
  // validate: (v) => !!v && String(v).trim() !== "",
54
55
  // },
55
56
  };
@@ -80,7 +81,7 @@ export async function validateCSV(csvInput) {
80
81
  ...row,
81
82
  "issuedate-b1":
82
83
  messages.length === 0
83
- ? normalizeDate(normalizedRow["issuedate-b1"])
84
+ ? normalizeDate(normalizeExcelDate(normalizedRow["issuedate-b1"]))
84
85
  : row["issuedate-b1"],
85
86
 
86
87
  __status: messages.length > 0 ? "ERROR" : "SUCCESS",
@@ -56,7 +56,7 @@ export function parseCSV(input) {
56
56
  /* 🔒 3️⃣ SAFE sheet_to_json (ADD HERE) */
57
57
  const json = XLSX.utils.sheet_to_json(worksheet, {
58
58
  defval: "",
59
- raw: false,
59
+ raw: true,
60
60
  });
61
61
 
62
62
  /* (Optional but recommended) Row limit */
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Convert Excel date OR string → DD/MM/YYYY
3
+ */
4
+ export function normalizeExcelDate(value) {
5
+ // Excel serial date (number)
6
+ if (typeof value === "number") {
7
+ const excelEpoch = new Date(Date.UTC(1899, 11, 30));
8
+ const date = new Date(excelEpoch.getTime() + value * 86400000);
9
+
10
+ const dd = String(date.getUTCDate()).padStart(2, "0");
11
+ const mm = String(date.getUTCMonth() + 1).padStart(2, "0");
12
+ const yyyy = date.getUTCFullYear();
13
+
14
+ return `${dd}/${mm}/${yyyy}`;
15
+ }
16
+
17
+ // Already string → trust validator
18
+ return value;
19
+ }