-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjectDataTypeValidation.js
More file actions
217 lines (202 loc) · 5.36 KB
/
objectDataTypeValidation.js
File metadata and controls
217 lines (202 loc) · 5.36 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
const isSupporedType = (type) => {
// if not valid raise error
let supporedTypes = ["string", "number", "boolean", "array", "undefined"];
return supporedTypes.includes(type);
};
const validateObject = (obj, schema) => {
for (let key in schema) {
// if schema is not direct type (number ,string ,bolean,undefined)
if (typeof obj[key] !== schema[key]) {
if (typeof schema[key] === "object") {
if (!validateObject(obj[key], schema[key])) {
console.log(`${key} is not valid object`);
return false;
} else {
console.log(`${key} is valid [${key}:{}]`);
}
} else if (schema[key] === "array") {
if (Array.isArray(obj[key]))
console.log(`${key} is valid type. ${"array"} == ${schema[key]}`);
else {
console.log(`${key} is not valid array`);
return false;
}
} else {
console.log(`${key} is not valid array or object]`);
return false;
}
} else if (!isSupporedType(schema[key])) {
throw new Error(`${key} is not supported type [${schema[key]}]`);
} else {
console.log(`${key} is valid type. ${typeof obj[key]} == ${schema[key]}`);
}
}
return true;
};
// Test Cases
console.log("###########################################################");
console.log("###################### start testing ######################");
console.log("###########################################################");
// Helper function to run tests and log results
const runTest = (testName, obj, schema, expected) => {
console.log(`\nRunning test: ${testName}`);
const result = validateObject(obj, schema);
if (result === expected) {
console.log(`Test ${testName} passed.`);
} else {
console.error(
`
*******************************************************************
* Test ${testName} failed. Expected ${expected}, but got ${result}*
*******************************************************************
${JSON.stringify(obj, null, 2)}
${JSON.stringify(schema, null, 2)}
`
);
}
};
// Test Case 1: Valid Object
const schema1 = {
name: "string",
age: "number",
hobbies: "array",
isMarried: "boolean",
address: {
city: "string",
zip: "number",
},
};
const obj1 = {
name: "John Doe",
age: 30,
hobbies: ["reading", "hiking"],
isMarried: true,
address: {
city: "New York",
zip: 10001,
},
};
runTest("Valid Object", obj1, schema1, true);
// Test Case 2: Invalid Object - Wrong Type
const obj2 = {
name: "Jane Doe",
age: "30", // Should be a number
hobbies: ["reading"],
isMarried: false,
address: {
city: "London",
zip: 2000,
},
};
runTest("Invalid Object - Wrong Type", obj2, schema1, false);
// Test Case 3: Invalid Object - Missing Key
const obj3 = {
name: "Peter Pan",
age: 25,
hobbies: ["flying"],
// isMarried is missing
address: {
city: "Neverland",
zip: 0,
},
};
runTest("Invalid Object - Missing Key", obj3, schema1, false); // it will pass because the function only check if the key is exist in the object not the opposite
// Test Case 4: Invalid Object - Nested Wrong Type
const obj4 = {
name: "Alice",
age: 22,
hobbies: ["wondering"],
isMarried: false,
address: {
city: 123, // Should be a string
zip: 90210,
},
};
runTest("Invalid Object - Nested Wrong Type", obj4, schema1, false);
// Test Case 5: Invalid Object - Nested Missing Key
const obj5 = {
name: "Bob",
age: 40,
hobbies: ["coding"],
isMarried: true,
address: {
// city is missing
zip: 12345,
},
};
runTest("Invalid Object - Nested Missing Key", obj5, schema1, false); // it will pass because the function only check if the key is exist in the object not the opposite
// Test Case 6: Valid Object - Empty Object
const schema6 = {};
const obj6 = {};
runTest("Valid Object - Empty Object", obj6, schema6, true);
// Test Case 7: Invalid Object - Array instead of object
const schema7 = {
name: "string",
address: {
city: "string",
},
};
const obj7 = {
name: "test",
address: ["test"],
};
runTest("Invalid Object - Array instead of object", obj7, schema7, false);
// Test Case 8: Valid Object - undefined value
const schema8 = {
name: "string",
value: "undefined",
};
const obj8 = {
name: "test",
value: undefined,
};
runTest("Valid Object - undefined value", obj8, schema8, true);
// Test Case 9: Invalid Object - wrong undefined value
const schema9 = {
name: "string",
value: "undefined",
};
const obj9 = {
name: "test",
value: "test",
};
runTest("Invalid Object - wrong undefined value", obj9, schema9, false);
// Test Case 10: Valid Object - array value
const schema10 = {
name: "string",
value: "array",
};
const obj10 = {
name: "test",
value: ["test"],
};
runTest("Valid Object - array value", obj10, schema10, true);
// Test Case 11: Invalid Object - wrong array value
const schema11 = {
name: "string",
value: "array",
};
const obj11 = {
name: "test",
value: "test",
};
runTest("Invalid Object - wrong array value", obj11, schema11, false);
// Test Case 12: Invalid schema - unsupported type
const schema12 = {
name: "string",
value: "date",
};
const obj12 = {
name: "test",
value: "test",
};
try {
runTest("Invalid schema - unsupported type", obj12, schema12, false);
} catch (error) {
console.error(
`Test Invalid schema - unsupported type passed with error: ${error.message}`
);
}
console.log("###########################################################");
console.log("###################### end testing ######################");
console.log("###########################################################");