@fincity/kirun-js 2.10.0 → 2.12.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/__tests__/engine/function/system/HashDataTest.ts +353 -0
- package/__tests__/engine/function/system/ValidateSchemaTest.ts +157 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/module.js +1 -1
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/engine/function/system/HashData.ts +110 -0
- package/src/engine/function/system/ValidateSchema.ts +76 -0
- package/src/engine/repository/KIRunFunctionRepository.ts +2 -0
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
import { HashData } from '../../../../src/engine/function/system/HashData';
|
|
2
|
+
import { FunctionExecutionParameters } from '../../../../src/engine/runtime/FunctionExecutionParameters';
|
|
3
|
+
import { KIRunFunctionRepository, KIRunSchemaRepository } from '../../../../src';
|
|
4
|
+
import { MapUtil } from '../../../../src/engine/util/MapUtil';
|
|
5
|
+
|
|
6
|
+
const hashData = new HashData();
|
|
7
|
+
|
|
8
|
+
describe('HashData', () => {
|
|
9
|
+
test('should hash integer value with default algorithm', async () => {
|
|
10
|
+
const fep = new FunctionExecutionParameters(
|
|
11
|
+
new KIRunFunctionRepository(),
|
|
12
|
+
new KIRunSchemaRepository()
|
|
13
|
+
).setArguments(
|
|
14
|
+
MapUtil.of(HashData.PARAMETER_DATA, 12345)
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
const result = (await hashData.execute(fep))
|
|
18
|
+
.allResults()[0]
|
|
19
|
+
.getResult()
|
|
20
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
21
|
+
|
|
22
|
+
expect(result).toBe('5994471abb01112afcc18159f6cc74b4f511b99806da59b3caf5a9c173cacfc5');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('should hash double value with default algorithm', async () => {
|
|
26
|
+
const fep = new FunctionExecutionParameters(
|
|
27
|
+
new KIRunFunctionRepository(),
|
|
28
|
+
new KIRunSchemaRepository()
|
|
29
|
+
).setArguments(
|
|
30
|
+
MapUtil.of(HashData.PARAMETER_DATA, 123.45)
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
const result = (await hashData.execute(fep))
|
|
34
|
+
.allResults()[0]
|
|
35
|
+
.getResult()
|
|
36
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
37
|
+
|
|
38
|
+
expect(result).toBe('4ebc4a141b378980461430980948a55988fbf56f85d084ac33d8a8f61b9fab88');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('should hash simple string with default algorithm', async () => {
|
|
42
|
+
const fep = new FunctionExecutionParameters(
|
|
43
|
+
new KIRunFunctionRepository(),
|
|
44
|
+
new KIRunSchemaRepository()
|
|
45
|
+
).setArguments(
|
|
46
|
+
MapUtil.of(HashData.PARAMETER_DATA, 'test string')
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const result = (await hashData.execute(fep))
|
|
50
|
+
.allResults()[0]
|
|
51
|
+
.getResult()
|
|
52
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
53
|
+
|
|
54
|
+
expect(result).toBe('ee68a16fef8bf44a2b86b5614554b4079820f98dea14a67c3b507f59333cd591');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('should hash object with default algorithm', async () => {
|
|
58
|
+
const testObject = { name: 'Kailash', age: 23, city: 'Bengaluru' };
|
|
59
|
+
|
|
60
|
+
const fep = new FunctionExecutionParameters(
|
|
61
|
+
new KIRunFunctionRepository(),
|
|
62
|
+
new KIRunSchemaRepository()
|
|
63
|
+
).setArguments(
|
|
64
|
+
MapUtil.of(HashData.PARAMETER_DATA, testObject)
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
const result = (await hashData.execute(fep))
|
|
68
|
+
.allResults()[0]
|
|
69
|
+
.getResult()
|
|
70
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
71
|
+
|
|
72
|
+
expect(result).toBe('99ca912ea0516d95398daa6503a1748b1e39a2f82350e8ece10f8f8355570a46');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test('should hash object with primitive level true and default algorithm', async () => {
|
|
76
|
+
const testObject = { name: "Kailash", age: 23, city: 'Bengaluru' };
|
|
77
|
+
|
|
78
|
+
const fep = new FunctionExecutionParameters(
|
|
79
|
+
new KIRunFunctionRepository(),
|
|
80
|
+
new KIRunSchemaRepository()
|
|
81
|
+
).setArguments(
|
|
82
|
+
new Map<string, any>([
|
|
83
|
+
[HashData.PARAMETER_DATA, testObject],
|
|
84
|
+
[HashData.PARAMETER_PRIMITIVE_LEVEL, true]
|
|
85
|
+
])
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
const result = (await hashData.execute(fep))
|
|
89
|
+
.allResults()[0]
|
|
90
|
+
.getResult()
|
|
91
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
92
|
+
|
|
93
|
+
// Expected object with hashed keys and values
|
|
94
|
+
const expectedObject = {
|
|
95
|
+
// "name": "Kailash"
|
|
96
|
+
'6ffe58b85cad95f1e86f1afebc4ffba738ef6c5520811cb5f393f6c0234ffcb0': '587ba9b0b3222f5af3ad1c7495536e2d0356a9881f45059dcf619923e543b7cc',
|
|
97
|
+
// "age": 23
|
|
98
|
+
'78eb1fc4755453033fe186d682406e53543f575fa2b98887eceb8736e7d24567': '535fa30d7e25dd8a49f1536779734ec8286108d115da5045d77f3b4185d8f790',
|
|
99
|
+
// "city": "Bengaluru"
|
|
100
|
+
'b295162ccd7483bcf4f715b22b84d053efb490c5fd1beb6799751456d37c1ad1': 'afd27606e80dfd77bac4f51dabbb2ecf0ac061dd07b0fe58fc1cdde9a70ad3be'
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
expect(JSON.stringify(result)).toEqual(JSON.stringify(expectedObject));
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test('should hash array with primitive level true', async () => {
|
|
107
|
+
const testArray = ['test', 123, true, null];
|
|
108
|
+
|
|
109
|
+
const fep = new FunctionExecutionParameters(
|
|
110
|
+
new KIRunFunctionRepository(),
|
|
111
|
+
new KIRunSchemaRepository()
|
|
112
|
+
).setArguments(
|
|
113
|
+
new Map<string, any>([
|
|
114
|
+
[HashData.PARAMETER_DATA, testArray],
|
|
115
|
+
[HashData.PARAMETER_PRIMITIVE_LEVEL, true]
|
|
116
|
+
])
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
const result = (await hashData.execute(fep))
|
|
120
|
+
.allResults()[0]
|
|
121
|
+
.getResult()
|
|
122
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
123
|
+
|
|
124
|
+
expect(JSON.stringify(result)).toEqual(JSON.stringify([
|
|
125
|
+
'4d967a30111bf29f0eba01c448b375c1629b2fed01cdfcc3aed91f1b57d5dd5e',
|
|
126
|
+
'a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3',
|
|
127
|
+
'b5bea41b6c623f7c09f1bf24dcae58ebab3c0cdd90ad966bc43a45b44867e12b',
|
|
128
|
+
'null'
|
|
129
|
+
]));
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test('should hash with different algorithm (md5)', async () => {
|
|
133
|
+
const fep = new FunctionExecutionParameters(
|
|
134
|
+
new KIRunFunctionRepository(),
|
|
135
|
+
new KIRunSchemaRepository()
|
|
136
|
+
).setArguments(
|
|
137
|
+
new Map<string, any>([
|
|
138
|
+
[HashData.PARAMETER_DATA, 'test string'],
|
|
139
|
+
[HashData.PARAMETER_ALGORITHM, 'md5']
|
|
140
|
+
])
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
const result = (await hashData.execute(fep))
|
|
144
|
+
.allResults()[0]
|
|
145
|
+
.getResult()
|
|
146
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
147
|
+
|
|
148
|
+
expect(result).toBe('520a193597c1170fc7f00c6e77df571f');
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test('should handle null and undefined values', async () => {
|
|
152
|
+
const fepNull = new FunctionExecutionParameters(
|
|
153
|
+
new KIRunFunctionRepository(),
|
|
154
|
+
new KIRunSchemaRepository()
|
|
155
|
+
).setArguments(
|
|
156
|
+
MapUtil.of(HashData.PARAMETER_DATA, null)
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
const resultNull = (await hashData.execute(fepNull))
|
|
160
|
+
.allResults()[0]
|
|
161
|
+
.getResult()
|
|
162
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
163
|
+
|
|
164
|
+
expect(resultNull).toBe("null");
|
|
165
|
+
|
|
166
|
+
const fepUndefined = new FunctionExecutionParameters(
|
|
167
|
+
new KIRunFunctionRepository(),
|
|
168
|
+
new KIRunSchemaRepository()
|
|
169
|
+
).setArguments(
|
|
170
|
+
MapUtil.of(HashData.PARAMETER_DATA, undefined)
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
const resultUndefined = (await hashData.execute(fepUndefined))
|
|
174
|
+
.allResults()[0]
|
|
175
|
+
.getResult()
|
|
176
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
177
|
+
|
|
178
|
+
expect(resultUndefined).toBe("null");
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test('should hash nested objects with primitive level', async () => {
|
|
182
|
+
const nestedObject = {
|
|
183
|
+
user: {
|
|
184
|
+
name: 'John',
|
|
185
|
+
contacts: {
|
|
186
|
+
email: 'john@example.com',
|
|
187
|
+
phone: '1234567890'
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
active: true
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const fep = new FunctionExecutionParameters(
|
|
194
|
+
new KIRunFunctionRepository(),
|
|
195
|
+
new KIRunSchemaRepository()
|
|
196
|
+
).setArguments(
|
|
197
|
+
new Map<string, any>([
|
|
198
|
+
[HashData.PARAMETER_DATA, nestedObject],
|
|
199
|
+
[HashData.PARAMETER_PRIMITIVE_LEVEL, true]
|
|
200
|
+
])
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
const result = (await hashData.execute(fep))
|
|
204
|
+
.allResults()[0]
|
|
205
|
+
.getResult()
|
|
206
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
207
|
+
|
|
208
|
+
const expectedObject = {
|
|
209
|
+
"3190d261d186aeead3a8deec202737c7775af5c8d455a9e5ba958c48b5fd3f59": {
|
|
210
|
+
"6ffe58b85cad95f1e86f1afebc4ffba738ef6c5520811cb5f393f6c0234ffcb0": "152f3ca566617488c2450ba9a88bdb8ef1593c860c496c39386d68cfe351df3f",
|
|
211
|
+
"ffc121712a18c1513ded336647a0ccf7d369ba1927ac68c5d3272ac54535d633": {
|
|
212
|
+
"d55b99efba68c4d57a901b891ddbaa9d1d8ad6c52dd62390b00204efc3170441": "aaa07cfdec182c9ab13a25f43b0a84356bfe3ef199658fd4e15f96e5af54b665",
|
|
213
|
+
"d5511d58ae89a5136544686b7e471128789e3309c2ec9b520c217067f6abb75d": "9f191b3167af0649edeb8888946bee1c523b87aff6219fa7765ac731bca74fb1"
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
"db0fb2bf5ebc424454c3e11b5ee8bfb43af24a52e561aa49e94143831dc6fd93": "b5bea41b6c623f7c09f1bf24dcae58ebab3c0cdd90ad966bc43a45b44867e12b"
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
expect(JSON.stringify(result)).toEqual(JSON.stringify(expectedObject));
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test('Hash Meta Audience - Primitive Level - true', async () => {
|
|
223
|
+
const hashData = new HashData();
|
|
224
|
+
|
|
225
|
+
const nestedObject = [
|
|
226
|
+
[
|
|
227
|
+
"kunal",
|
|
228
|
+
"jha",
|
|
229
|
+
NaN,
|
|
230
|
+
9922552168
|
|
231
|
+
],
|
|
232
|
+
[
|
|
233
|
+
"Saleem",
|
|
234
|
+
NaN,
|
|
235
|
+
"gssaleem22@gmail.com",
|
|
236
|
+
9845367369
|
|
237
|
+
],
|
|
238
|
+
[
|
|
239
|
+
"Sunil",
|
|
240
|
+
"Gayathri",
|
|
241
|
+
NaN,
|
|
242
|
+
9900511830
|
|
243
|
+
],
|
|
244
|
+
[
|
|
245
|
+
"Hemant",
|
|
246
|
+
"Hemant",
|
|
247
|
+
NaN,
|
|
248
|
+
9619006766
|
|
249
|
+
],
|
|
250
|
+
[
|
|
251
|
+
"Hazarath",
|
|
252
|
+
"Sarabu",
|
|
253
|
+
"hazarath@yahoo.com",
|
|
254
|
+
9848514909
|
|
255
|
+
]
|
|
256
|
+
];
|
|
257
|
+
|
|
258
|
+
const fep = new FunctionExecutionParameters(
|
|
259
|
+
new KIRunFunctionRepository(),
|
|
260
|
+
new KIRunSchemaRepository()
|
|
261
|
+
).setArguments(
|
|
262
|
+
new Map<string, any>([
|
|
263
|
+
[HashData.PARAMETER_DATA, nestedObject],
|
|
264
|
+
[HashData.PARAMETER_ALGORITHM, 'sha256'],
|
|
265
|
+
[HashData.PARAMETER_PRIMITIVE_LEVEL, true]
|
|
266
|
+
])
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
const result = (await hashData.execute(fep))
|
|
270
|
+
.allResults()[0]
|
|
271
|
+
.getResult()
|
|
272
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
273
|
+
|
|
274
|
+
const expectedObject = [
|
|
275
|
+
[
|
|
276
|
+
'5c5ec4214a4f90fc81d20698f3b58971a65f99be8f807167c0dc357dbfca6d64',
|
|
277
|
+
'1a14fb90285bbdc811ec90c31535f8cbfe7a190cdea4388ffe5bfb5149373c85',
|
|
278
|
+
'74234e98afe7498fb5daf1f36ac2d78acc339464f950703b8c019892f982b90b',
|
|
279
|
+
'c73e1c882b48b71df3346a638b86a361d4ad58bd5119bed829c193192ea0bd12'
|
|
280
|
+
],
|
|
281
|
+
[
|
|
282
|
+
'b0a368eae50806c434c5dc52ea1743c033e20e28b37e95da3f515d426c412f24',
|
|
283
|
+
"74234e98afe7498fb5daf1f36ac2d78acc339464f950703b8c019892f982b90b",
|
|
284
|
+
"6aa7533f4df03ce2cdb94fbd3af0df01d3ad798997224b31bbd972c8f4110d98",
|
|
285
|
+
"55dd6fc4584ff7971599839a58ea554e0ceabf8ea54108c2afe96b9600ad22de"
|
|
286
|
+
],
|
|
287
|
+
[
|
|
288
|
+
"d4ac1f574136969f501b11f8312613addf81a738bd32d318ff57b14c57074c46",
|
|
289
|
+
"d77c692372de396251e5a30d31f8f0d1e729fc2d2ddf38286d4f9133aa7849de",
|
|
290
|
+
"74234e98afe7498fb5daf1f36ac2d78acc339464f950703b8c019892f982b90b",
|
|
291
|
+
"859aee372e6161388c67a2ebe39ea30da2f09d6e4dca715be79109d8c9c9a946"
|
|
292
|
+
],
|
|
293
|
+
[
|
|
294
|
+
"8ff58050b37737f3b0d5b9541c1cb1f67394b41631bfdc927ab216ff580af445",
|
|
295
|
+
"8ff58050b37737f3b0d5b9541c1cb1f67394b41631bfdc927ab216ff580af445",
|
|
296
|
+
"74234e98afe7498fb5daf1f36ac2d78acc339464f950703b8c019892f982b90b",
|
|
297
|
+
"3aaf978a21568f3132e586b5fab896a46930831d8362ae3a630f9b0e1ac44d5a"
|
|
298
|
+
],
|
|
299
|
+
[
|
|
300
|
+
"ff1363632be96e64e74f8f6ce09e899dc93243a0aba24ae8d1e901df266e783b",
|
|
301
|
+
"60124d4663c3581f6d5ab01d3a8652e67c83a0eea14d2eebc617132386a63533",
|
|
302
|
+
"5df5828651d67e079dbaa41176ed84c87e1823a316abd2a55ade9c9045dadb0a",
|
|
303
|
+
"c6701bb10611df1b1ac0f1bc3d008200a81ccb78a2e3e71db8d0e6b6ad6bca83"
|
|
304
|
+
]
|
|
305
|
+
]
|
|
306
|
+
|
|
307
|
+
expect(JSON.stringify(result)).toEqual(JSON.stringify(expectedObject));
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
test('should hash 2D array with primitive level true', async () => {
|
|
311
|
+
const twoDArray = [
|
|
312
|
+
['a', 1, true],
|
|
313
|
+
['b', 2, false],
|
|
314
|
+
['c', 3, null]
|
|
315
|
+
];
|
|
316
|
+
|
|
317
|
+
const fep = new FunctionExecutionParameters(
|
|
318
|
+
new KIRunFunctionRepository(),
|
|
319
|
+
new KIRunSchemaRepository()
|
|
320
|
+
).setArguments(
|
|
321
|
+
new Map<string, any>([
|
|
322
|
+
[HashData.PARAMETER_DATA, twoDArray],
|
|
323
|
+
[HashData.PARAMETER_PRIMITIVE_LEVEL, true]
|
|
324
|
+
])
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
const result = (await hashData.execute(fep))
|
|
328
|
+
.allResults()[0]
|
|
329
|
+
.getResult()
|
|
330
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
331
|
+
|
|
332
|
+
const expectedArray = [
|
|
333
|
+
[
|
|
334
|
+
'ac8d8342bbb2362d13f0a559a3621bb407011368895164b628a54f7fc33fc43c', // hash of "a"
|
|
335
|
+
'6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b', // hash of 1
|
|
336
|
+
'b5bea41b6c623f7c09f1bf24dcae58ebab3c0cdd90ad966bc43a45b44867e12b' // hash of true
|
|
337
|
+
],
|
|
338
|
+
[
|
|
339
|
+
'c100f95c1913f9c72fc1f4ef0847e1e723ffe0bde0b36e5f36c13f81fe8c26ed', // hash of "b"
|
|
340
|
+
'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35', // hash of 2
|
|
341
|
+
'fcbcf165908dd18a9e49f7ff27810176db8e9f63b4352213741664245224f8aa' // hash of false
|
|
342
|
+
],
|
|
343
|
+
[
|
|
344
|
+
'879923da020d1533f4d8e921ea7bac61e8ba41d3c89d17a4d14e3a89c6780d5d', // hash of "c"
|
|
345
|
+
'4e07408562bedb8b60ce05c1decfe3ad16b72230967de01f640b7e4729b49fce', // hash of 3
|
|
346
|
+
'null' // null value
|
|
347
|
+
]
|
|
348
|
+
];
|
|
349
|
+
|
|
350
|
+
expect(JSON.stringify(result)).toEqual(JSON.stringify(expectedArray));
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
});
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { FunctionExecutionParameters, KIRunFunctionRepository, KIRunSchemaRepository } from '../../../../src';
|
|
2
|
+
import { ValidateSchema } from '../../../../src/engine/function/system/ValidateSchema';
|
|
3
|
+
|
|
4
|
+
describe('ValidateSchema', () => {
|
|
5
|
+
let validator: ValidateSchema;
|
|
6
|
+
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
validator = new ValidateSchema();
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
test('complex object validation should pass', async () => {
|
|
12
|
+
const schema = {
|
|
13
|
+
name: 'lead',
|
|
14
|
+
type: ['OBJECT'],
|
|
15
|
+
version: 1,
|
|
16
|
+
properties: {
|
|
17
|
+
name: {
|
|
18
|
+
type: ['STRING'],
|
|
19
|
+
minLength: 3
|
|
20
|
+
},
|
|
21
|
+
mobileNumber: {
|
|
22
|
+
type: ['STRING'],
|
|
23
|
+
minLength: 3
|
|
24
|
+
},
|
|
25
|
+
formType: {
|
|
26
|
+
type: ['STRING'],
|
|
27
|
+
enums: ['LEAD_FORM', 'CONTACT_FORM']
|
|
28
|
+
},
|
|
29
|
+
email: {
|
|
30
|
+
type: ['STRING']
|
|
31
|
+
},
|
|
32
|
+
address: {
|
|
33
|
+
type: ['OBJECT'],
|
|
34
|
+
properties: {
|
|
35
|
+
street: {
|
|
36
|
+
type: ['STRING'],
|
|
37
|
+
minLength: 5
|
|
38
|
+
},
|
|
39
|
+
city: {
|
|
40
|
+
type: ['STRING']
|
|
41
|
+
},
|
|
42
|
+
state: {
|
|
43
|
+
type: ['STRING']
|
|
44
|
+
},
|
|
45
|
+
postalCode: {
|
|
46
|
+
type: ['STRING'],
|
|
47
|
+
pattern: '^[0-9]{6}$'
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
required: ['street', 'city', 'state']
|
|
51
|
+
},
|
|
52
|
+
employment: {
|
|
53
|
+
type: ['OBJECT'],
|
|
54
|
+
properties: {
|
|
55
|
+
company: {
|
|
56
|
+
type: ['STRING']
|
|
57
|
+
},
|
|
58
|
+
position: {
|
|
59
|
+
type: ['STRING']
|
|
60
|
+
},
|
|
61
|
+
experience: {
|
|
62
|
+
type: ['INTEGER'],
|
|
63
|
+
minimum: 0
|
|
64
|
+
},
|
|
65
|
+
skills: {
|
|
66
|
+
type: ['ARRAY'],
|
|
67
|
+
items: {
|
|
68
|
+
type: ['STRING']
|
|
69
|
+
},
|
|
70
|
+
minItems: 1
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
required: ['company', 'position']
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
required: ['name', 'email', 'formType']
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const source = {
|
|
80
|
+
name: 'John Doe',
|
|
81
|
+
mobileNumber: '9876543210',
|
|
82
|
+
formType: 'LEAD_FORM',
|
|
83
|
+
email: 'john.doe@example.com',
|
|
84
|
+
address: {
|
|
85
|
+
street: '123 Main Street',
|
|
86
|
+
city: 'New York',
|
|
87
|
+
state: 'NY',
|
|
88
|
+
postalCode: '100001'
|
|
89
|
+
},
|
|
90
|
+
employment: {
|
|
91
|
+
company: 'Tech Corp',
|
|
92
|
+
position: 'Senior Developer',
|
|
93
|
+
experience: 5,
|
|
94
|
+
skills: ['Java', 'Spring', 'React']
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
await expectValidation(schema, source, true);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test('simple string validation should pass', async () => {
|
|
102
|
+
const schema = {
|
|
103
|
+
type: ['STRING'],
|
|
104
|
+
minLength: 3,
|
|
105
|
+
maxLength: 10
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
await expectValidation(schema, 'Hello', true);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test('number validation should pass', async () => {
|
|
112
|
+
const schema = {
|
|
113
|
+
type: ['INTEGER'],
|
|
114
|
+
minimum: 0,
|
|
115
|
+
maximum: 100
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
await expectValidation(schema, 50, true);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test('simple array validation should pass', async () => {
|
|
122
|
+
const schema = {
|
|
123
|
+
type: ['ARRAY'],
|
|
124
|
+
items: {
|
|
125
|
+
type: ['STRING']
|
|
126
|
+
},
|
|
127
|
+
minItems: 1,
|
|
128
|
+
maxItems: 3
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
await expectValidation(schema, ['item1', 'item2'], true);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test('validation should fail for invalid input', async () => {
|
|
135
|
+
const schema = {
|
|
136
|
+
type: ['STRING'],
|
|
137
|
+
minLength: 5
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
await expectValidation(schema, 'Hi', false);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
async function expectValidation(schema: any, source: any, expectedResult: boolean) {
|
|
144
|
+
const context = new FunctionExecutionParameters(
|
|
145
|
+
new KIRunFunctionRepository(),
|
|
146
|
+
new KIRunSchemaRepository()
|
|
147
|
+
).setArguments(new Map([
|
|
148
|
+
['source', source],
|
|
149
|
+
['schema', schema]
|
|
150
|
+
]));
|
|
151
|
+
|
|
152
|
+
const result = await validator.execute(context);
|
|
153
|
+
const isValid = result.allResults()[0].getResult().get('isValid');
|
|
154
|
+
|
|
155
|
+
expect(isValid).toBe(expectedResult);
|
|
156
|
+
}
|
|
157
|
+
});
|