@fincity/kirun-js 2.4.0 → 2.7.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.
Files changed (39) hide show
  1. package/__tests__/engine/function/system/math/MathFunctionRepositoryTest.ts +9 -18
  2. package/__tests__/engine/function/system/math/MaximumTest.ts +3 -2
  3. package/__tests__/engine/function/system/math/MinimumTest.ts +18 -20
  4. package/__tests__/engine/json/schema/covnertor/BooleanConvertorTest.ts +55 -0
  5. package/__tests__/engine/json/schema/covnertor/NullConvertorTest.ts +47 -0
  6. package/__tests__/engine/json/schema/covnertor/NumberConvertorTest.ts +154 -0
  7. package/__tests__/engine/json/schema/covnertor/StringConvertorTest.ts +56 -0
  8. package/__tests__/engine/json/schema/validator/AnyOfAllOfOneOfValidatorTest.ts +9 -7
  9. package/__tests__/engine/json/schema/validator/NotValidatorTest.ts +3 -3
  10. package/__tests__/engine/json/schema/validator/StringValidatorTest.ts +1 -9
  11. package/__tests__/engine/json/schema/validator/TypeValidatorTest.ts +290 -0
  12. package/__tests__/engine/runtime/expression/ExpressionEvaluationTest.ts +80 -0
  13. package/__tests__/engine/runtime/expression/ExpressionEvaluatorStringLiteralTest.ts +73 -1
  14. package/dist/index.js +1 -1
  15. package/dist/index.js.map +1 -1
  16. package/dist/module.js +1 -1
  17. package/dist/module.js.map +1 -1
  18. package/dist/types.d.ts +14 -8
  19. package/dist/types.d.ts.map +1 -1
  20. package/generator/generateValidationCSV.ts +6 -1
  21. package/generator/validation-js.csv +0 -0
  22. package/package.json +4 -1
  23. package/src/engine/function/system/object/ObjectConvert.ts +73 -0
  24. package/src/engine/function/system/object/ObjectPutValue.ts +0 -2
  25. package/src/engine/json/schema/convertor/BooleanConvertor.ts +76 -0
  26. package/src/engine/json/schema/convertor/NullConvertor.ts +31 -0
  27. package/src/engine/json/schema/convertor/NumberConvertor.ts +117 -0
  28. package/src/engine/json/schema/convertor/StringConvertor.ts +41 -0
  29. package/src/engine/json/schema/convertor/enums/ConversionMode.ts +11 -0
  30. package/src/engine/json/schema/convertor/exception/SchemaConversionException.ts +39 -0
  31. package/src/engine/json/schema/validator/AnyOfAllOfOneOfValidator.ts +96 -36
  32. package/src/engine/json/schema/validator/ArrayValidator.ts +15 -7
  33. package/src/engine/json/schema/validator/ObjectValidator.ts +24 -13
  34. package/src/engine/json/schema/validator/SchemaValidator.ts +74 -22
  35. package/src/engine/json/schema/validator/TypeValidator.ts +136 -23
  36. package/src/engine/runtime/expression/tokenextractor/TokenValueExtractor.ts +63 -59
  37. package/src/engine/util/json/ConvertorUtil.ts +51 -0
  38. package/src/engine/util/json/ValidatorUtil.ts +29 -0
  39. package/tsconfig.json +2 -1
