@cloudstrytech/validations 1.0.1 → 1.0.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.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "JavaScript utility functions for validation",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -1,64 +1,72 @@
1
- import { parseCSV } from "./parseCSV.js";
1
+ import { parseCSV } from "./parseCsv";
2
2
  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
- "Name",
8
- "Email",
9
- "Mobile",
10
- "BadgeId",
11
- "IssueDate",
12
- "Location",
13
- "Company"
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.forEach((row, index) => {
21
- const rowNumber = index + 2; // header is row 1
52
+ const resultRows = rows.map((row) => {
53
+ const messages = [];
22
54
 
23
- MANDATORY_FIELDS.forEach((field) => {
55
+ for (const field in MANDATORY_FIELDS) {
24
56
  const value = row[field];
57
+ const { validate, message } = MANDATORY_FIELDS[field];
25
58
 
26
- // Empty check
27
- if (!value || String(value).trim() === "") {
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
- if (field === "Mobile" && !isValidIndianMobile(value)) {
46
- errors.push({
47
- row: rowNumber,
48
- column: field,
49
- error: "Invalid Indian mobile number"
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 errors;
71
+ return { rows: resultRows };
64
72
  }