-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
157 lines (149 loc) · 7.68 KB
/
index.js
File metadata and controls
157 lines (149 loc) · 7.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const moment = require("moment");
async function validateInput(body, rules, setState, customMessages = {}) {
const errors = {};
for (const [key, value] of Object.entries(rules)) {
const input = body[key] ?? null;
const rulesArr = value.split("|");
for (const ruleStr of rulesArr) {
const [rule, param] = ruleStr.split(":");
if (errors[key]) continue;
if (rulesArr.includes("nullable") && input === null) {
continue;
}
// Helper function to get error message (custom or default)
const getErrorMessage = (ruleKey, defaultMsg) => {
const customKey = `${key}.${ruleKey}`;
return customMessages[customKey] || customMessages[ruleKey] || defaultMsg;
};
switch (rule) {
case "required":
if (input === null || input === "") {
errors[key] = getErrorMessage('required', `${key} is required`);
}
break;
case "not-empty":
if (input === "") {
errors[key] = getErrorMessage('not-empty', `${key} cannot be blank`);
}
break;
case "nullable":
continue;
case "min":
if (typeof input === "string" && input.length < parseInt(param)) {
errors[key] = getErrorMessage('min', `${key} must be at least ${param} characters long`);
}
break;
case "max":
if (typeof input === "string" && input.length > parseInt(param)) {
errors[key] = getErrorMessage('max', `${key} cannot be more than ${param} characters long`);
}
break;
case "digits":
if (typeof input === "string" && input.length !== parseInt(param)) {
errors[key] = getErrorMessage('digits', `${key} should be exactly ${param} characters long`);
}
break;
case "email":
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (input !== null && typeof input === "string" && !emailRegex.test(input)) {
errors[key] = getErrorMessage('email', `Invalid email address for ${key}`);
}
break;
case "password":
const passwordRegex = /^(?=.*[A-Za-z])(?=.*\d)(?=.*[!@#$%^&*(),.?":{}|<>])[A-Za-z\d!@#$%^&*(),.?":{}|<>]{8,}$/;
if (input !== null && typeof input === "string" && !passwordRegex.test(input)) {
errors[key] = getErrorMessage('password', `${key} must be at least 8 characters long, contain letters, numbers, and at least one special character`);
}
break;
case "confirm_password":
const confirm = body['confirm_password'];
if (confirm === null || confirm === "") {
errors[key] = getErrorMessage('confirm_password.missing', `${key} is missing confirm password field`);
} else if (confirm !== input) {
errors[key] = getErrorMessage('confirm_password.mismatch', `${key} is not matching confirm password field`);
}
break;
case "in":
const validValues = param.split(",");
if (typeof input === "string" && !validValues.includes(input)) {
errors[key] = getErrorMessage('in', `${key} must be one of the following values: ${validValues.join(", ")}`);
}
break;
case "date":
const dateFormat = param || "YYYY-MM-DD";
if (input !== null && !moment(input, dateFormat, true).isValid()) {
errors[key] = getErrorMessage('date', `${key} must be a valid date with format ${dateFormat}`);
}
break;
case "future":
if (input !== null && typeof input === "string" && moment(input).isValid()) {
if (!moment(input).isAfter(moment())) {
errors[key] = getErrorMessage('future', `${key} must be a future date`);
}
} else {
errors[key] = getErrorMessage('date.invalid', `${key} is not a valid date`);
}
break;
case "past":
if (input !== null && typeof input === "string" && moment(input).isValid()) {
if (!moment(input).isBefore(moment())) {
errors[key] = getErrorMessage('past', `${key} must be a past date`);
}
} else {
errors[key] = getErrorMessage('date.invalid', `${key} is not a valid date`);
}
break;
case "string":
if (input !== null && typeof input !== "string") {
errors[key] = getErrorMessage('string', `${key} must be a string`);
}
break;
case "integer":
if (input !== null && !Number.isInteger(input)) {
errors[key] = getErrorMessage('integer', `${key} must be an integer`);
}
break;
case "boolean":
if (input !== null && typeof input !== "boolean") {
errors[key] = getErrorMessage('boolean', `${key} must be a boolean`);
}
break;
case "url":
const urlRegex = /^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([/\w .-]*)*\/?$/;
if (input !== null && typeof input === "string" && !urlRegex.test(input)) {
errors[key] = getErrorMessage('url', `${key} must be a valid URL`);
}
break;
case "numeric":
if (input !== null && (isNaN(input) || typeof input === "boolean")) {
errors[key] = getErrorMessage('numeric', `${key} must be a numeric value`);
}
break;
case "min_value":
if (input !== null && !isNaN(input) && parseFloat(input) < parseFloat(param)) {
errors[key] = getErrorMessage('min_value', `${key} must be at least ${param}`);
}
break;
case "max_value":
if (input !== null && !isNaN(input) && parseFloat(input) > parseFloat(param)) {
errors[key] = getErrorMessage('max_value', `${key} must not be greater than ${param}`);
}
break;
case "required_if":
const [conditionField, conditionValue] = param.split(",");
if (
body[conditionField] === conditionValue &&
(input === null || input === "")
) {
errors[key] = getErrorMessage('required_if', `${key} is required when ${conditionField} is ${conditionValue}`);
}
break;
default:
break;
}
}
}
setState(errors);
return Object.keys(errors).length <= 0;
}
module.exports = validateInput;