@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.
- package/.prettierrc +9 -0
- package/__tests__/engine/function/system/array/AddFirstTest.ts +235 -0
- package/__tests__/engine/function/system/array/AddTest.ts +126 -0
- package/__tests__/engine/function/system/array/BinarySearchTest.ts +135 -0
- package/__tests__/engine/function/system/array/CompareTest.ts +48 -0
- package/__tests__/engine/function/system/array/CopyTest.ts +86 -0
- package/__tests__/engine/function/system/array/DeleteFirstTest.ts +103 -0
- package/__tests__/engine/function/system/array/DeleteFromTest.ts +232 -0
- package/__tests__/engine/function/system/array/DeleteLastTest.ts +103 -0
- package/__tests__/engine/function/system/array/DeleteTest.ts +110 -0
- package/__tests__/engine/function/system/array/DisjointTest.ts +184 -0
- package/__tests__/engine/function/system/array/Equals.ts +67 -0
- package/__tests__/engine/function/system/array/FillTest.ts +36 -0
- package/__tests__/engine/function/system/array/FrequencyTest.ts +97 -0
- package/__tests__/engine/function/system/array/IndexOfArrayTest.ts +347 -0
- package/__tests__/engine/function/system/array/IndexOfTest.ts +267 -0
- package/__tests__/engine/function/system/array/InsertTest.ts +229 -0
- package/__tests__/engine/function/system/array/LastIndexOfArrayTest.ts +213 -0
- package/__tests__/engine/function/system/array/LastIndexOfTest.ts +253 -0
- package/__tests__/engine/function/system/array/MaxTest.ts +98 -0
- package/__tests__/engine/function/system/array/MinTest.ts +99 -0
- package/__tests__/engine/function/system/array/MisMatchTest.ts +215 -0
- package/__tests__/engine/function/system/array/ReverseTest.ts +235 -0
- package/__tests__/engine/function/system/array/RotateTest.ts +137 -0
- package/__tests__/engine/function/system/array/ShuffleTest.ts +154 -0
- package/__tests__/engine/function/system/array/SortTest.ts +130 -0
- package/__tests__/engine/function/system/array/SubArrayTest.ts +236 -0
- package/__tests__/engine/function/system/math/AddTest.ts +12 -0
- package/__tests__/engine/function/system/string/ConcatenateTest.ts +46 -0
- package/__tests__/engine/function/system/string/DeleteForGivenLengthTest.ts +38 -0
- package/__tests__/engine/function/system/string/InsertAtGivenPositionTest.ts +53 -0
- package/__tests__/engine/function/system/string/PostPadTest.ts +54 -0
- package/__tests__/engine/function/system/string/PrePadTest.ts +54 -0
- package/__tests__/engine/function/system/string/RegionMatchesTest.ts +78 -0
- package/__tests__/engine/function/system/string/ReverseTest.ts +36 -0
- package/__tests__/engine/function/system/string/SplitTest.ts +61 -0
- package/__tests__/engine/function/system/string/StringFunctionRepoTest2.ts +126 -0
- package/__tests__/engine/function/system/string/StringFunctionRepoTest3.ts +54 -0
- package/__tests__/engine/function/system/string/StringFunctionRepositoryTest.ts +146 -0
- package/__tests__/engine/function/system/string/ToStringTest.ts +43 -0
- package/__tests__/engine/function/system/string/TrimToTest.ts +28 -0
- package/__tests__/engine/json/schema/validator/SchemaValidatorTest.ts +26 -0
- package/__tests__/engine/runtime/KIRuntimeTest.ts +351 -0
- package/__tests__/engine/runtime/expression/ExpressionEvaluationTest.ts +128 -0
- package/__tests__/engine/runtime/expression/ExpressionTest.ts +33 -0
- package/__tests__/engine/runtime/expression/tokenextractor/OutputMapTokenValueExtractorTest.ts +44 -0
- package/__tests__/engine/runtime/expression/tokenextractor/TokenValueExtractorTest.ts +72 -0
- package/__tests__/engine/util/LinkedListTest.ts +29 -0
- package/__tests__/engine/util/string/StringFormatterTest.ts +17 -0
- package/__tests__/indexTest.ts +33 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/module.js +2 -0
- package/dist/module.js.map +1 -0
- package/dist/types.d.ts +430 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +54 -0
- package/src/engine/HybridRepository.ts +18 -0
- package/src/engine/Repository.ts +3 -0
- package/src/engine/constant/KIRunConstants.ts +7 -0
- package/src/engine/exception/ExecutionException.ts +12 -0
- package/src/engine/exception/KIRuntimeException.ts +12 -0
- package/src/engine/function/AbstractFunction.ts +76 -0
- package/src/engine/function/Function.ts +13 -0
- package/src/engine/function/system/GenerateEvent.ts +76 -0
- package/src/engine/function/system/If.ts +40 -0
- package/src/engine/function/system/array/AbstractArrayFunction.ts +158 -0
- package/src/engine/function/system/array/Add.ts +31 -0
- package/src/engine/function/system/array/AddFirst.ts +44 -0
- package/src/engine/function/system/array/BinarySearch.ts +66 -0
- package/src/engine/function/system/array/Compare.ts +150 -0
- package/src/engine/function/system/array/Copy.ts +56 -0
- package/src/engine/function/system/array/Delete.ts +63 -0
- package/src/engine/function/system/array/DeleteFirst.ts +22 -0
- package/src/engine/function/system/array/DeleteFrom.ts +51 -0
- package/src/engine/function/system/array/DeleteLast.ts +23 -0
- package/src/engine/function/system/array/Disjoint.ts +85 -0
- package/src/engine/function/system/array/Equals.ts +36 -0
- package/src/engine/function/system/array/Fill.ts +51 -0
- package/src/engine/function/system/array/Frequency.ts +68 -0
- package/src/engine/function/system/array/IndexOf.ts +56 -0
- package/src/engine/function/system/array/IndexOfArray.ts +67 -0
- package/src/engine/function/system/array/Insert.ts +48 -0
- package/src/engine/function/system/array/LastIndexOf.ts +62 -0
- package/src/engine/function/system/array/LastIndexOfArray.ts +70 -0
- package/src/engine/function/system/array/Max.ts +31 -0
- package/src/engine/function/system/array/Min.ts +32 -0
- package/src/engine/function/system/array/MisMatch.ts +66 -0
- package/src/engine/function/system/array/Reverse.ts +50 -0
- package/src/engine/function/system/array/Rotate.ts +43 -0
- package/src/engine/function/system/array/Shuffle.ts +31 -0
- package/src/engine/function/system/array/Sort.ts +70 -0
- package/src/engine/function/system/array/SubArray.ts +48 -0
- package/src/engine/function/system/context/Create.ts +73 -0
- package/src/engine/function/system/context/Get.ts +55 -0
- package/src/engine/function/system/context/SetFunction.ts +244 -0
- package/src/engine/function/system/loop/CountLoop.ts +55 -0
- package/src/engine/function/system/loop/RangeLoop.ts +120 -0
- package/src/engine/function/system/math/Add.ts +34 -0
- package/src/engine/function/system/math/GenericMathFunction.ts +80 -0
- package/src/engine/function/system/math/Hypotenuse.ts +57 -0
- package/src/engine/function/system/math/MathFunctionRepository.ts +63 -0
- package/src/engine/function/system/math/Maximum.ts +38 -0
- package/src/engine/function/system/math/Minimum.ts +38 -0
- package/src/engine/function/system/math/Random.ts +27 -0
- package/src/engine/function/system/string/AbstractStringFunction.ts +409 -0
- package/src/engine/function/system/string/Concatenate.ts +57 -0
- package/src/engine/function/system/string/DeleteForGivenLength.ts +82 -0
- package/src/engine/function/system/string/InsertAtGivenPosition.ts +72 -0
- package/src/engine/function/system/string/PostPad.ts +83 -0
- package/src/engine/function/system/string/PrePad.ts +73 -0
- package/src/engine/function/system/string/RegionMatches.ts +119 -0
- package/src/engine/function/system/string/ReplaceAtGivenPosition.ts +101 -0
- package/src/engine/function/system/string/Reverse.ts +62 -0
- package/src/engine/function/system/string/Split.ts +53 -0
- package/src/engine/function/system/string/StringFunctionRepository.ts +60 -0
- package/src/engine/function/system/string/ToString.ts +45 -0
- package/src/engine/function/system/string/TrimTo.ts +55 -0
- package/src/engine/json/JsonExpression.ts +11 -0
- package/src/engine/json/schema/Schema.ts +670 -0
- package/src/engine/json/schema/SchemaUtil.ts +145 -0
- package/src/engine/json/schema/array/ArraySchemaType.ts +44 -0
- package/src/engine/json/schema/object/AdditionalPropertiesType.ts +32 -0
- package/src/engine/json/schema/string/StringFormat.ts +7 -0
- package/src/engine/json/schema/type/MultipleType.ts +28 -0
- package/src/engine/json/schema/type/SchemaType.ts +11 -0
- package/src/engine/json/schema/type/SingleType.ts +23 -0
- package/src/engine/json/schema/type/Type.ts +6 -0
- package/src/engine/json/schema/type/TypeUtil.ts +25 -0
- package/src/engine/json/schema/validator/AnyOfAllOfOneOfValidator.ts +108 -0
- package/src/engine/json/schema/validator/ArrayValidator.ts +145 -0
- package/src/engine/json/schema/validator/BooleanValidator.ts +24 -0
- package/src/engine/json/schema/validator/NullValidator.ts +17 -0
- package/src/engine/json/schema/validator/NumberValidator.ts +115 -0
- package/src/engine/json/schema/validator/ObjectValidator.ts +184 -0
- package/src/engine/json/schema/validator/SchemaValidator.ts +141 -0
- package/src/engine/json/schema/validator/StringValidator.ts +97 -0
- package/src/engine/json/schema/validator/TypeValidator.ts +49 -0
- package/src/engine/json/schema/validator/exception/SchemaReferenceException.ts +19 -0
- package/src/engine/json/schema/validator/exception/SchemaValidationException.ts +22 -0
- package/src/engine/model/AbstractStatement.ts +29 -0
- package/src/engine/model/Argument.ts +36 -0
- package/src/engine/model/Event.ts +62 -0
- package/src/engine/model/EventResult.ts +34 -0
- package/src/engine/model/FunctionDefinition.ts +69 -0
- package/src/engine/model/FunctionOutput.ts +37 -0
- package/src/engine/model/FunctionOutputGenerator.ts +5 -0
- package/src/engine/model/FunctionSignature.ts +67 -0
- package/src/engine/model/Parameter.ts +97 -0
- package/src/engine/model/ParameterReference.ts +57 -0
- package/src/engine/model/ParameterReferenceType.ts +4 -0
- package/src/engine/model/ParameterType.ts +4 -0
- package/src/engine/model/Position.ts +40 -0
- package/src/engine/model/Statement.ts +101 -0
- package/src/engine/model/StatementGroup.ts +37 -0
- package/src/engine/namespaces/Namespaces.ts +11 -0
- package/src/engine/repository/KIRunFunctionRepository.ts +37 -0
- package/src/engine/repository/KIRunSchemaRepository.ts +24 -0
- package/src/engine/runtime/ContextElement.ts +28 -0
- package/src/engine/runtime/FunctionExecutionParameters.ts +74 -0
- package/src/engine/runtime/KIRuntime.ts +653 -0
- package/src/engine/runtime/StatementExecution.ts +61 -0
- package/src/engine/runtime/StatementMessage.ts +30 -0
- package/src/engine/runtime/StatementMessageType.ts +5 -0
- package/src/engine/runtime/expression/Expression.ts +330 -0
- package/src/engine/runtime/expression/ExpressionEvaluator.ts +305 -0
- package/src/engine/runtime/expression/ExpressionToken.ts +15 -0
- package/src/engine/runtime/expression/ExpressionTokenValue.ts +23 -0
- package/src/engine/runtime/expression/Operation.ts +190 -0
- package/src/engine/runtime/expression/exception/ExpressionEvaluationException.ts +15 -0
- package/src/engine/runtime/expression/operators/binary/ArithmeticAdditionOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/ArithmeticDivisionOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/ArithmeticInetgerDivisionOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/ArithmeticModulusOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/ArithmeticMultiplicationOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/ArithmeticSubtractionOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/ArrayOperator.ts +31 -0
- package/src/engine/runtime/expression/operators/binary/BinaryOperator.ts +15 -0
- package/src/engine/runtime/expression/operators/binary/BitwiseAndOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/BitwiseLeftShiftOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/BitwiseOrOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/BitwiseRightShiftOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/BitwiseUnsignedRightShiftOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/BitwiseXorOperator.ts +9 -0
- package/src/engine/runtime/expression/operators/binary/LogicalAndOperator.ts +25 -0
- package/src/engine/runtime/expression/operators/binary/LogicalEqualOperator.ts +13 -0
- package/src/engine/runtime/expression/operators/binary/LogicalGreaterThanEqualOperator.ts +24 -0
- package/src/engine/runtime/expression/operators/binary/LogicalGreaterThanOperator.ts +24 -0
- package/src/engine/runtime/expression/operators/binary/LogicalLessThanEqualOperator.ts +24 -0
- package/src/engine/runtime/expression/operators/binary/LogicalLessThanOperator.ts +24 -0
- package/src/engine/runtime/expression/operators/binary/LogicalNotEqualOperator.ts +13 -0
- package/src/engine/runtime/expression/operators/binary/LogicalOrOperator.ts +25 -0
- package/src/engine/runtime/expression/operators/binary/ObjectOperator.ts +24 -0
- package/src/engine/runtime/expression/operators/unary/ArithmeticUnaryMinusOperator.ts +13 -0
- package/src/engine/runtime/expression/operators/unary/ArithmeticUnaryPlusOperator.ts +13 -0
- package/src/engine/runtime/expression/operators/unary/BitwiseComplementOperator.ts +22 -0
- package/src/engine/runtime/expression/operators/unary/LogicalNotOperator.ts +22 -0
- package/src/engine/runtime/expression/operators/unary/UnaryOperator.ts +15 -0
- package/src/engine/runtime/expression/tokenextractor/LiteralTokenValueExtractor.ts +64 -0
- package/src/engine/runtime/expression/tokenextractor/TokenValueExtractor.ts +136 -0
- package/src/engine/runtime/graph/ExecutionGraph.ts +81 -0
- package/src/engine/runtime/graph/GraphVertex.ts +118 -0
- package/src/engine/runtime/graph/GraphVertexType.ts +4 -0
- package/src/engine/runtime/tokenextractor/ArgumentsTokenValueExtractor.ts +22 -0
- package/src/engine/runtime/tokenextractor/ContextTokenValueExtractor.ts +37 -0
- package/src/engine/runtime/tokenextractor/OutputMapTokenValueExtractor.ts +32 -0
- package/src/engine/util/ArrayUtil.ts +23 -0
- package/src/engine/util/LinkedList.ts +229 -0
- package/src/engine/util/MapUtil.ts +86 -0
- package/src/engine/util/NullCheck.ts +3 -0
- package/src/engine/util/Tuples.ts +43 -0
- package/src/engine/util/primitive/PrimitiveUtil.ts +143 -0
- package/src/engine/util/string/StringBuilder.ts +59 -0
- package/src/engine/util/string/StringFormatter.ts +25 -0
- package/src/engine/util/string/StringUtil.ts +48 -0
- package/src/index.ts +13 -0
- package/tsconfig.json +6 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { Repository } from '../../Repository';
|
|
2
|
+
import { StringUtil } from '../../util/string/StringUtil';
|
|
3
|
+
import { Tuple2 } from '../../util/Tuples';
|
|
4
|
+
import { Schema } from './Schema';
|
|
5
|
+
import { SchemaType } from './type/SchemaType';
|
|
6
|
+
import { SchemaReferenceException } from './validator/exception/SchemaReferenceException';
|
|
7
|
+
import { SchemaValidationException } from './validator/exception/SchemaValidationException';
|
|
8
|
+
|
|
9
|
+
export class SchemaUtil {
|
|
10
|
+
private static readonly UNABLE_TO_RETRIVE_SCHEMA_FROM_REFERENCED_PATH: string =
|
|
11
|
+
'Unable to retrive schema from referenced path';
|
|
12
|
+
|
|
13
|
+
private static readonly CYCLIC_REFERENCE_LIMIT_COUNTER: number = 20;
|
|
14
|
+
|
|
15
|
+
public static getDefaultValue(s: Schema | undefined, sRepository: Repository<Schema>): any {
|
|
16
|
+
if (!s) return undefined;
|
|
17
|
+
|
|
18
|
+
if (s.getConstant()) return s.getConstant();
|
|
19
|
+
|
|
20
|
+
if (s.getDefaultValue()) return s.getDefaultValue();
|
|
21
|
+
|
|
22
|
+
return SchemaUtil.getDefaultValue(
|
|
23
|
+
SchemaUtil.getSchemaFromRef(s, sRepository, s.getRef()),
|
|
24
|
+
sRepository,
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public static getSchemaFromRef(
|
|
29
|
+
schema: Schema,
|
|
30
|
+
sRepository: Repository<Schema> | undefined,
|
|
31
|
+
ref: string | undefined,
|
|
32
|
+
iteration: number = 0,
|
|
33
|
+
): Schema | undefined {
|
|
34
|
+
iteration++;
|
|
35
|
+
|
|
36
|
+
if (iteration == SchemaUtil.CYCLIC_REFERENCE_LIMIT_COUNTER)
|
|
37
|
+
throw new SchemaValidationException(ref ?? '', 'Schema has a cyclic reference');
|
|
38
|
+
|
|
39
|
+
if (!schema || !ref || StringUtil.isNullOrBlank(ref)) return undefined;
|
|
40
|
+
|
|
41
|
+
if (!ref.startsWith('#')) {
|
|
42
|
+
var tuple = SchemaUtil.resolveExternalSchema(schema, sRepository, ref);
|
|
43
|
+
if (tuple) {
|
|
44
|
+
schema = tuple.getT1();
|
|
45
|
+
ref = tuple.getT2();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let parts: string[] = ref.split('/');
|
|
50
|
+
let i: number = 1;
|
|
51
|
+
|
|
52
|
+
schema = SchemaUtil.resolveInternalSchema(schema, sRepository, ref, iteration, parts, i);
|
|
53
|
+
|
|
54
|
+
return schema;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private static resolveInternalSchema(
|
|
58
|
+
inSchema: Schema,
|
|
59
|
+
sRepository: Repository<Schema> | undefined,
|
|
60
|
+
ref: string,
|
|
61
|
+
iteration: number,
|
|
62
|
+
parts: string[],
|
|
63
|
+
i: number,
|
|
64
|
+
): Schema {
|
|
65
|
+
let schema: Schema | undefined = inSchema;
|
|
66
|
+
while (i < parts.length) {
|
|
67
|
+
if (parts[i] === '$defs') {
|
|
68
|
+
i++;
|
|
69
|
+
|
|
70
|
+
if (i >= parts.length || !schema.get$defs())
|
|
71
|
+
throw new SchemaReferenceException(
|
|
72
|
+
ref,
|
|
73
|
+
SchemaUtil.UNABLE_TO_RETRIVE_SCHEMA_FROM_REFERENCED_PATH,
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
schema = schema.get$defs()?.get(parts[i]);
|
|
77
|
+
} else {
|
|
78
|
+
if (
|
|
79
|
+
schema &&
|
|
80
|
+
(!schema.getType()?.contains(SchemaType.OBJECT) || !schema.getProperties())
|
|
81
|
+
)
|
|
82
|
+
throw new SchemaReferenceException(
|
|
83
|
+
ref,
|
|
84
|
+
'Cannot retrievie schema from non Object type schemas',
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
schema = schema.getProperties()?.get(parts[i]);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
i++;
|
|
91
|
+
|
|
92
|
+
if (!schema)
|
|
93
|
+
throw new SchemaReferenceException(
|
|
94
|
+
ref,
|
|
95
|
+
SchemaUtil.UNABLE_TO_RETRIVE_SCHEMA_FROM_REFERENCED_PATH,
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
if (!StringUtil.isNullOrBlank(schema.getRef())) {
|
|
99
|
+
schema = SchemaUtil.getSchemaFromRef(
|
|
100
|
+
schema,
|
|
101
|
+
sRepository,
|
|
102
|
+
schema.getRef(),
|
|
103
|
+
iteration,
|
|
104
|
+
);
|
|
105
|
+
if (!schema)
|
|
106
|
+
throw new SchemaReferenceException(
|
|
107
|
+
ref,
|
|
108
|
+
SchemaUtil.UNABLE_TO_RETRIVE_SCHEMA_FROM_REFERENCED_PATH,
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return schema;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private static resolveExternalSchema(
|
|
116
|
+
inSchem: Schema,
|
|
117
|
+
sRepository: Repository<Schema> | undefined,
|
|
118
|
+
ref: string,
|
|
119
|
+
): Tuple2<Schema, string> | undefined {
|
|
120
|
+
|
|
121
|
+
if (!sRepository) return undefined;
|
|
122
|
+
|
|
123
|
+
let nms = StringUtil.splitAtFirstOccurance(inSchem?.getRef() ?? '', '/');
|
|
124
|
+
if (!nms[0]) return undefined;
|
|
125
|
+
|
|
126
|
+
let nmspnm = StringUtil.splitAtFirstOccurance(nms[0], '.');
|
|
127
|
+
if (!nmspnm[0] || !nmspnm[1]) return undefined;
|
|
128
|
+
|
|
129
|
+
let schema = sRepository.find(nmspnm[0], nmspnm[1]);
|
|
130
|
+
if (!schema) return undefined;
|
|
131
|
+
if (!nms[1] || nms[1] === '') return new Tuple2(schema, ref);
|
|
132
|
+
|
|
133
|
+
ref = '#/' + nms[1];
|
|
134
|
+
|
|
135
|
+
if (!schema)
|
|
136
|
+
throw new SchemaReferenceException(
|
|
137
|
+
ref,
|
|
138
|
+
SchemaUtil.UNABLE_TO_RETRIVE_SCHEMA_FROM_REFERENCED_PATH,
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
return new Tuple2(schema, ref);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
private constructor() {}
|
|
145
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { isNullValue } from '../../../util/NullCheck';
|
|
2
|
+
import { Schema } from '../Schema';
|
|
3
|
+
|
|
4
|
+
export class ArraySchemaType {
|
|
5
|
+
private singleSchema: Schema | undefined;
|
|
6
|
+
private tupleSchema: Schema[] | undefined;
|
|
7
|
+
|
|
8
|
+
public setSingleSchema(schema: Schema): ArraySchemaType {
|
|
9
|
+
this.singleSchema = schema;
|
|
10
|
+
return this;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public setTupleSchema(schemas: Schema[]): ArraySchemaType {
|
|
14
|
+
this.tupleSchema = schemas;
|
|
15
|
+
return this;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public getSingleSchema(): Schema | undefined {
|
|
19
|
+
return this.singleSchema;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public getTupleSchema(): Schema[] | undefined {
|
|
23
|
+
return this.tupleSchema;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public isSingleType(): boolean {
|
|
27
|
+
return !isNullValue(this.singleSchema);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public static of(...schemas: Schema[]): ArraySchemaType {
|
|
31
|
+
if (schemas.length == 1) return new ArraySchemaType().setSingleSchema(schemas[0]);
|
|
32
|
+
|
|
33
|
+
return new ArraySchemaType().setTupleSchema(schemas);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public static from(obj: any): ArraySchemaType | undefined {
|
|
37
|
+
if (!obj) return undefined;
|
|
38
|
+
if (Array.isArray(obj)) ArraySchemaType.of(...Schema.fromListOfSchemas(obj));
|
|
39
|
+
|
|
40
|
+
let x = Schema.from(obj);
|
|
41
|
+
if (!x) return undefined;
|
|
42
|
+
return ArraySchemaType.of(x);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Schema } from '../Schema';
|
|
2
|
+
|
|
3
|
+
export class AdditionalPropertiesType {
|
|
4
|
+
private booleanValue?: boolean;
|
|
5
|
+
private schemaValue?: Schema;
|
|
6
|
+
|
|
7
|
+
public getBooleanValue(): boolean | undefined {
|
|
8
|
+
return this.booleanValue;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
public getSchemaValue(): Schema | undefined {
|
|
12
|
+
return this.schemaValue;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public setBooleanValue(booleanValue: boolean): AdditionalPropertiesType {
|
|
16
|
+
this.booleanValue = booleanValue;
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public setSchemaValue(schemaValue: Schema): AdditionalPropertiesType {
|
|
21
|
+
this.schemaValue = schemaValue;
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public static from(obj: any): AdditionalPropertiesType | undefined {
|
|
26
|
+
if (!obj) return undefined;
|
|
27
|
+
const ad = new AdditionalPropertiesType();
|
|
28
|
+
ad.booleanValue = obj.booleanValue;
|
|
29
|
+
ad.schemaValue = obj.schemaValue;
|
|
30
|
+
return ad;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { SchemaType } from './SchemaType';
|
|
2
|
+
import { Type } from './Type';
|
|
3
|
+
|
|
4
|
+
export class MultipleType extends Type {
|
|
5
|
+
private type: Set<SchemaType>;
|
|
6
|
+
|
|
7
|
+
constructor(type: Set<SchemaType>) {
|
|
8
|
+
super();
|
|
9
|
+
this.type = type;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
public getType(): Set<SchemaType> {
|
|
13
|
+
return this.type;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public setType(type: Set<SchemaType>): MultipleType {
|
|
17
|
+
this.type = type;
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public getAllowedSchemaTypes(): Set<SchemaType> {
|
|
22
|
+
return this.type;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public contains(type: SchemaType): boolean {
|
|
26
|
+
return this.type?.has(type);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { SchemaType } from './SchemaType';
|
|
2
|
+
import { Type } from './Type';
|
|
3
|
+
|
|
4
|
+
export class SingleType extends Type {
|
|
5
|
+
private type: SchemaType;
|
|
6
|
+
|
|
7
|
+
constructor(type: SchemaType) {
|
|
8
|
+
super();
|
|
9
|
+
this.type = type;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
public getType(): SchemaType {
|
|
13
|
+
return this.type;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public getAllowedSchemaTypes(): Set<SchemaType> {
|
|
17
|
+
return new Set([this.type]);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public contains(type: SchemaType): boolean {
|
|
21
|
+
return this.type == type;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { MultipleType } from './MultipleType';
|
|
2
|
+
import { SchemaType } from './SchemaType';
|
|
3
|
+
import { SingleType } from './SingleType';
|
|
4
|
+
import { Type } from './Type';
|
|
5
|
+
|
|
6
|
+
export class TypeUtil {
|
|
7
|
+
public static of(...types: SchemaType[]): Type {
|
|
8
|
+
if (types.length == 1) return new SingleType(types[0]);
|
|
9
|
+
|
|
10
|
+
return new MultipleType(new Set(types));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public static from(types: any): Type | undefined {
|
|
14
|
+
if (typeof types === 'string') {
|
|
15
|
+
return TypeUtil.of(types as SchemaType);
|
|
16
|
+
} else if (Array.isArray(types)) {
|
|
17
|
+
return TypeUtil.of(
|
|
18
|
+
...Array.of(types)
|
|
19
|
+
.map((e) => e as any)
|
|
20
|
+
.map((e) => e as SchemaType),
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { Repository } from '../../../Repository';
|
|
2
|
+
import { Schema } from '../Schema';
|
|
3
|
+
import { SchemaValidationException } from './exception/SchemaValidationException';
|
|
4
|
+
import { SchemaValidator } from './SchemaValidator';
|
|
5
|
+
|
|
6
|
+
export class AnyOfAllOfOneOfValidator {
|
|
7
|
+
public static validate(
|
|
8
|
+
parents: Schema[],
|
|
9
|
+
schema: Schema,
|
|
10
|
+
repository: Repository<Schema> | undefined,
|
|
11
|
+
element: any,
|
|
12
|
+
): any {
|
|
13
|
+
let list: SchemaValidationException[] = [];
|
|
14
|
+
if (schema.getOneOf() && !schema.getOneOf()) {
|
|
15
|
+
AnyOfAllOfOneOfValidator.oneOf(parents, schema, repository, element, list);
|
|
16
|
+
} else if (schema.getAllOf() && !schema.getAllOf()) {
|
|
17
|
+
AnyOfAllOfOneOfValidator.allOf(parents, schema, repository, element, list);
|
|
18
|
+
} else if (schema.getAnyOf() && !schema.getAnyOf()) {
|
|
19
|
+
AnyOfAllOfOneOfValidator.anyOf(parents, schema, repository, element, list);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return element;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
private static anyOf(
|
|
26
|
+
parents: Schema[],
|
|
27
|
+
schema: Schema,
|
|
28
|
+
repository: Repository<Schema> |undefined,
|
|
29
|
+
element: any,
|
|
30
|
+
list: SchemaValidationException[],
|
|
31
|
+
) {
|
|
32
|
+
let flag: boolean = false;
|
|
33
|
+
for (let s of schema.getAnyOf() ?? []) {
|
|
34
|
+
try {
|
|
35
|
+
AnyOfAllOfOneOfValidator.validate(parents, s, repository, element);
|
|
36
|
+
flag = true;
|
|
37
|
+
break;
|
|
38
|
+
} catch (err: any) {
|
|
39
|
+
flag = false;
|
|
40
|
+
list.push(err);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!flag) {
|
|
45
|
+
throw new SchemaValidationException(
|
|
46
|
+
SchemaValidator.path(parents),
|
|
47
|
+
"The value don't satisfy any of the schemas.",
|
|
48
|
+
list,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private static allOf(
|
|
54
|
+
parents: Schema[],
|
|
55
|
+
schema: Schema,
|
|
56
|
+
repository: Repository<Schema> | undefined,
|
|
57
|
+
element: any,
|
|
58
|
+
list: SchemaValidationException[],
|
|
59
|
+
) {
|
|
60
|
+
let flag: number = 0;
|
|
61
|
+
for (let s of schema.getAllOf() ?? []) {
|
|
62
|
+
try {
|
|
63
|
+
AnyOfAllOfOneOfValidator.validate(parents, s, repository, element);
|
|
64
|
+
flag++;
|
|
65
|
+
} catch (err: any) {
|
|
66
|
+
list.push(err);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (flag !== schema.getAllOf()?.length) {
|
|
71
|
+
throw new SchemaValidationException(
|
|
72
|
+
SchemaValidator.path(parents),
|
|
73
|
+
"The value doesn't satisfy some of the schemas.",
|
|
74
|
+
list,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
private static oneOf(
|
|
80
|
+
parents: Schema[],
|
|
81
|
+
schema: Schema,
|
|
82
|
+
repository: Repository<Schema> | undefined,
|
|
83
|
+
element: any,
|
|
84
|
+
list: SchemaValidationException[],
|
|
85
|
+
) {
|
|
86
|
+
let flag: number = 0;
|
|
87
|
+
for (let s of schema.getOneOf() ?? []) {
|
|
88
|
+
try {
|
|
89
|
+
AnyOfAllOfOneOfValidator.validate(parents, s, repository, element);
|
|
90
|
+
flag++;
|
|
91
|
+
} catch (err : any) {
|
|
92
|
+
list.push(err);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (flag != 1) {
|
|
97
|
+
throw new SchemaValidationException(
|
|
98
|
+
SchemaValidator.path(parents),
|
|
99
|
+
flag == 0
|
|
100
|
+
? 'The value does not satisfy any schema'
|
|
101
|
+
: 'The value satisfy more than one schema',
|
|
102
|
+
list,
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private constructor() {}
|
|
108
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { Repository } from '../../../Repository';
|
|
2
|
+
import { isNullValue } from '../../../util/NullCheck';
|
|
3
|
+
import { ArraySchemaType } from '../array/ArraySchemaType';
|
|
4
|
+
import { Schema } from '../Schema';
|
|
5
|
+
import { SchemaValidationException } from './exception/SchemaValidationException';
|
|
6
|
+
import { SchemaValidator } from './SchemaValidator';
|
|
7
|
+
|
|
8
|
+
export class ArrayValidator {
|
|
9
|
+
public static validate(
|
|
10
|
+
parents: Schema[],
|
|
11
|
+
schema: Schema,
|
|
12
|
+
repository: Repository<Schema> | undefined,
|
|
13
|
+
element: any,
|
|
14
|
+
): any {
|
|
15
|
+
if (isNullValue(element))
|
|
16
|
+
throw new SchemaValidationException(
|
|
17
|
+
SchemaValidator.path(parents),
|
|
18
|
+
'Expected an array but found null',
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
if (!Array.isArray(element))
|
|
22
|
+
throw new SchemaValidationException(
|
|
23
|
+
SchemaValidator.path(parents),
|
|
24
|
+
element.toString() + ' is not an Array',
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
let array: any[] = element as any[];
|
|
28
|
+
|
|
29
|
+
ArrayValidator.checkMinMaxItems(parents, schema, array);
|
|
30
|
+
|
|
31
|
+
ArrayValidator.checkItems(parents, schema, repository, array);
|
|
32
|
+
|
|
33
|
+
ArrayValidator.checkUniqueItems(parents, schema, array);
|
|
34
|
+
|
|
35
|
+
ArrayValidator.checkContains(parents, schema, repository, array);
|
|
36
|
+
|
|
37
|
+
return element;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public static checkContains(
|
|
41
|
+
parents: Schema[],
|
|
42
|
+
schema: Schema,
|
|
43
|
+
repository: Repository<Schema> | undefined,
|
|
44
|
+
array: any[],
|
|
45
|
+
) {
|
|
46
|
+
if (!schema.getContains()) return;
|
|
47
|
+
|
|
48
|
+
let flag: boolean = false;
|
|
49
|
+
for (let i = 0; i < array.length; i++) {
|
|
50
|
+
let newParents: Schema[] = !parents ? [] : [...parents];
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
SchemaValidator.validate(newParents, schema.getContains(), repository, array[i]);
|
|
54
|
+
flag = true;
|
|
55
|
+
break;
|
|
56
|
+
} catch (err) {
|
|
57
|
+
flag = false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (!flag) {
|
|
62
|
+
throw new SchemaValidationException(
|
|
63
|
+
SchemaValidator.path(parents),
|
|
64
|
+
'None of the items are of type contains schema',
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public static checkUniqueItems(parents: Schema[], schema: Schema, array: any[]): void {
|
|
70
|
+
if (schema.getUniqueItems() && schema.getUniqueItems()) {
|
|
71
|
+
let set: Set<any> = new Set<any>(array);
|
|
72
|
+
|
|
73
|
+
if (set.size !== array.length)
|
|
74
|
+
throw new SchemaValidationException(
|
|
75
|
+
SchemaValidator.path(parents),
|
|
76
|
+
'Items on the array are not unique',
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
public static checkMinMaxItems(parents: Schema[], schema: Schema, array: any[]): void {
|
|
82
|
+
if (schema.getMinItems() && schema.getMinItems()! > array.length) {
|
|
83
|
+
throw new SchemaValidationException(
|
|
84
|
+
SchemaValidator.path(parents),
|
|
85
|
+
'Array should have minimum of ' + schema.getMinItems() + ' elements',
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (schema.getMaxItems() && schema.getMaxItems()! < array.length) {
|
|
90
|
+
throw new SchemaValidationException(
|
|
91
|
+
SchemaValidator.path(parents),
|
|
92
|
+
'Array can have maximum of ' + schema.getMaxItems() + ' elements',
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
public static checkItems(
|
|
98
|
+
parents: Schema[],
|
|
99
|
+
schema: Schema,
|
|
100
|
+
repository: Repository<Schema> | undefined,
|
|
101
|
+
array: any[],
|
|
102
|
+
) {
|
|
103
|
+
if (!schema.getItems()) return;
|
|
104
|
+
|
|
105
|
+
let type: ArraySchemaType = schema.getItems()!;
|
|
106
|
+
|
|
107
|
+
if (type.getSingleSchema()) {
|
|
108
|
+
for (let i = 0; i < array.length; i++) {
|
|
109
|
+
let newParents: Schema[] = !parents ? [] : [...parents];
|
|
110
|
+
let element: any = SchemaValidator.validate(
|
|
111
|
+
newParents,
|
|
112
|
+
type.getSingleSchema(),
|
|
113
|
+
repository,
|
|
114
|
+
array[i],
|
|
115
|
+
);
|
|
116
|
+
array[i] = element;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (type.getTupleSchema()) {
|
|
121
|
+
if (type.getTupleSchema()!.length !== array.length) {
|
|
122
|
+
throw new SchemaValidationException(
|
|
123
|
+
SchemaValidator.path(parents),
|
|
124
|
+
'Expected an array with only ' +
|
|
125
|
+
type.getTupleSchema()!.length +
|
|
126
|
+
' but found ' +
|
|
127
|
+
array.length,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
for (let i = 0; i < array.length; i++) {
|
|
132
|
+
let newParents: Schema[] = !parents ? [] : [...parents];
|
|
133
|
+
let element: any = SchemaValidator.validate(
|
|
134
|
+
newParents,
|
|
135
|
+
type.getTupleSchema()![i],
|
|
136
|
+
repository,
|
|
137
|
+
array[i],
|
|
138
|
+
);
|
|
139
|
+
array[i] = element;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
private constructor() {}
|
|
145
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { isNullValue } from '../../../util/NullCheck';
|
|
2
|
+
import { Schema } from '../Schema';
|
|
3
|
+
import { SchemaValidationException } from './exception/SchemaValidationException';
|
|
4
|
+
import { SchemaValidator } from './SchemaValidator';
|
|
5
|
+
|
|
6
|
+
export class BooleanValidator {
|
|
7
|
+
public static validate(parents: Schema[], schema: Schema, element: any): any {
|
|
8
|
+
if (isNullValue(element))
|
|
9
|
+
throw new SchemaValidationException(
|
|
10
|
+
SchemaValidator.path(parents),
|
|
11
|
+
'Expected a boolean but found null',
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
if (typeof element !== 'boolean')
|
|
15
|
+
throw new SchemaValidationException(
|
|
16
|
+
SchemaValidator.path(parents),
|
|
17
|
+
element.toString() + ' is not a boolean',
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
return element;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
private constructor() {}
|
|
24
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Schema } from '../Schema';
|
|
2
|
+
import { SchemaValidationException } from './exception/SchemaValidationException';
|
|
3
|
+
import { SchemaValidator } from './SchemaValidator';
|
|
4
|
+
|
|
5
|
+
export class NullValidator {
|
|
6
|
+
public static validate(parents: Schema[], schema: Schema, element: any): any {
|
|
7
|
+
if (element)
|
|
8
|
+
throw new SchemaValidationException(
|
|
9
|
+
SchemaValidator.path(parents),
|
|
10
|
+
'Expected a null but found ' + element,
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
return element;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
private constructor() {}
|
|
17
|
+
}
|