@fincity/kirun-js 1.0.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 (217) hide show
  1. package/.prettierrc +9 -0
  2. package/__tests__/engine/function/system/array/AddFirstTest.ts +235 -0
  3. package/__tests__/engine/function/system/array/AddTest.ts +126 -0
  4. package/__tests__/engine/function/system/array/BinarySearchTest.ts +135 -0
  5. package/__tests__/engine/function/system/array/CompareTest.ts +48 -0
  6. package/__tests__/engine/function/system/array/CopyTest.ts +86 -0
  7. package/__tests__/engine/function/system/array/DeleteFirstTest.ts +103 -0
  8. package/__tests__/engine/function/system/array/DeleteFromTest.ts +232 -0
  9. package/__tests__/engine/function/system/array/DeleteLastTest.ts +103 -0
  10. package/__tests__/engine/function/system/array/DeleteTest.ts +110 -0
  11. package/__tests__/engine/function/system/array/DisjointTest.ts +184 -0
  12. package/__tests__/engine/function/system/array/Equals.ts +67 -0
  13. package/__tests__/engine/function/system/array/FillTest.ts +36 -0
  14. package/__tests__/engine/function/system/array/FrequencyTest.ts +97 -0
  15. package/__tests__/engine/function/system/array/IndexOfArrayTest.ts +347 -0
  16. package/__tests__/engine/function/system/array/IndexOfTest.ts +267 -0
  17. package/__tests__/engine/function/system/array/InsertTest.ts +229 -0
  18. package/__tests__/engine/function/system/array/LastIndexOfArrayTest.ts +213 -0
  19. package/__tests__/engine/function/system/array/LastIndexOfTest.ts +253 -0
  20. package/__tests__/engine/function/system/array/MaxTest.ts +98 -0
  21. package/__tests__/engine/function/system/array/MinTest.ts +99 -0
  22. package/__tests__/engine/function/system/array/MisMatchTest.ts +215 -0
  23. package/__tests__/engine/function/system/array/ReverseTest.ts +235 -0
  24. package/__tests__/engine/function/system/array/RotateTest.ts +137 -0
  25. package/__tests__/engine/function/system/array/ShuffleTest.ts +154 -0
  26. package/__tests__/engine/function/system/array/SortTest.ts +130 -0
  27. package/__tests__/engine/function/system/array/SubArrayTest.ts +236 -0
  28. package/__tests__/engine/function/system/math/AddTest.ts +12 -0
  29. package/__tests__/engine/function/system/string/ConcatenateTest.ts +46 -0
  30. package/__tests__/engine/function/system/string/DeleteForGivenLengthTest.ts +38 -0
  31. package/__tests__/engine/function/system/string/InsertAtGivenPositionTest.ts +53 -0
  32. package/__tests__/engine/function/system/string/PostPadTest.ts +54 -0
  33. package/__tests__/engine/function/system/string/PrePadTest.ts +54 -0
  34. package/__tests__/engine/function/system/string/RegionMatchesTest.ts +78 -0
  35. package/__tests__/engine/function/system/string/ReverseTest.ts +36 -0
  36. package/__tests__/engine/function/system/string/SplitTest.ts +61 -0
  37. package/__tests__/engine/function/system/string/StringFunctionRepoTest2.ts +126 -0
  38. package/__tests__/engine/function/system/string/StringFunctionRepoTest3.ts +54 -0
  39. package/__tests__/engine/function/system/string/StringFunctionRepositoryTest.ts +146 -0
  40. package/__tests__/engine/function/system/string/ToStringTest.ts +43 -0
  41. package/__tests__/engine/function/system/string/TrimToTest.ts +28 -0
  42. package/__tests__/engine/json/schema/validator/SchemaValidatorTest.ts +26 -0
  43. package/__tests__/engine/runtime/KIRuntimeTest.ts +351 -0
  44. package/__tests__/engine/runtime/expression/ExpressionEvaluationTest.ts +128 -0
  45. package/__tests__/engine/runtime/expression/ExpressionTest.ts +33 -0
  46. package/__tests__/engine/runtime/expression/tokenextractor/OutputMapTokenValueExtractorTest.ts +44 -0
  47. package/__tests__/engine/runtime/expression/tokenextractor/TokenValueExtractorTest.ts +72 -0
  48. package/__tests__/engine/util/LinkedListTest.ts +29 -0
  49. package/__tests__/engine/util/string/StringFormatterTest.ts +17 -0
  50. package/__tests__/indexTest.ts +33 -0
  51. package/dist/index.js +2 -0
  52. package/dist/index.js.map +1 -0
  53. package/dist/module.js +2 -0
  54. package/dist/module.js.map +1 -0
  55. package/dist/types.d.ts +430 -0
  56. package/dist/types.d.ts.map +1 -0
  57. package/package.json +54 -0
  58. package/src/engine/HybridRepository.ts +18 -0
  59. package/src/engine/Repository.ts +3 -0
  60. package/src/engine/constant/KIRunConstants.ts +7 -0
  61. package/src/engine/exception/ExecutionException.ts +12 -0
  62. package/src/engine/exception/KIRuntimeException.ts +12 -0
  63. package/src/engine/function/AbstractFunction.ts +76 -0
  64. package/src/engine/function/Function.ts +13 -0
  65. package/src/engine/function/system/GenerateEvent.ts +76 -0
  66. package/src/engine/function/system/If.ts +40 -0
  67. package/src/engine/function/system/array/AbstractArrayFunction.ts +158 -0
  68. package/src/engine/function/system/array/Add.ts +31 -0
  69. package/src/engine/function/system/array/AddFirst.ts +44 -0
  70. package/src/engine/function/system/array/BinarySearch.ts +66 -0
  71. package/src/engine/function/system/array/Compare.ts +150 -0
  72. package/src/engine/function/system/array/Copy.ts +56 -0
  73. package/src/engine/function/system/array/Delete.ts +63 -0
  74. package/src/engine/function/system/array/DeleteFirst.ts +22 -0
  75. package/src/engine/function/system/array/DeleteFrom.ts +51 -0
  76. package/src/engine/function/system/array/DeleteLast.ts +23 -0
  77. package/src/engine/function/system/array/Disjoint.ts +85 -0
  78. package/src/engine/function/system/array/Equals.ts +36 -0
  79. package/src/engine/function/system/array/Fill.ts +51 -0
  80. package/src/engine/function/system/array/Frequency.ts +68 -0
  81. package/src/engine/function/system/array/IndexOf.ts +56 -0
  82. package/src/engine/function/system/array/IndexOfArray.ts +67 -0
  83. package/src/engine/function/system/array/Insert.ts +48 -0
  84. package/src/engine/function/system/array/LastIndexOf.ts +62 -0
  85. package/src/engine/function/system/array/LastIndexOfArray.ts +70 -0
  86. package/src/engine/function/system/array/Max.ts +31 -0
  87. package/src/engine/function/system/array/Min.ts +32 -0
  88. package/src/engine/function/system/array/MisMatch.ts +66 -0
  89. package/src/engine/function/system/array/Reverse.ts +50 -0
  90. package/src/engine/function/system/array/Rotate.ts +43 -0
  91. package/src/engine/function/system/array/Shuffle.ts +31 -0
  92. package/src/engine/function/system/array/Sort.ts +70 -0
  93. package/src/engine/function/system/array/SubArray.ts +48 -0
  94. package/src/engine/function/system/context/Create.ts +73 -0
  95. package/src/engine/function/system/context/Get.ts +55 -0
  96. package/src/engine/function/system/context/SetFunction.ts +244 -0
  97. package/src/engine/function/system/loop/CountLoop.ts +55 -0
  98. package/src/engine/function/system/loop/RangeLoop.ts +120 -0
  99. package/src/engine/function/system/math/Add.ts +34 -0
  100. package/src/engine/function/system/math/GenericMathFunction.ts +80 -0
  101. package/src/engine/function/system/math/Hypotenuse.ts +57 -0
  102. package/src/engine/function/system/math/MathFunctionRepository.ts +63 -0
  103. package/src/engine/function/system/math/Maximum.ts +38 -0
  104. package/src/engine/function/system/math/Minimum.ts +38 -0
  105. package/src/engine/function/system/math/Random.ts +27 -0
  106. package/src/engine/function/system/string/AbstractStringFunction.ts +409 -0
  107. package/src/engine/function/system/string/Concatenate.ts +57 -0
  108. package/src/engine/function/system/string/DeleteForGivenLength.ts +82 -0
  109. package/src/engine/function/system/string/InsertAtGivenPosition.ts +72 -0
  110. package/src/engine/function/system/string/PostPad.ts +83 -0
  111. package/src/engine/function/system/string/PrePad.ts +73 -0
  112. package/src/engine/function/system/string/RegionMatches.ts +119 -0
  113. package/src/engine/function/system/string/ReplaceAtGivenPosition.ts +101 -0
  114. package/src/engine/function/system/string/Reverse.ts +62 -0
  115. package/src/engine/function/system/string/Split.ts +53 -0
  116. package/src/engine/function/system/string/StringFunctionRepository.ts +60 -0
  117. package/src/engine/function/system/string/ToString.ts +45 -0
  118. package/src/engine/function/system/string/TrimTo.ts +55 -0
  119. package/src/engine/json/JsonExpression.ts +11 -0
  120. package/src/engine/json/schema/Schema.ts +670 -0
  121. package/src/engine/json/schema/SchemaUtil.ts +145 -0
  122. package/src/engine/json/schema/array/ArraySchemaType.ts +44 -0
  123. package/src/engine/json/schema/object/AdditionalPropertiesType.ts +32 -0
  124. package/src/engine/json/schema/string/StringFormat.ts +7 -0
  125. package/src/engine/json/schema/type/MultipleType.ts +28 -0
  126. package/src/engine/json/schema/type/SchemaType.ts +11 -0
  127. package/src/engine/json/schema/type/SingleType.ts +23 -0
  128. package/src/engine/json/schema/type/Type.ts +6 -0
  129. package/src/engine/json/schema/type/TypeUtil.ts +25 -0
  130. package/src/engine/json/schema/validator/AnyOfAllOfOneOfValidator.ts +108 -0
  131. package/src/engine/json/schema/validator/ArrayValidator.ts +145 -0
  132. package/src/engine/json/schema/validator/BooleanValidator.ts +24 -0
  133. package/src/engine/json/schema/validator/NullValidator.ts +17 -0
  134. package/src/engine/json/schema/validator/NumberValidator.ts +115 -0
  135. package/src/engine/json/schema/validator/ObjectValidator.ts +184 -0
  136. package/src/engine/json/schema/validator/SchemaValidator.ts +141 -0
  137. package/src/engine/json/schema/validator/StringValidator.ts +97 -0
  138. package/src/engine/json/schema/validator/TypeValidator.ts +49 -0
  139. package/src/engine/json/schema/validator/exception/SchemaReferenceException.ts +19 -0
  140. package/src/engine/json/schema/validator/exception/SchemaValidationException.ts +22 -0
  141. package/src/engine/model/AbstractStatement.ts +29 -0
  142. package/src/engine/model/Argument.ts +36 -0
  143. package/src/engine/model/Event.ts +62 -0
  144. package/src/engine/model/EventResult.ts +34 -0
  145. package/src/engine/model/FunctionDefinition.ts +69 -0
  146. package/src/engine/model/FunctionOutput.ts +37 -0
  147. package/src/engine/model/FunctionOutputGenerator.ts +5 -0
  148. package/src/engine/model/FunctionSignature.ts +67 -0
  149. package/src/engine/model/Parameter.ts +97 -0
  150. package/src/engine/model/ParameterReference.ts +57 -0
  151. package/src/engine/model/ParameterReferenceType.ts +4 -0
  152. package/src/engine/model/ParameterType.ts +4 -0
  153. package/src/engine/model/Position.ts +40 -0
  154. package/src/engine/model/Statement.ts +101 -0
  155. package/src/engine/model/StatementGroup.ts +37 -0
  156. package/src/engine/namespaces/Namespaces.ts +11 -0
  157. package/src/engine/repository/KIRunFunctionRepository.ts +37 -0
  158. package/src/engine/repository/KIRunSchemaRepository.ts +24 -0
  159. package/src/engine/runtime/ContextElement.ts +28 -0
  160. package/src/engine/runtime/FunctionExecutionParameters.ts +74 -0
  161. package/src/engine/runtime/KIRuntime.ts +653 -0
  162. package/src/engine/runtime/StatementExecution.ts +61 -0
  163. package/src/engine/runtime/StatementMessage.ts +30 -0
  164. package/src/engine/runtime/StatementMessageType.ts +5 -0
  165. package/src/engine/runtime/expression/Expression.ts +330 -0
  166. package/src/engine/runtime/expression/ExpressionEvaluator.ts +305 -0
  167. package/src/engine/runtime/expression/ExpressionToken.ts +15 -0
  168. package/src/engine/runtime/expression/ExpressionTokenValue.ts +23 -0
  169. package/src/engine/runtime/expression/Operation.ts +190 -0
  170. package/src/engine/runtime/expression/exception/ExpressionEvaluationException.ts +15 -0
  171. package/src/engine/runtime/expression/operators/binary/ArithmeticAdditionOperator.ts +9 -0
  172. package/src/engine/runtime/expression/operators/binary/ArithmeticDivisionOperator.ts +9 -0
  173. package/src/engine/runtime/expression/operators/binary/ArithmeticInetgerDivisionOperator.ts +9 -0
  174. package/src/engine/runtime/expression/operators/binary/ArithmeticModulusOperator.ts +9 -0
  175. package/src/engine/runtime/expression/operators/binary/ArithmeticMultiplicationOperator.ts +9 -0
  176. package/src/engine/runtime/expression/operators/binary/ArithmeticSubtractionOperator.ts +9 -0
  177. package/src/engine/runtime/expression/operators/binary/ArrayOperator.ts +31 -0
  178. package/src/engine/runtime/expression/operators/binary/BinaryOperator.ts +15 -0
  179. package/src/engine/runtime/expression/operators/binary/BitwiseAndOperator.ts +9 -0
  180. package/src/engine/runtime/expression/operators/binary/BitwiseLeftShiftOperator.ts +9 -0
  181. package/src/engine/runtime/expression/operators/binary/BitwiseOrOperator.ts +9 -0
  182. package/src/engine/runtime/expression/operators/binary/BitwiseRightShiftOperator.ts +9 -0
  183. package/src/engine/runtime/expression/operators/binary/BitwiseUnsignedRightShiftOperator.ts +9 -0
  184. package/src/engine/runtime/expression/operators/binary/BitwiseXorOperator.ts +9 -0
  185. package/src/engine/runtime/expression/operators/binary/LogicalAndOperator.ts +25 -0
  186. package/src/engine/runtime/expression/operators/binary/LogicalEqualOperator.ts +13 -0
  187. package/src/engine/runtime/expression/operators/binary/LogicalGreaterThanEqualOperator.ts +24 -0
  188. package/src/engine/runtime/expression/operators/binary/LogicalGreaterThanOperator.ts +24 -0
  189. package/src/engine/runtime/expression/operators/binary/LogicalLessThanEqualOperator.ts +24 -0
  190. package/src/engine/runtime/expression/operators/binary/LogicalLessThanOperator.ts +24 -0
  191. package/src/engine/runtime/expression/operators/binary/LogicalNotEqualOperator.ts +13 -0
  192. package/src/engine/runtime/expression/operators/binary/LogicalOrOperator.ts +25 -0
  193. package/src/engine/runtime/expression/operators/binary/ObjectOperator.ts +24 -0
  194. package/src/engine/runtime/expression/operators/unary/ArithmeticUnaryMinusOperator.ts +13 -0
  195. package/src/engine/runtime/expression/operators/unary/ArithmeticUnaryPlusOperator.ts +13 -0
  196. package/src/engine/runtime/expression/operators/unary/BitwiseComplementOperator.ts +22 -0
  197. package/src/engine/runtime/expression/operators/unary/LogicalNotOperator.ts +22 -0
  198. package/src/engine/runtime/expression/operators/unary/UnaryOperator.ts +15 -0
  199. package/src/engine/runtime/expression/tokenextractor/LiteralTokenValueExtractor.ts +64 -0
  200. package/src/engine/runtime/expression/tokenextractor/TokenValueExtractor.ts +136 -0
  201. package/src/engine/runtime/graph/ExecutionGraph.ts +81 -0
  202. package/src/engine/runtime/graph/GraphVertex.ts +118 -0
  203. package/src/engine/runtime/graph/GraphVertexType.ts +4 -0
  204. package/src/engine/runtime/tokenextractor/ArgumentsTokenValueExtractor.ts +22 -0
  205. package/src/engine/runtime/tokenextractor/ContextTokenValueExtractor.ts +37 -0
  206. package/src/engine/runtime/tokenextractor/OutputMapTokenValueExtractor.ts +32 -0
  207. package/src/engine/util/ArrayUtil.ts +23 -0
  208. package/src/engine/util/LinkedList.ts +229 -0
  209. package/src/engine/util/MapUtil.ts +86 -0
  210. package/src/engine/util/NullCheck.ts +3 -0
  211. package/src/engine/util/Tuples.ts +43 -0
  212. package/src/engine/util/primitive/PrimitiveUtil.ts +143 -0
  213. package/src/engine/util/string/StringBuilder.ts +59 -0
  214. package/src/engine/util/string/StringFormatter.ts +25 -0
  215. package/src/engine/util/string/StringUtil.ts +48 -0
  216. package/src/index.ts +13 -0
  217. package/tsconfig.json +6 -0