@@ -0,0 +1,290 @@
1
+ import {
2
+ ArrayValidator,
3
+ BooleanValidator,
4
+ KIRunSchemaRepository,
5
+ MapUtil,
6
+ NumberValidator,
7
+ Schema,
8
+ SchemaType,
9
+ StringValidator,
10
+ TypeValidator,
11
+ } from '../../../../../src';
12
+ import { StringConvertor } from '../../../../../src/engine/json/schema/convertor/StringConvertor';
13
+ import { ConversionMode } from '../../../../../src/engine/json/schema/convertor/enums/ConversionMode';
14
+ import { NumberConvertor } from '../../../../../src/engine/json/schema/convertor/NumberConvertor';
15
+ import { BooleanConvertor } from '../../../../../src/engine/json/schema/convertor/BooleanConvertor';
16
+ import { NullConvertor } from '../../../../../src/engine/json/schema/convertor/NullConvertor';
17
+
18
+ const repo = new KIRunSchemaRepository();
19
+
20
+ test('Type Validator for Number', async () => {
21
+ const element = { value: 123 };
22
+ const schema = new Schema();
23
+
24
+ const result = await TypeValidator.validate(
25
+ [],
26
+ SchemaType.INTEGER,
27
+ schema,
28
+ repo,
29
+ element.value,
30
+ );
31
+ expect(result).toEqual(NumberValidator.validate(SchemaType.INTEGER, [], schema, element.value));
32
+ });
33
+
34
+ test('Type Validator for String', async () => {
35
+ const element = { value: 'string' };
36
+ const schema = new Schema();
37
+
38
+ const result = await TypeValidator.validate([], SchemaType.STRING, schema, repo, element.value);
39
+ expect(result).toEqual(StringValidator.validate([], schema, element.value));
40
+ });
41
+
42
+ test('Type Validator for Boolean', async () => {
43
+ const element = { value: true };
44
+ const schema = new Schema();
45
+
46
+ const result = await TypeValidator.validate(
47
+ [],
48
+ SchemaType.BOOLEAN,
49
+ schema,
50
+ repo,
51
+ element.value,
52
+ );
53
+ expect(result).toEqual(BooleanValidator.validate([], schema, element.value));
54
+ });
55
+
56
+ test('Type Validator for Array', async () => {
57
+ const schema = new Schema();
58
+ const array = ['abc'];
59
+
60
+ const result = await TypeValidator.validate([], SchemaType.ARRAY, schema, repo, array);
61
+ const expected = await ArrayValidator.validate([], schema, repo, array);
62
+ expect(result).toEqual(expected);
63
+ });
64
+
65
+ test('Type Validator for Null', async () => {
66
+ const schema = new Schema();
67
+
68
+ const result = await TypeValidator.validate([], SchemaType.NULL, schema, repo, null);
69
+ expect(result).toBeNull();
70
+ });
71
+
72
+ test('Type Validator with String Conversion', async () => {
73
+ const element = { value: 'string' };
74
+ const schema = new Schema();
75
+
76
+ const result = await TypeValidator.validate(
77
+ [],
78
+ SchemaType.STRING,
79
+ schema,
80
+ repo,
81
+ element.value,
82
+ true,
83
+ ConversionMode.STRICT,
84
+ );
85
+ expect(result).toEqual(
86
+ StringConvertor.convert([], schema, ConversionMode.STRICT, element.value),
87
+ );
88
+ });
89
+
90
+ test('Type Validator with Number Conversion', async () => {
91
+ const element = { value: '12345' };
92
+ const schema = new Schema();
93
+
94
+ const result = await TypeValidator.validate(
95
+ [],
96
+ SchemaType.INTEGER,
97
+ schema,
98
+ repo,
99
+ element.value,
100
+ true,
101
+ ConversionMode.STRICT,
102
+ );
103
+ expect(result).toEqual(
104
+ NumberConvertor.convert(
105
+ [],
106
+ SchemaType.INTEGER,
107
+ schema,
108
+ ConversionMode.STRICT,
109
+ element.value,
110
+ ),
111
+ );
112
+ });
113
+
114
+ test('Type Validator with Boolean Conversion', async () => {
115
+ const element = { value: 'true' };
116
+ const schema = new Schema();
117
+
118
+ const result = await TypeValidator.validate(
119
+ [],
120
+ SchemaType.BOOLEAN,
121
+ schema,
122
+ repo,
123
+ element.value,
124
+ true,
125
+ ConversionMode.STRICT,
126
+ );
127
+ expect(result).toEqual(
128
+ BooleanConvertor.convert([], schema, ConversionMode.STRICT, element.value),
129
+ );
130
+ });
131
+
132
+ test('Type Validator with Null Conversion', async () => {
133
+ const element = null;
134
+ const schema = new Schema();
135
+
136
+ const result = await TypeValidator.validate(
137
+ [],
138
+ SchemaType.NULL,
139
+ schema,
140
+ repo,
141
+ element,
142
+ true,
143
+ ConversionMode.STRICT,
144
+ );
145
+ expect(result).toEqual(NullConvertor.convert([], schema, ConversionMode.STRICT, element));
146
+ });
147
+
148
+ test('Boolean Convertor Array', async () => {
149
+ const schema = Schema.ofArray('boolean', Schema.ofBoolean('boolean'));
150
+
151
+ const booleanArray: (string | number | boolean)[] = [true, false, 'yes', 'no', 'y', 'n', 1, 0];
152
+
153
+ const expectedArray: boolean[] = [true, false, true, false, true, false, true, false];
154
+
155
+ const result: any = await TypeValidator.validate(
156
+ [],
157
+ SchemaType.ARRAY,
158
+ schema,
159
+ repo,
160
+ booleanArray,
161
+ true,
162
+ ConversionMode.STRICT,
163
+ );
164
+
165
+ expect(result).toEqual(expectedArray);
166
+ });
167
+
168
+ test('Number Convertor Array', async () => {
169
+ const schema = Schema.ofArray('number', Schema.ofNumber('number'));
170
+
171
+ const numberArray = ['11', '11.12', '11192371231', '1123.123', '0'];
172
+
173
+ const expectedArray = [11, 11.12, 11192371231, 1123.123, 0];
174
+
175
+ const result = await TypeValidator.validate(
176
+ [],
177
+ SchemaType.ARRAY,
178
+ schema,
179
+ repo,
180
+ numberArray,
181
+ true,
182
+ ConversionMode.LENIENT,
183
+ );
184
+
185
+ expect(result).toEqual(expectedArray);
186
+ });
187
+
188
+ test('Object Conversion', async () => {
189
+ const numberArray: string[] = ['11', '11.12', '11192371231', '1123.123', '0'];
190
+
191
+ const object = {
192
+ int: '1997',
193
+ long: '123123123',
194
+ float: '123.12',
195
+ double: '123.1232',
196
+ booleanTrue: 'true',
197
+ booleanFalse: 'false',
198
+ string: 12314,
199
+ numberArray: numberArray,
200
+ };
201
+
202
+ const schema: Schema = Schema.ofObject('jsonObject').setProperties(
203
+ MapUtil.of(
204
+ 'int',
205
+ Schema.ofNumber('int'),
206
+ 'long',
207
+ Schema.ofLong('long'),
208
+ 'float',
209
+ Schema.ofFloat('float'),
210
+ 'double',
211
+ Schema.ofNumber('double'),
212
+ 'booleanTrue',
213
+ Schema.ofBoolean('booleanTrue'),
214
+ 'booleanFalse',
215
+ Schema.ofBoolean('booleanFalse'),
216
+ 'string',
217
+ Schema.ofString('string'),
218
+ 'numberArray',
219
+ Schema.ofArray('number', Schema.ofNumber('number')),
220
+ ),
221
+ );
222
+
223
+ const expectedObject = {
224
+ int: 1997,
225
+ long: 123123123,
226
+ float: 123.12,
227
+ double: 123.1232,
228
+ booleanTrue: true,
229
+ booleanFalse: false,
230
+ string: '12314',
231
+ numberArray: [11, 11.12, 11192371231, 1123.123, 0],
232
+ };
233
+
234
+ const result: any = await TypeValidator.validate(
235
+ [],
236
+ SchemaType.OBJECT,
237
+ schema,
238
+ repo,
239
+ object,
240
+ true,
241
+ ConversionMode.STRICT,
242
+ );
243
+
244
+ expect(result).toEqual(expectedObject);
245
+ });
246
+
247
+ test('Object of Object Conversion', async () => {
248
+ const innerObject = {
249
+ innerInt: '42',
250
+ innerString: 'innerValue',
251
+ };
252
+
253
+ const outerObject = {
254
+ innerObject: innerObject,
255
+ outerInt: '100',
256
+ };
257
+
258
+ const innerSchema: Schema = Schema.ofObject('innerObject').setProperties(
259
+ MapUtil.of(
260
+ 'innerInt',
261
+ Schema.ofNumber('innerInt'),
262
+ 'innerString',
263
+ Schema.ofString('innerString'),
264
+ ),
265
+ );
266
+
267
+ const outerSchema: Schema = Schema.ofObject('outerObject').setProperties(
268
+ MapUtil.of('innerObject', innerSchema, 'outerInt', Schema.ofNumber('outerInt')),
269
+ );
270
+
271
+ const expectedOuterObject = {
272
+ innerObject: {
273
+ innerInt: 42,
274
+ innerString: 'innerValue',
275
+ },
276
+ outerInt: 100,
277
+ };
278
+
279
+ const result: any = await TypeValidator.validate(
280
+ [],
281
+ SchemaType.OBJECT,
282
+ outerSchema,
283
+ repo,
284
+ outerObject,
285
+ true,
286
+ ConversionMode.STRICT,
287
+ );
288
+
289
+ expect(result).toEqual(expectedOuterObject);
290
+ });
@@ -139,6 +139,86 @@ test('Expression Test', () => {
139
139
  );
140
140
  });
