Repository navigation
Expand file tree
/
Copy pathreal.js
More file actions
218 lines (202 loc) · 5.95 KB
/
Copy pathreal.js
File metadata and controls
218 lines (202 loc) · 5.95 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { ValidationUtils } from "./utils/validation-utils.js";
import { OPERATORS } from "./constants/validation-types.js";
import { ValidationController } from "./index.js";
// import { console } from "./utils/console.js";
// console.enable();
// Register the IS_MAGIC operator
ValidationUtils.addCustomOperator("IS_MAGIC", (value, comparisonValue) => {
// The value must exactly match 'magic'
return value === "magic";
});
// Register the MATCH_CUSTOM_PATTERN operator
ValidationUtils.addCustomOperator("MATCH_CUSTOM_PATTERN", (value, pattern) => {
// The value must contain the specified pattern
if (typeof value !== "string" || typeof pattern !== "string") {
throw new Error(
"Both the value and pattern must be strings for MATCH_CUSTOM_PATTERN."
);
}
return value.includes(pattern);
});
// Register a custom type for "magicString"
ValidationUtils.addCustomType("magicString", (value) => {
return typeof value === "string" && value.includes("magic");
});
// Register a custom type for "positiveInteger"
ValidationUtils.addCustomType("positiveInteger", (value) => {
return typeof value === "number" && Number.isInteger(value) && value > 0;
});
const formData = [
{ fieldName: "name", value: "Jo" }, // Too short
{ fieldName: "age", value: "20" }, // Valid, but as a string
{ fieldName: "email", value: "invalid-email" }, // Invalid format
{ fieldName: "phone", value: "1234567890" }, // Incorrect format
{ fieldName: "experience", value: 3 }, // Valid
{ fieldName: "references", value: "" }, // Missing but required
{ fieldName: "preferredJobRole", value: "" }, // Missing but required
{ fieldName: "coverLetter", value: "" }, // Missing, not required in this case
{ fieldName: "magicField", value: "magic" },
{ fieldName: "customPatternField", value: "this is magic!" },
{ fieldName: "validMagicField", value: "this is magic!" }, // Pass
{ fieldName: "positiveField", value: 42 }, // Pass
{ fieldName: "invalidMagicField", value: "nothing special" }, // Fail
{ fieldName: "negativeField", value: -10 }, // Fail
];
const rules = [
// Name validation
{
type: "REQUIRED",
fieldId: "name",
errorMessage: "Name is required.",
},
{
type: "LENGTH_CHECK",
fieldId: "name",
minLength: 3,
maxLength: 50,
errorMessage: "Name must be between 3 and 50 characters.",
},
// Age validation
{
type: "REQUIRED",
fieldId: "age",
errorMessage: "Age is required.",
},
{
type: "TYPE_CHECK",
fieldId: "age",
expectedType: "number",
errorMessage: "Age must be a number.",
},
{
type: "COMPARISON",
fieldId: "age",
operator: "GREATER_THAN_OR_EQUAL",
value: 18,
errorMessage: "Age must be at least 18.",
},
// Email validation
{
type: "REQUIRED",
fieldId: "email",
errorMessage: "Email is required.",
},
{
type: "REGEX",
fieldId: "email",
value: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
errorMessage: "Invalid email format.",
},
// Phone validation
{
type: "REQUIRED",
fieldId: "phone",
errorMessage: "Phone number is required.",
},
{
type: "REGEX",
fieldId: "phone",
value: /^[0-9]{3}-[0-9]{4}-[0-9]{4}$/,
errorMessage: "Phone number must follow the format xxx-xxxx-xxxx.",
},
// Experience validation
{
type: "REQUIRED",
fieldId: "experience",
errorMessage: "Experience is required.",
},
{
type: "TYPE_CHECK",
fieldId: "experience",
expectedType: "number",
errorMessage: "Experience must be a number.",
},
// References validation
{
type: "DEPENDENCY",
fieldId: "references",
dependentFieldId: "experience",
dependentOperator: "LESS_THAN",
dependentValue: 5,
operator: "NOT_EMPTY",
errorMessage: "References are required if experience is less than 5 years.",
},
// Preferred Job Role validation
{
type: "DEPENDENCY",
fieldId: "preferredJobRole",
dependentFieldId: "experience",
dependentOperator: "GREATER_THAN",
dependentValue: 2,
operator: "NOT_EMPTY",
errorMessage:
"Preferred Job Role is required for applicants with more than 2 years of experience.",
},
// Cover Letter validation
{
type: "DEPENDENCY",
fieldId: "coverLetter",
dependentFieldId: "experience",
dependentOperator: "GREATER_THAN",
dependentValue: 5,
operator: "NOT_EMPTY",
errorMessage:
"Cover Letter is required for applicants with more than 5 years of experience.",
},
{
type: "COMPARISON",
fieldId: "magicField",
operator: "IS_MAGIC",
errorMessage: "The value must be 'magic'.",
},
{
type: "COMPARISON",
fieldId: "customPatternField",
operator: "MATCH_CUSTOM_PATTERN",
value: "magic",
errorMessage: "The field must contain the custom pattern 'magic'.",
},
{
type: "TYPE_CHECK",
fieldId: "validMagicField",
expectedType: "magicString",
errorMessage: "The value must be a magicString type.",
},
{
type: "TYPE_CHECK",
fieldId: "positiveField",
expectedType: "positiveInteger",
errorMessage: "The value must be a positive integer.",
},
{
type: "TYPE_CHECK",
fieldId: "invalidMagicField",
expectedType: "magicString",
errorMessage: "The value must be a magicString type.",
},
{
type: "TYPE_CHECK",
fieldId: "negativeField",
expectedType: "positiveInteger",
errorMessage: "The value must be a positive integer.",
},
];
const controller = new ValidationController();
// Validate the form with the updated rules
controller
.validateForm(formData, rules)
.then((validationResults) => {
if (validationResults.hasErrors) {
console.log("Validation Errors:");
Object.entries(validationResults.details).forEach(([fieldId, errors]) => {
errors.forEach((error) => {
console.log(`- ${error.message} (Field: ${fieldId})`);
});
});
} else {
console.log("All fields validated successfully!");
}
})
.catch((error) => {
console.error("Error during validation:", error);
});