@@ -0,0 +1,115 @@
1
+ import { isNullValue } from '../../../util/NullCheck';
2
+ import { Schema } from '../Schema';
3
+ import { SchemaType } from '../type/SchemaType';
4
+ import { SchemaValidationException } from './exception/SchemaValidationException';
5
+ import { SchemaValidator } from './SchemaValidator';
6
+
7
+ export class NumberValidator {
8
+ public static validate(type: SchemaType, parents: Schema[], schema: Schema, element: any): any {
9
+ if (isNullValue(element))
10
+ throw new SchemaValidationException(
11
+ SchemaValidator.path(parents),
12
+ 'Expected a number but found null',
13
+ );
14
+
15
+ if (typeof element !== 'number')
16
+ throw new SchemaValidationException(
17
+ SchemaValidator.path(parents),
18
+ element.toString() + ' is not a ' + type,
19
+ );
20
+
21
+ let n: number = NumberValidator.extractNumber(type, parents, schema, element);
22
+
23
+ NumberValidator.checkRange(parents, schema, element, n);
24
+
25
+ NumberValidator.checkMultipleOf(parents, schema, element, n);
26
+
27
+ return element;
28
+ }
29
+
30
+ private static extractNumber(
31
+ type: SchemaType,
32
+ parents: Schema[],
33
+ schema: Schema,
34
+ element: any,
35
+ ): number {
36
+ let n = element;
37
+
38
+ try {
39
+ if (type == SchemaType.LONG || type == SchemaType.INTEGER) n = Math.round(n);
40
+ } catch (err: any) {
41
+ throw new SchemaValidationException(
42
+ SchemaValidator.path(parents),
43
+ element + ' is not a number of type ' + type,
44
+ err,
45
+ );
46
+ }
47
+
48
+ if (
49
+ isNullValue(n) ||
50
+ ((type == SchemaType.LONG || type == SchemaType.INTEGER) && n != element)
51
+ ) {
52
+ throw new SchemaValidationException(
53
+ SchemaValidator.path(parents),
54
+ element.toString() + ' is not a number of type ' + type,
55
+ );
56
+ }
57
+
58
+ return n;
59
+ }
60
+
61
+ private static checkMultipleOf(parents: Schema[], schema: Schema, element: any, n: number) {
62
+ if (schema.getMultipleOf()) {
63
+ let l1: number = n;
64
+ let l2: number = schema.getMultipleOf()!;
65
+
66
+ if (l1 % l2 != 0)
67
+ throw new SchemaValidationException(
68
+ SchemaValidator.path(parents),
69
+ element.toString() + ' is not multiple of ' + schema.getMultipleOf(),
70
+ );
71
+ }
72
+ }
73
+
74
+ private static checkRange(parents: Schema[], schema: Schema, element: any, n: number) {
75
+ if (schema.getMinimum() && NumberValidator.numberCompare(n, schema.getMinimum()!) < 0) {
76
+ throw new SchemaValidationException(
77
+ SchemaValidator.path(parents),
78
+ element.toString() + ' should be greater than or equal to ' + schema.getMinimum(),
79
+ );
80
+ }
81
+
82
+ if (schema.getMaximum() && NumberValidator.numberCompare(n, schema.getMaximum()!) > 0) {
83
+ throw new SchemaValidationException(
84
+ SchemaValidator.path(parents),
85
+ element.toString() + ' should be less than or equal to ' + schema.getMaximum(),
86
+ );
87
+ }
88
+
89
+ if (
90
+ schema.getExclusiveMinimum() &&
91
+ NumberValidator.numberCompare(n, schema.getExclusiveMinimum()!) <= 0
92
+ ) {
93
+ throw new SchemaValidationException(
94
+ SchemaValidator.path(parents),
95
+ element.toString() + ' should be greater than ' + schema.getExclusiveMinimum(),
96
+ );
97
+ }
98
+
99
+ if (
100
+ schema.getExclusiveMaximum() &&
101
+ NumberValidator.numberCompare(n, schema.getExclusiveMaximum()!) > 0
102
+ ) {
103
+ throw new SchemaValidationException(
104
+ SchemaValidator.path(parents),
105
+ element.toString() + ' should be less than ' + schema.getExclusiveMaximum(),
106
+ );
107
+ }
108
+ }
109
+
110
+ private static numberCompare(n1: number, n2: number): number {
111
+ return n1 - n2;
112
+ }
113
+
114
+ private constructor() {}
115
+ }
@@ -0,0 +1,184 @@
1
+ import { Repository } from '../../../Repository';
2
+ import { isNullValue } from '../../../util/NullCheck';
3
+ import { AdditionalPropertiesType } from '../object/AdditionalPropertiesType';
4
+ import { Schema } from '../Schema';
5
+ import { SchemaValidationException } from './exception/SchemaValidationException';
6
+ import { SchemaValidator } from './SchemaValidator';
7
+
8
+ export class ObjectValidator {
9
+ public static validate(
10
+ parents: Schema[],
11
+ schema: Schema,
12
+ repository: Repository<Schema> | undefined,
13
+ element: any,
14
+ ) {
15
+ if (isNullValue(element))
16
+ throw new SchemaValidationException(
17
+ SchemaValidator.path(parents),
18
+ 'Expected an object but found null',
19
+ );
20
+
21
+ if (typeof element !== 'object' || Array.isArray(element))
22
+ throw new SchemaValidationException(
23
+ SchemaValidator.path(parents),
24
+ element.toString() + ' is not an Object',
25
+ );
26
+
27
+ let jsonObject: any = element;
28
+ let keys: Set<string> = new Set<string>(Object.keys(jsonObject));
29
+
30
+ ObjectValidator.checkMinMaxProperties(parents, schema, keys);
31
+
32
+ if (schema.getPropertyNames()) {
33
+ ObjectValidator.checkPropertyNameSchema(parents, schema, repository, keys);
34
+ }
35
+
36
+ if (schema.getRequired()) {
37
+ ObjectValidator.checkRequired(parents, schema, jsonObject);
38
+ }
39
+
40
+ if (schema.getProperties()) {
41
+ ObjectValidator.checkProperties(parents, schema, repository, jsonObject, keys);
42
+ }
43
+
44
+ if (schema.getPatternProperties()) {
45
+ ObjectValidator.checkPatternProperties(parents, schema, repository, jsonObject, keys);
46
+ }
47
+
48
+ if (schema.getAdditionalProperties()) {
49
+ ObjectValidator.checkAddtionalProperties(parents, schema, repository, jsonObject, keys);
50
+ }
51
+ }
52
+
53
+ private static checkPropertyNameSchema(
54
+ parents: Schema[],
55
+ schema: Schema,
56
+ repository: Repository<Schema> | undefined,
57
+ keys: Set<String>,
58
+ ) {
59
+ for (let key of Array.from(keys.values())) {
60
+ try {
61
+ SchemaValidator.validate(
62
+ parents,
63
+ schema.getPropertyNames() as Schema,
64
+ repository,
65
+ key,
66
+ );
67
+ } catch (err) {
68
+ throw new SchemaValidationException(
69
+ SchemaValidator.path(parents),
70
+ "Property name '" + key + "' does not fit the property schema",
71
+ );
72
+ }
73
+ }
74
+ }
75
+
76
+ private static checkRequired(parents: Schema[], schema: Schema, jsonObject: any) {
77
+ for (const key of schema.getRequired() ?? []) {
78
+ if (isNullValue(jsonObject[key])) {
79
+ throw new SchemaValidationException(
80
+ SchemaValidator.path(parents),
81
+ key + ' is mandatory',
82
+ );
83
+ }
84
+ }
85
+ }
86
+
87
+ private static checkAddtionalProperties(
88
+ parents: Schema[],
89
+ schema: Schema,
90
+ repository: Repository<Schema> | undefined,
91
+ jsonObject: any,
92
+ keys: Set<string>,
93
+ ) {
94
+ let apt: AdditionalPropertiesType = schema.getAdditionalProperties()!;
95
+ if (apt.getSchemaValue()) {
96
+ for (let key of Array.from(keys.values())) {
97
+ let newParents: Schema[] = !parents ? [] : [...parents];
98
+
99
+ let element: any = SchemaValidator.validate(
100
+ newParents,
101
+ apt.getSchemaValue(),
102
+ repository,
103
+ jsonObject.get(key),
104
+ );
105
+ jsonObject[key] = element;
106
+ }
107
+ } else {
108
+ if (apt.getBooleanValue() === false && keys.size) {
109
+ throw new SchemaValidationException(
110
+ SchemaValidator.path(parents),
111
+ keys.toString() + ' are additional properties which are not allowed.',
112
+ );
113
+ }
114
+ }
115
+ }
116
+
117
+ private static checkPatternProperties(
118
+ parents: Schema[],
119
+ schema: Schema,
120
+ repository: Repository<Schema> | undefined,
121
+ jsonObject: any,
122
+ keys: Set<string>,
123
+ ) {
124
+ const compiledPatterns: Map<string, RegExp> = new Map<string, RegExp>();
125
+ for (const keyPattern of Array.from(schema.getPatternProperties()!.keys()))
126
+ compiledPatterns.set(keyPattern, new RegExp(keyPattern));
127
+
128
+ let goodKeys: string[] = [];
129
+
130
+ for (const key of Array.from(keys.values())) {
131
+ const newParents: Schema[] = !parents ? [] : [...parents];
132
+
133
+ for (const e of Array.from(compiledPatterns.entries())) {
134
+ if (e[1].test(key)) {
135
+ const element: any = SchemaValidator.validate(
136
+ newParents,
137
+ schema.getPatternProperties()!.get(e[0]),
138
+ repository,
139
+ jsonObject[key],
140
+ );
141
+ jsonObject[key] = element;
142
+ keys.delete(key);
143
+ break;
144
+ }
145
+ }
146
+ }
147
+ }
148
+
149
+ private static checkProperties(
150
+ parents: Schema[],
151
+ schema: Schema,
152
+ repository: Repository<Schema> | undefined,
153
+ jsonObject: any,
154
+ keys: Set<string>,
155
+ ) {
156
+ for (const each of Array.from(schema.getProperties()!)) {
157
+ let value: any = jsonObject[each[0]];
158
+ if (isNullValue(value)) continue;
159
+
160
+ let newParents: Schema[] = !parents ? [] : [...parents];
161
+ let element: any = SchemaValidator.validate(newParents, each[1], repository, value);
162
+ jsonObject[each[0]] = element;
163
+ keys.delete(each[0]);
164
+ }
165
+ }
166
+
167
+ private static checkMinMaxProperties(parents: Schema[], schema: Schema, keys: Set<string>) {
168
+ if (schema.getMinProperties() && keys.size < schema.getMinProperties()!) {
169
+ throw new SchemaValidationException(
170
+ SchemaValidator.path(parents),
171
+ 'Object should have minimum of ' + schema.getMinProperties() + ' properties',
172
+ );
173
+ }
174
+
175
+ if (schema.getMaxProperties() && keys.size > schema.getMaxProperties()!) {
176
+ throw new SchemaValidationException(
177
+ SchemaValidator.path(parents),
178
+ 'Object can have maximum of ' + schema.getMaxProperties() + ' properties',
179
+ );
180
+ }
181
+ }
182
+
183
+ private constructor() {}
184
+ }
@@ -0,0 +1,141 @@
1
+ import { Repository } from '../../../Repository';
2
+ import { isNullValue } from '../../../util/NullCheck';
3
+ import { StringUtil } from '../../../util/string/StringUtil';
4
+ import { Schema } from '../Schema';
5
+ import { SchemaUtil } from '../SchemaUtil';
6
+ import { SchemaType } from '../type/SchemaType';
7
+ import { AnyOfAllOfOneOfValidator } from './AnyOfAllOfOneOfValidator';
8
+ import { SchemaValidationException } from './exception/SchemaValidationException';
9
+ import { TypeValidator } from './TypeValidator';
10
+
11
+ export class SchemaValidator {
12
+ public static path(parents: Schema[] | undefined): string {
13
+ if (!parents) return '';
14
+
15
+ return parents
16
+ .map((e) => e.getTitle() ?? '')
17
+ .filter((e) => !!e)
18
+ .reduce((a, c, i) => a + (i === 0 ? '' : '.') + c, '');
19
+ }
20
+
21
+ public static validate(
22
+ parents: Schema[] | undefined,
23
+ schema: Schema | undefined,
24
+ repository: Repository<Schema> | undefined,
25
+ element: any,
26
+ ): any {
27
+ if (!schema) {
28
+ return element;
29
+ }
30
+
31
+ if (!parents) {
32
+ parents = new Array();
33
+ }
34
+ parents.push(schema);
35
+
36
+ if (isNullValue(element) && !isNullValue(schema.getDefaultValue())) {
37
+ return JSON.parse(JSON.stringify(schema.getDefaultValue()));
38
+ }
39
+
40
+ if (schema.getConstant()) {
41
+ return SchemaValidator.constantValidation(parents, schema, element);
42
+ }
43
+
44
+ if (schema.getEnums() && !schema.getEnums()?.length) {
45
+ return SchemaValidator.enumCheck(parents, schema, element);
46
+ }
47
+
48
+ if (schema.getType()) {
49
+ SchemaValidator.typeValidation(parents, schema, repository, element);
50
+ }
51
+
52
+ if (!StringUtil.isNullOrBlank(schema.getRef())) {
53
+ return SchemaValidator.validate(
54
+ parents,
55
+ SchemaUtil.getSchemaFromRef(parents[0], repository, schema.getRef()),
56
+ repository,
57
+ element,
58
+ );
59
+ }
60
+
61
+ if (schema.getOneOf() || schema.getAllOf() || schema.getAnyOf()) {
62
+ AnyOfAllOfOneOfValidator.validate(parents, schema, repository, element);
63
+ }
64
+
65
+ if (schema.getNot()) {
66
+ let flag: boolean = false;
67
+ try {
68
+ SchemaValidator.validate(parents, schema.getNot(), repository, element);
69
+ flag = true;
70
+ } catch (err) {
71
+ flag = false;
72
+ }
73
+ if (flag)
74
+ throw new SchemaValidationException(
75
+ SchemaValidator.path(parents),
76
+ 'Schema validated value in not condition.',
77
+ );
78
+ }
79
+
80
+ return element;
81
+ }
82
+
83
+ public static constantValidation(parents: Schema[], schema: Schema, element: any): any {
84
+ if (!schema.getConstant().equals(element)) {
85
+ throw new SchemaValidationException(
86
+ SchemaValidator.path(parents),
87
+ 'Expecting a constant value : ' + element,
88
+ );
89
+ }
90
+ return element;
91
+ }
92
+
93
+ public static enumCheck(parents: Schema[], schema: Schema, element: any): any {
94
+ let x: boolean = false;
95
+ for (let e of schema.getEnums() ?? []) {
96
+ if (e === element) {
97
+ x = true;
98
+ break;
99
+ }
100
+ }
101
+
102
+ if (x) return element;
103
+ else {
104
+ throw new SchemaValidationException(
105
+ SchemaValidator.path(parents),
106
+ 'Value is not one of ' + schema.getEnums(),
107
+ );
108
+ }
109
+ }
110
+
111
+ public static typeValidation(
112
+ parents: Schema[],
113
+ schema: Schema,
114
+ repository: Repository<Schema> | undefined,
115
+ element: any,
116
+ ) {
117
+ let valid: boolean = false;
118
+ let list: SchemaValidationException[] = [];
119
+
120
+ for (const type of Array.from(schema.getType()?.getAllowedSchemaTypes()?.values() ?? [])) {
121
+ try {
122
+ TypeValidator.validate(parents, type, schema, repository, element);
123
+ valid = true;
124
+ break;
125
+ } catch (err: any) {
126
+ valid = false;
127
+ list.push(err);
128
+ }
129
+ }
130
+
131
+ if (!valid) {
132
+ throw new SchemaValidationException(
133
+ SchemaValidator.path(parents),
134
+ 'Value ' + JSON.stringify(element) + ' is not of valid type(s)',
135
+ list,
136
+ );
137
+ }
138
+ }
139
+
140
+ private constructor() {}
141
+ }
@@ -0,0 +1,97 @@
1
+ import { isNullValue } from '../../../util/NullCheck';
2
+ import { Schema } from '../Schema';
3
+ import { StringFormat } from '../string/StringFormat';
4
+ import { SchemaValidationException } from './exception/SchemaValidationException';
5
+ import { SchemaValidator } from './SchemaValidator';
6
+
7
+ export class StringValidator {
8
+ private static readonly TIME: RegExp =
9
+ /^([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?([+-][01][0-9]:[0-5][0-9])?$/;
10
+
11
+ private static readonly DATE: RegExp =
12
+ /^[0-9]{4,4}-([0][0-9]|[1][0-2])-(0[1-9]|[1-2][1-9]|3[01])$/;
13
+
14
+ private static readonly DATETIME: RegExp =
15
+ /^[0-9]{4,4}-([0][0-9]|[1][0-2])-(0[1-9]|[1-2][1-9]|3[01])T([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?([+-][01][0-9]:[0-5][0-9])?$/;
16
+
17
+ public static validate(parents: Schema[], schema: Schema, element: any): any {
18
+ if (isNullValue(element))
19
+ throw new SchemaValidationException(
20
+ SchemaValidator.path(parents),
21
+ 'Expected a string but found null',
22
+ );
23
+
24
+ if (typeof element !== 'string')
25
+ throw new SchemaValidationException(
26
+ SchemaValidator.path(parents),
27
+ element.toString() + ' is not String',
28
+ );
29
+
30
+ if (schema.getFormat() == StringFormat.TIME) {
31
+ StringValidator.patternMatcher(
32
+ parents,
33
+ schema,
34
+ element,
35
+ StringValidator.TIME,
36
+ 'time pattern',
37
+ );
38
+ } else if (schema.getFormat() == StringFormat.DATE) {
39
+ StringValidator.patternMatcher(
40
+ parents,
41
+ schema,
42
+ element,
43
+ StringValidator.DATE,
44
+ 'date pattern',
45
+ );
46
+ } else if (schema.getFormat() == StringFormat.DATETIME) {
47
+ StringValidator.patternMatcher(
48
+ parents,
49
+ schema,
50
+ element,
51
+ StringValidator.DATETIME,
52
+ 'date time pattern',
53
+ );
54
+ } else if (schema.getPattern()) {
55
+ StringValidator.patternMatcher(
56
+ parents,
57
+ schema,
58
+ element,
59
+ new RegExp(schema.getPattern()!),
60
+ 'pattern ' + schema.getPattern(),
61
+ );
62
+ }
63
+
64
+ let length: number = element.length;
65
+ if (schema.getMinLength() && length < schema.getMinLength()!) {
66
+ throw new SchemaValidationException(
67
+ SchemaValidator.path(parents),
68
+ 'Expected a minimum of ' + schema.getMinLength() + ' characters',
69
+ );
70
+ } else if (schema.getMaxLength() && length > schema.getMinLength()!) {
71
+ throw new SchemaValidationException(
72
+ SchemaValidator.path(parents),
73
+ 'Expected a maximum of ' + schema.getMaxLength() + ' characters',
74
+ );
75
+ }
76
+
77
+ return element;
78
+ }
79
+
80
+ private static patternMatcher(
81
+ parents: Schema[],
82
+ schema: Schema,
83
+ element: any,
84
+ pattern: RegExp,
85
+ message: string,
86
+ ) {
87
+ let matched: boolean = pattern.test(element);
88
+ if (!matched) {
89
+ throw new SchemaValidationException(
90
+ SchemaValidator.path(parents),
91
+ element.toString() + ' is not matched with the ' + message,
92
+ );
93
+ }
94
+ }
95
+
96
+ private constructor() {}
97
+ }
@@ -0,0 +1,49 @@
1
+ import { Repository } from '../../../Repository';
2
+ import { Schema } from '../Schema';
3
+ import { SchemaType } from '../type/SchemaType';
4
+ import { ArrayValidator } from './ArrayValidator';
5
+ import { BooleanValidator } from './BooleanValidator';
6
+ import { SchemaValidationException } from './exception/SchemaValidationException';
7
+ import { NullValidator } from './NullValidator';
8
+ import { NumberValidator } from './NumberValidator';
9
+ import { ObjectValidator } from './ObjectValidator';
10
+ import { SchemaValidator } from './SchemaValidator';
11
+ import { StringValidator } from './StringValidator';
12
+
13
+ export class TypeValidator {
14
+ public static validate(
15
+ parents: Schema[],
16
+ type: SchemaType,
17
+ schema: Schema,
18
+ repository: Repository<Schema> |undefined,
19
+ element: any,
20
+ ): any {
21
+ if (type == SchemaType.STRING) {
22
+ StringValidator.validate(parents, schema, element);
23
+ } else if (
24
+ type == SchemaType.LONG ||
25
+ type == SchemaType.INTEGER ||
26
+ type == SchemaType.DOUBLE ||
27
+ type == SchemaType.FLOAT
28
+ ) {
29
+ NumberValidator.validate(type, parents, schema, element);
30
+ } else if (type == SchemaType.BOOLEAN) {
31
+ BooleanValidator.validate(parents, schema, element);
32
+ } else if (type == SchemaType.OBJECT) {
33
+ ObjectValidator.validate(parents, schema, repository, element);
34
+ } else if (type == SchemaType.ARRAY) {
35
+ ArrayValidator.validate(parents, schema, repository, element);
36
+ } else if (type == SchemaType.NULL) {
37
+ NullValidator.validate(parents, schema, element);
38
+ } else {
39
+ throw new SchemaValidationException(
40
+ SchemaValidator.path(parents),
41
+ type + ' is not a valid type.',
42
+ );
43
+ }
44
+
45
+ return element;
46
+ }
47
+
48
+ private constructor() {}
49
+ }
@@ -0,0 +1,19 @@
1
+ export class SchemaReferenceException extends Error {
2
+ private schemaPath: string;
3
+ private cause?: Error;
4
+
5
+ constructor(schemaPath: string, message: string, err?: Error) {
6
+ super(schemaPath.trim() ? schemaPath + '-' + message : message);
7
+
8
+ this.schemaPath = schemaPath;
9
+ this.cause = err;
10
+ }
11
+
12
+ public getSchemaPath(): string {
13
+ return this.schemaPath;
14
+ }
15
+
16
+ public getCause(): Error | undefined {
17
+ return this.cause;
18
+ }
19
+ }
@@ -0,0 +1,22 @@
1
+ export class SchemaValidationException extends Error {
2
+ private schemaPath: string;
3
+ private cause?: Error;
4
+ constructor(
5
+ schemaPath: string,
6
+ message: string,
7
+ sve: SchemaValidationException[] = [],
8
+ err?: Error,
9
+ ) {
10
+ super(message + (sve ? sve.map((e) => e.message).reduce((a, c) => a + '\n' + c, '') : ''));
11
+ this.schemaPath = schemaPath;
12
+ this.cause = err;
13
+ }
14
+
15
+ public getSchemaPath(): string {
16
+ return this.schemaPath;
17
+ }
18
+
19
+ public getCause(): Error | undefined {
20
+ return this.cause;
21
+ }
22
+ }
@@ -0,0 +1,29 @@
1
+ import { Position } from './Position';
2
+
3
+ export class AbstractStatement {
4
+ private comment?: string;
5
+ private description?: string;
6
+ private position?: Position;
7
+
8
+ public getComment(): string | undefined {
9
+ return this.comment;
10
+ }
11
+ public setComment(comment: string): AbstractStatement {
12
+ this.comment = comment;
13
+ return this;
14
+ }
15
+ public getDescription(): string | undefined {
16
+ return this.description;
17
+ }
18
+ public setDescription(description: string): AbstractStatement {
19
+ this.description = description;
20
+ return this;
21
+ }
22
+ public getPosition(): Position | undefined {
23
+ return this.position;
24
+ }
25
+ public setPosition(position: Position): AbstractStatement {
26
+ this.position = position;
27
+ return this;
28
+ }
29
+ }
@@ -0,0 +1,36 @@
1
+ export class Argument {
2
+ private argumentIndex: number = 0;
3
+ private name: string;
4
+ private value: any;
5
+
6
+ public constructor(argumentIndex: number, name: string, value?: any) {
7
+ this.argumentIndex = argumentIndex;
8
+ this.name = name;
9
+ this.value = value;
10
+ }
11
+ public getArgumentIndex(): number {
12
+ return this.argumentIndex;
13
+ }
14
+ public setArgumentIndex(argumentIndex: number): Argument {
15
+ this.argumentIndex = argumentIndex;
16
+ return this;
17
+ }
18
+ public getName(): string {
19
+ return this.name;
20
+ }
21
+ public setName(name: string): Argument {
22
+ this.name = name;
23
+ return this;
24
+ }
25
+ public getValue(): any {
26
+ return this.value;
27
+ }
28
+ public setValue(value: any): Argument {
29
+ this.value = value;
30
+ return this;
31
+ }
32
+
33
+ public static of(name: string, value: any): Argument {
34
+ return new Argument(0, name, value);
35
+ }
36
+ }