141
141
 
142
+ test('Expression Evaluation with Square Access Bracket', () => {
143
+ let phone = { phone1: '1234', phone2: '5678', phone3: '5678' };
144
+
145
+ let address = {
146
+ line1: 'Flat 202, PVR Estates',
147
+ line2: 'Nagvara',
148
+ city: 'Benguluru',
149
+ pin: '560048',
150
+ phone: phone,
151
+ };
152
+
153
+ let arr = [10, 20, 30];
154
+
155
+ let obj = {
156
+ studentName: 'Kumar',
157
+ math: 20,
158
+ isStudent: true,
159
+ address: address,
160
+ array: arr,
161
+ num: 1,
162
+ };
163
+
164
+ let inMap: Map<string, any> = new Map();
165
+ inMap.set('name', 'Kiran');
166
+ inMap.set('obj', obj);
167
+
168
+ let output: Map<string, Map<string, Map<string, any>>> = new Map([
169
+ ['step1', new Map([['output', inMap]])],
170
+ ]);
171
+
172
+ let parameters: FunctionExecutionParameters = new FunctionExecutionParameters(
173
+ new KIRunFunctionRepository(),
174
+ new KIRunSchemaRepository(),
175
+ )
176
+ .setArguments(new Map())
177
+ .setContext(new Map())
178
+ .setSteps(output);
179
+
180
+ // New test cases for square access bracket using parameters value map
181
+ expect(
182
+ new ExpressionEvaluator('Steps.step1.output.obj.phone.phone2 = Steps.step1.output.obj["phone"]["phone2"]')
183
+ .evaluate(parameters.getValuesMap())
184
+ ).toBe(true);
185
+
186
+ expect(
187
+ new ExpressionEvaluator('Steps.step1.output.obj["phone"]["phone2"] = Steps.step1.output.obj["phone"]["phone2"]')
188
+ .evaluate(parameters.getValuesMap())
189
+ ).toBe(true);
190
+
191
+ expect(
192
+ new ExpressionEvaluator('Steps.step1.output.obj["address"].phone["phone2"] != Steps.step1.output.address.obj.phone.phone1')
193
+ .evaluate(parameters.getValuesMap())
194
+ ).toBe(true);
195
+
196
+ expect(
197
+ new ExpressionEvaluator('Steps.step1.output.obj["address"]["phone"]["phone2"] != Steps.step1.output.address.obj.phone.phone1')
198
+ .evaluate(parameters.getValuesMap())
199
+ ).toBe(true);
200
+
201
+ expect(
202
+ new ExpressionEvaluator('Steps.step1.output.obj["address"]["phone"]["phone2"] != Steps.step1.output["address"]["phone"]["phone2"]')
203
+ .evaluate(parameters.getValuesMap())
204
+ ).toBe(true);
205
+
206
+ expect(
207
+ new ExpressionEvaluator('Steps.step1.output.obj.array[Steps.step1.output.obj["num"] + 1] + 2')
208
+ .evaluate(parameters.getValuesMap())
209
+ ).toBe(32);
210
+
211
+ expect(
212
+ new ExpressionEvaluator('Steps.step1.output.obj.array[Steps.step1.output.obj["num"] + 1] + Steps.step1.output.obj.array[Steps.step1.output.obj["num"] + 1]')
213
+ .evaluate(parameters.getValuesMap())
214
+ ).toBe(60);
215
+
216
+ expect(
217
+ new ExpressionEvaluator('Steps.step1.output.obj.array[Steps.step1.output.obj.num + 1] + Steps.step1.output.obj.array[Steps.step1.output.obj.num + 1]')
218
+ .evaluate(parameters.getValuesMap())
219
+ ).toBe(60);
220
+ });
221
+
142
222
  test('ExpressionEvaluation deep tests', () => {
143
223
  let atv: ArgumentsTokenValueExtractor = new ArgumentsTokenValueExtractor(
144
224
  new Map<string, any>([
@@ -4,7 +4,7 @@ import {
4
4
  MapUtil,
5
5
  TokenValueExtractor,
6
6
  } from '../../../../src';
7
- import { ExpressionEvaluator } from '../../../../src/engine/runtime/expression/ExpressionEvaluator';
7
+ import { ExpressionEvaluator } from '../../../../src';
8
8
 
9
9
  test('Expression with String Literal - 1 ', () => {
10
10
  let ex: Expression = new Expression("'ki/run'+'ab'");
@@ -98,3 +98,75 @@ test('Expression with String Literal - 2 ', () => {
98
98
  ev = new ExpressionEvaluator('2.val');
99
99
  expect(ev.evaluate(valuesMap)).toBe('2.val');
100
100
  });
101
+
102
+ test('Testing for string length with object', () => {
103
+ const jsonObj = {
104
+ greeting: 'hello',
105
+ name: 'surendhar'
106
+ };
107
+
108
+ let atv: ArgumentsTokenValueExtractor = new ArgumentsTokenValueExtractor(
109
+ new Map<string, any>([
110
+ ['a', 'surendhar '],
111
+ ['b', 2],
112
+ ['c', true],
113
+ ['d', 1.5],
114
+ ['obj', jsonObj],
115
+ ]),
116
+ );
117
+ const valuesMap: Map<string, TokenValueExtractor> = MapUtil.of(atv.getPrefix(), atv);
118
+
119
+ let ev: ExpressionEvaluator = new ExpressionEvaluator('Arguments.a.length');
120
+ expect(ev.evaluate(valuesMap)).toBe(10);
121
+
122
+ ev = new ExpressionEvaluator('Arguments.b.length');
123
+ expect(() => ev.evaluate(valuesMap)).toThrow();
124
+
125
+ ev = new ExpressionEvaluator('Arguments.obj.greeting.length * "S"');
126
+ expect(ev.evaluate(valuesMap)).toBe('SSSSS');
127
+
128
+ ev = new ExpressionEvaluator('Arguments.obj.greeting.length * "SP"');
129
+ expect(ev.evaluate(valuesMap)).toBe('SPSPSPSPSP');
130
+
131
+ ev = new ExpressionEvaluator('Arguments.obj.name.length ? "fun" : "not Fun"');
132
+ expect(ev.evaluate(valuesMap)).toBe('fun');
133
+ });
134
+
135
+ test('Testing for string length with square brackets', () => {
136
+ const jsonObj = {
137
+ greeting: 'hello',
138
+ name: 'surendhar'
139
+ };
140
+
141
+ let atv: ArgumentsTokenValueExtractor = new ArgumentsTokenValueExtractor(
142
+ new Map<string, any>([
143
+ ['a', 'surendhar '],
144
+ ['b', 2],
145
+ ['c', true],
146
+ ['d', 1.5],
147
+ ['obj', jsonObj],
148
+ ]),
149
+ );
150
+ const valuesMap: Map<string, TokenValueExtractor> = MapUtil.of(atv.getPrefix(), atv);
151
+
152
+ let ev: ExpressionEvaluator = new ExpressionEvaluator('Arguments.a["length"]');
153
+ expect(ev.evaluate(valuesMap)).toBe(10);
154
+
155
+ ev = new ExpressionEvaluator('Arguments.b["length"]');
156
+ expect(() => ev.evaluate(valuesMap)).toThrow();
157
+
158
+ ev = new ExpressionEvaluator('Arguments.obj.greeting["length"] * "S"');
159
+ expect(ev.evaluate(valuesMap)).toBe('SSSSS');
160
+
161
+ ev = new ExpressionEvaluator('Arguments.obj.greeting["length"] * "SP"');
162
+ expect(ev.evaluate(valuesMap)).toBe('SPSPSPSPSP');
163
+
164
+ ev = new ExpressionEvaluator('Arguments.obj["greeting"]["length"] * "S"');
165
+ expect(ev.evaluate(valuesMap)).toBe('SSSSS');
166
+
167
+ ev = new ExpressionEvaluator('Arguments.obj["greeting"]["length"] * "SP"');
168
+ expect(ev.evaluate(valuesMap)).toBe('SPSPSPSPSP');
169
+
170
+ ev = new ExpressionEvaluator('Arguments.obj.name["length"] ? "fun" : "not Fun"');
171
+ expect(ev.evaluate(valuesMap)).toBe('fun');
172
+ });