@fincity/kirun-js 2.10.0 → 2.11.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.
@@ -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
+ });