ingeni-validation 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.
Files changed (2) hide show
  1. package/index.js +117 -0
  2. package/package.json +12 -0
package/index.js ADDED
@@ -0,0 +1,117 @@
1
+
2
+
3
+ async function validateMandate(req, res, requiredFields) {
4
+ try {
5
+ // const missingFields = requiredFields.filter(field => !req.body[field]);
6
+
7
+ const nameRegex = /^[A-Za-z .-]+$/;
8
+ const missingFields = requiredFields.filter(
9
+ field => req.body[field] === undefined ||
10
+ req.body[field] === null ||
11
+ req.body[field] === '' ||
12
+ req.body[field] === 'undefined' ||
13
+ (typeof req.body[field] === "string" && req.body[field].trim() === "")
14
+ );
15
+
16
+
17
+
18
+ if (missingFields.length > 0) {
19
+ /*
20
+ return res.status(400).json({
21
+ success: false,
22
+ message: `Missing required fields: ${missingFields.join(", ")}`
23
+ });
24
+ */
25
+ return {
26
+ success: false,
27
+ message: `Missing required fields: ${missingFields.join(", ")}`
28
+ };
29
+ }
30
+
31
+
32
+ // return true if everything is fine
33
+ return true;
34
+
35
+ } catch (error) {
36
+ console.error("Validation error:", error);
37
+ return {
38
+ success: false,
39
+ message: "Internal Server Error"
40
+ };
41
+ }
42
+ };
43
+
44
+ async function validateOptional(req, res, optionalFields) {
45
+ try {
46
+ // const missingFields = requiredFields.filter(field => !req.body[field]);
47
+
48
+
49
+ const optionMissingFields = optionalFields.filter(
50
+ opField => req.body[opField] === undefined
51
+ );
52
+
53
+ if (optionMissingFields.length > 0) {
54
+ return {
55
+ success: false,
56
+ message: `Missing optional fields: ${optionMissingFields.join(", ")}`
57
+ };
58
+ }
59
+
60
+
61
+ // return true if everything is fine
62
+ return true;
63
+
64
+ } catch (error) {
65
+ console.error("Validation error:", error);
66
+ return {
67
+ success: false,
68
+ message: "Internal Server Error"
69
+ };
70
+ }
71
+ };
72
+
73
+
74
+ async function isValidMobile(mobile) {
75
+
76
+ const mobileStr = String(mobile).trim();
77
+ return /^[0-9]{10}$/.test(mobileStr);
78
+ }
79
+
80
+
81
+ async function isValidEmail(email) {
82
+
83
+ const emailStr = String(email).trim();
84
+ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
85
+
86
+ return emailRegex.test(emailStr);
87
+ }
88
+
89
+
90
+ async function isValidOtp(otp) {
91
+ if (otp === undefined || otp === null) return false;
92
+
93
+ const otpStr = String(otp).trim();
94
+
95
+ if (otpStr.length !== 6) return false;
96
+
97
+ return /^[0-9]{6}$/.test(otpStr);
98
+ }
99
+
100
+
101
+
102
+ async function isValidPin(pinCode) {
103
+ if (pinCode === undefined || pinCode === null) return false;
104
+
105
+ // convert safely to string
106
+ const pinCodeStr = String(pinCode).trim();
107
+
108
+ // exactly 6 digits
109
+ if (pinCodeStr.length !== 6) return false;
110
+
111
+ return /^[0-9]{6}$/.test(pinCodeStr);
112
+ }
113
+
114
+
115
+
116
+
117
+ export {validateMandate,validateOptional, isValidMobile, isValidEmail, isValidOtp, isValidPin };
package/package.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "ingeni-validation",
3
+ "version": "1.0.0",
4
+ "description": "Ingeni Validation",
5
+ "license": "ISC",
6
+ "author": "",
7
+ "type": "commonjs",
8
+ "main": "index.js",
9
+ "scripts": {
10
+ "test": "Validations"
11
+ }
12
+ }