@cloudstrytech/validations 1.0.0

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 ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@cloudstrytech/validations",
3
+ "version": "1.0.0",
4
+ "description": "JavaScript utility functions for validation",
5
+ "type": "module",
6
+ "main": "src/index.js",
7
+ "keywords": [
8
+ "javascript",
9
+ "validation",
10
+ "regex",
11
+ "form-utils"
12
+ ],
13
+ "author": "Cloudstry",
14
+ "license": "MIT",
15
+ "dependencies": {
16
+ "papaparse": "^5.5.3"
17
+ }
18
+ }
@@ -0,0 +1,62 @@
1
+ import { parseCSV } from "./parseCSV.js";
2
+ import { isValidEmail } from "../validators/emailValidator.js";
3
+ import { isValidIndianMobile } from "../validators/mobileValidator.js";
4
+ import { isValidDate } from "../validators/dateValidator.js";
5
+
6
+ const MANDATORY_FIELDS = [
7
+ "Name",
8
+ "Email",
9
+ "Mobile",
10
+ "BadgeId",
11
+ "IssueDate",
12
+ "Location",
13
+ "Company"
14
+ ];
15
+
16
+ export async function validateCSV(csvInput) {
17
+ const rows = await parseCSV(csvInput);
18
+ const errors = [];
19
+
20
+ rows.forEach((row, index) => {
21
+ const rowNumber = index + 2;
22
+
23
+ MANDATORY_FIELDS.forEach((field) => {
24
+ const value = row[field];
25
+
26
+ if (!value || String(value).trim() === "") {
27
+ errors.push({
28
+ row: rowNumber,
29
+ column: field,
30
+ error: "Mandatory field is empty"
31
+ });
32
+ return;
33
+ }
34
+
35
+ if (field === "Email" && !isValidEmail(value)) {
36
+ errors.push({
37
+ row: rowNumber,
38
+ column: field,
39
+ error: "Invalid email format"
40
+ });
41
+ }
42
+
43
+ if (field === "Mobile" && !isValidIndianMobile(value)) {
44
+ errors.push({
45
+ row: rowNumber,
46
+ column: field,
47
+ error: "Invalid Indian mobile number"
48
+ });
49
+ }
50
+
51
+ if (field === "IssueDate" && !isValidDate(value)) {
52
+ errors.push({
53
+ row: rowNumber,
54
+ column: field,
55
+ error: "Invalid date format"
56
+ });
57
+ }
58
+ });
59
+ });
60
+
61
+ return errors;
62
+ }
@@ -0,0 +1,41 @@
1
+ import Papa from "papaparse";
2
+
3
+ /**
4
+ * Parse CSV from File or string
5
+ * @param {File | string} input
6
+ * @returns {Promise<Array<Object>>}
7
+ */
8
+ export function parseCSV(input) {
9
+ return new Promise((resolve, reject) => {
10
+ if (!input) {
11
+ reject(new Error("CSV input is required"));
12
+ return;
13
+ }
14
+
15
+ // Browser File upload
16
+ if (typeof File !== "undefined" && input instanceof File) {
17
+ Papa.parse(input, {
18
+ header: true,
19
+ skipEmptyLines: true,
20
+ complete: (result) => resolve(result.data),
21
+ error: reject
22
+ });
23
+ return;
24
+ }
25
+
26
+ // CSV string
27
+ if (typeof input === "string") {
28
+ Papa.parse(input, {
29
+ header: true,
30
+ skipEmptyLines: true,
31
+ complete: (result) => resolve(result.data),
32
+ error: reject
33
+ });
34
+ return;
35
+ }
36
+
37
+ reject(
38
+ new Error("Invalid CSV input. Provide a File or CSV string.")
39
+ );
40
+ });
41
+ }
package/src/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { isValidEmail } from "./validators/emailValidator.js";
2
+ export { isValidIndianMobile } from "./validators/mobileValidator.js";
3
+ export { isValidDate } from "./validators/dateValidator.js";
4
+ export { validateCSV } from "./csv/csvValidator.js";
@@ -0,0 +1,3 @@
1
+ export function isValidDate(value) {
2
+ return !isNaN(Date.parse(value));
3
+ }
@@ -0,0 +1,4 @@
1
+ export function isValidEmail(email) {
2
+ const regex = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
3
+ return regex.test(String(email).trim());
4
+ }
@@ -0,0 +1,4 @@
1
+ export function isValidIndianMobile(mobile) {
2
+ const regex = /^[6-9]\d{9}$/;
3
+ return regex.test(String(mobile).trim());
4
+ }