@fincity/kirun-js 2.5.0 → 2.8.2

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 (124) hide show
  1. package/__tests__/engine/function/system/WaitTest.ts +1 -1
  2. package/__tests__/engine/function/system/array/AddFirstTest.ts +2 -6
  3. package/__tests__/engine/function/system/array/IndexOfTest.ts +8 -13
  4. package/__tests__/engine/function/system/array/LastIndexOfTest.ts +9 -28
  5. package/__tests__/engine/function/system/date/AddSubtractTimeTest.ts +93 -0
  6. package/__tests__/engine/function/system/date/DateFunctionRepositoryTest.ts +96 -0
  7. package/__tests__/engine/function/system/date/DifferenceTest.ts +93 -0
  8. package/__tests__/engine/function/system/date/EpochToTimestampTest.ts +133 -0
  9. package/__tests__/engine/function/system/date/FromDateStringTest.ts +51 -0
  10. package/__tests__/engine/function/system/date/FromNowTest.ts +71 -0
  11. package/__tests__/engine/function/system/date/GetCurrentTimeStampTest.ts +19 -27
  12. package/__tests__/engine/function/system/date/GetNamesTest.ts +105 -0
  13. package/__tests__/engine/function/system/date/IsBetweenTest.ts +53 -0
  14. package/__tests__/engine/function/system/date/IsValidISODateTest.ts +42 -54
  15. package/__tests__/engine/function/system/date/LastFirstOfTest.ts +64 -0
  16. package/__tests__/engine/function/system/date/SetTimeZoneTest.ts +48 -0
  17. package/__tests__/engine/function/system/date/StartEndOfTest.ts +53 -0
  18. package/__tests__/engine/function/system/date/TimeAsTest.ts +24 -0
  19. package/__tests__/engine/function/system/date/TimestampToEpochTest.ts +45 -0
  20. package/__tests__/engine/function/system/date/ToDateStringTest.ts +48 -0
  21. package/__tests__/engine/function/system/math/MathFunctionRepositoryTest.ts +2 -2
  22. package/__tests__/engine/function/system/math/MaximumTest.ts +3 -2
  23. package/__tests__/engine/function/system/math/MinimumTest.ts +18 -20
  24. package/__tests__/engine/function/system/math/RandomFloatTest.ts +21 -22
  25. package/__tests__/engine/function/system/math/RandomIntTest.ts +5 -4
  26. package/__tests__/engine/json/schema/covnertor/BooleanConvertorTest.ts +55 -0
  27. package/__tests__/engine/json/schema/covnertor/NullConvertorTest.ts +47 -0
  28. package/__tests__/engine/json/schema/covnertor/NumberConvertorTest.ts +154 -0
  29. package/__tests__/engine/json/schema/covnertor/StringConvertorTest.ts +56 -0
  30. package/__tests__/engine/json/schema/validator/AnyOfAllOfOneOfValidatorTest.ts +9 -7
  31. package/__tests__/engine/json/schema/validator/NotValidatorTest.ts +3 -3
  32. package/__tests__/engine/json/schema/validator/StringValidatorTest.ts +1 -9
  33. package/__tests__/engine/json/schema/validator/TypeValidatorTest.ts +290 -0
  34. package/__tests__/engine/repository/RepositoryFilterTest.ts +4 -1
  35. package/__tests__/engine/runtime/expression/ExpressionEqualityTest.ts +76 -0
  36. package/dist/index.js +1 -1
  37. package/dist/index.js.map +1 -1
  38. package/dist/module.js +1 -1
  39. package/dist/module.js.map +1 -1
  40. package/dist/types.d.ts +27 -20
  41. package/dist/types.d.ts.map +1 -1
  42. package/generator/generateValidationCSV.ts +52 -38
  43. package/generator/validation-js.csv +470 -166
  44. package/package.json +11 -6
  45. package/src/engine/function/AbstractFunction.ts +1 -1
  46. package/src/engine/function/system/Print.ts +19 -5
  47. package/src/engine/function/system/Wait.ts +1 -1
  48. package/src/engine/function/system/array/AbstractArrayFunction.ts +3 -3
  49. package/src/engine/function/system/array/Fill.ts +1 -1
  50. package/src/engine/function/system/array/Frequency.ts +0 -1
  51. package/src/engine/function/system/array/IndexOf.ts +4 -4
  52. package/src/engine/function/system/array/LastIndexOf.ts +3 -3
  53. package/src/engine/function/system/array/Rotate.ts +1 -1
  54. package/src/engine/function/system/array/Shuffle.ts +1 -1
  55. package/src/engine/function/system/array/Sort.ts +1 -1
  56. package/src/engine/function/system/date/AbstractDateFunction.ts +229 -111
  57. package/src/engine/function/system/date/AddSubtractTime.ts +99 -0
  58. package/src/engine/function/system/date/DateFunctionRepository.ts +228 -122
  59. package/src/engine/function/system/date/EpochToTimestamp.ts +75 -0
  60. package/src/engine/function/system/date/FromDateString.ts +44 -0
  61. package/src/engine/function/system/date/FromNow.ts +77 -0
  62. package/src/engine/function/system/date/GetCurrent.ts +20 -0
  63. package/src/engine/function/system/date/GetNames.ts +74 -0
  64. package/src/engine/function/system/date/IsBetween.ts +62 -0
  65. package/src/engine/function/system/date/IsValidISODate.ts +54 -40
  66. package/src/engine/function/system/date/LastFirstOf.ts +72 -0
  67. package/src/engine/function/system/date/SetTimeZone.ts +43 -0
  68. package/src/engine/function/system/date/StartEndOf.ts +44 -0
  69. package/src/engine/function/system/date/TimeAs.ts +64 -0
  70. package/src/engine/function/system/date/TimestampToEpoch.ts +54 -0
  71. package/src/engine/function/system/date/ToDateString.ts +49 -0
  72. package/src/engine/function/system/date/common.ts +9 -0
  73. package/src/engine/function/system/math/MathFunctionRepository.ts +43 -4
  74. package/src/engine/function/system/math/RandomAny.ts +57 -0
  75. package/src/engine/function/system/object/ObjectConvert.ts +99 -0
  76. package/src/engine/function/system/object/ObjectFunctionRepository.ts +2 -1
  77. package/src/engine/function/system/object/ObjectPutValue.ts +0 -2
  78. package/src/engine/function/system/string/AbstractStringFunction.ts +42 -82
  79. package/src/engine/function/system/string/StringFunctionRepository.ts +39 -20
  80. package/src/engine/json/schema/SchemaUtil.ts +1 -1
  81. package/src/engine/json/schema/convertor/BooleanConvertor.ts +76 -0
  82. package/src/engine/json/schema/convertor/NullConvertor.ts +31 -0
  83. package/src/engine/json/schema/convertor/NumberConvertor.ts +117 -0
  84. package/src/engine/json/schema/convertor/StringConvertor.ts +41 -0
  85. package/src/engine/json/schema/convertor/enums/ConversionMode.ts +11 -0
  86. package/src/engine/json/schema/convertor/exception/SchemaConversionException.ts +39 -0
  87. package/src/engine/json/schema/validator/AnyOfAllOfOneOfValidator.ts +96 -36
  88. package/src/engine/json/schema/validator/ArrayValidator.ts +15 -7
  89. package/src/engine/json/schema/validator/ObjectValidator.ts +24 -13
  90. package/src/engine/json/schema/validator/SchemaValidator.ts +74 -22
  91. package/src/engine/json/schema/validator/TypeValidator.ts +136 -23
  92. package/src/engine/repository/KIRunSchemaRepository.ts +57 -3
  93. package/src/engine/util/json/ConvertorUtil.ts +51 -0
  94. package/src/engine/util/json/ValidatorUtil.ts +29 -0
  95. package/src/engine/util/string/StringUtil.ts +12 -0
  96. package/__tests__/engine/function/system/date/AddTimeTest.ts +0 -115
  97. package/__tests__/engine/function/system/date/DateToEpochTest.ts +0 -74
  98. package/__tests__/engine/function/system/date/DifferenceOfTimestampsTest.ts +0 -53
  99. package/__tests__/engine/function/system/date/EpochToDateTest.ts +0 -105
  100. package/__tests__/engine/function/system/date/GetDateTest.ts +0 -85
  101. package/__tests__/engine/function/system/date/GetDayTest.ts +0 -85
  102. package/__tests__/engine/function/system/date/GetFullYearTest.ts +0 -85
  103. package/__tests__/engine/function/system/date/GetHoursTest.ts +0 -85
  104. package/__tests__/engine/function/system/date/GetMilliSecondsTest.ts +0 -85
  105. package/__tests__/engine/function/system/date/GetMinutesTest.ts +0 -85
  106. package/__tests__/engine/function/system/date/GetMonthTest.ts +0 -85
  107. package/__tests__/engine/function/system/date/GetSecondsTest.ts +0 -85
  108. package/__tests__/engine/function/system/date/GetTimeAsArrayTest.ts +0 -59
  109. package/__tests__/engine/function/system/date/GetTimeAsObjectTest.ts +0 -83
  110. package/__tests__/engine/function/system/date/GetTimeTest.ts +0 -86
  111. package/__tests__/engine/function/system/date/IsLeapYearTest.ts +0 -85
  112. package/__tests__/engine/function/system/date/MaximumTimestampTest.ts +0 -55
  113. package/__tests__/engine/function/system/date/MinimumTimestampTest.ts +0 -54
  114. package/__tests__/engine/function/system/date/SubtractTimeTest.ts +0 -95
  115. package/src/engine/function/system/date/DateToEpoch.ts +0 -39
  116. package/src/engine/function/system/date/DifferenceOfTimestamps.ts +0 -45
  117. package/src/engine/function/system/date/EpochToDate.ts +0 -76
  118. package/src/engine/function/system/date/GetCurrentTimeStamp.ts +0 -36
  119. package/src/engine/function/system/date/GetTimeAsArray.ts +0 -48
  120. package/src/engine/function/system/date/GetTimeAsObject.ts +0 -66
  121. package/src/engine/function/system/date/MaximumTimestamp.ts +0 -73
  122. package/src/engine/function/system/date/MinimumTimestamp.ts +0 -74
  123. package/src/engine/function/system/math/RandomFloat.ts +0 -56
  124. package/src/engine/function/system/math/RandomInt.ts +0 -56
@@ -0,0 +1,31 @@
1
+ import { Schema } from '../Schema';
2
+ import { ConversionMode } from './enums/ConversionMode';
3
+ import { isNullValue } from '../../../util/NullCheck';
4
+ import { ConvertorUtil } from '../../../util/json/ConvertorUtil';
5
+
6
+ export class NullConvertor {
7
+ public static convert(
8
+ parents: Schema[],
9
+ schema: Schema,
10
+ mode: ConversionMode,
11
+ element: any,
12
+ ): null {
13
+ if (isNullValue(element)) {
14
+ return element;
15
+ }
16
+
17
+ if ('string' === typeof element && element.toLowerCase() === 'null') {
18
+ return null;
19
+ }
20
+
21
+ return ConvertorUtil.handleUnConvertibleValueWithDefault(
22
+ parents,
23
+ mode,
24
+ element,
25
+ null,
26
+ 'Unable to convert to null',
27
+ );
28
+ }
29
+
30
+ private constructor() {}
31
+ }
@@ -0,0 +1,117 @@
1
+ import { Schema } from '../Schema';
2
+ import { SchemaType } from '../type/SchemaType';
3
+ import { ConversionMode } from './enums/ConversionMode';
4
+ import { isNullValue } from '../../../util/NullCheck';
5
+ import { ConvertorUtil } from '../../../util/json/ConvertorUtil';
6
+
7
+ export class NumberConvertor {
8
+ public static convert(
9
+ parents: Schema[],
10
+ type: SchemaType,
11
+ schema: Schema,
12
+ mode: ConversionMode,
13
+ element: any,
14
+ ): number | null {
15
+ if (isNullValue(element)) {
16
+ return ConvertorUtil.handleUnConvertibleValueWithDefault(
17
+ parents,
18
+ mode,
19
+ element,
20
+ this.getDefault(schema),
21
+ 'Expected a number but found null',
22
+ );
23
+ }
24
+
25
+ if (typeof element === 'object' || typeof element === 'boolean' || Array.isArray(element)) {
26
+ return ConvertorUtil.handleUnConvertibleValueWithDefault(
27
+ parents,
28
+ mode,
29
+ element,
30
+ this.getDefault(schema),
31
+ element + ' is not a ' + type,
32
+ );
33
+ }
34
+
35
+ if (typeof element === 'string') {
36
+ element = Number(element);
37
+ if (isNaN(element)) {
38
+ return ConvertorUtil.handleUnConvertibleValueWithDefault(
39
+ parents,
40
+ mode,
41
+ element,
42
+ this.getDefault(schema),
43
+ element + ' is not a ' + type,
44
+ );
45
+ }
46
+ }
47
+
48
+ const number: number | null = this.extractNumber(type, element, mode);
49
+
50
+ if (number === null) {
51
+ return ConvertorUtil.handleUnConvertibleValueWithDefault(
52
+ parents,
53
+ mode,
54
+ element,
55
+ this.getDefault(schema),
56
+ element + ' is not a ' + type,
57
+ );
58
+ }
59
+
60
+ return number;
61
+ }
62
+
63
+ private static extractNumber(
64
+ schemaType: SchemaType,
65
+ element: number,
66
+ mode: ConversionMode,
67
+ ): number | null {
68
+ if (typeof element !== 'number') {
69
+ return null;
70
+ }
71
+
72
+ switch (schemaType) {
73
+ case SchemaType.INTEGER:
74
+ return this.isInteger(element, mode) ? Math.floor(element) : null;
75
+ case SchemaType.LONG:
76
+ return this.isLong(element, mode) ? Math.floor(element) : null;
77
+ case SchemaType.DOUBLE:
78
+ return element;
79
+ case SchemaType.FLOAT:
80
+ return this.isFloat(element, mode) ? element : null;
81
+ default:
82
+ return null;
83
+ }
84
+ }
85
+
86
+ private static isInteger(value: number, mode: ConversionMode): boolean {
87
+ if (mode !== ConversionMode.STRICT) {
88
+ return typeof value === 'number';
89
+ }
90
+ return Number.isInteger(value);
91
+ }
92
+
93
+ private static isLong(value: number, mode: ConversionMode): boolean {
94
+ if (mode !== ConversionMode.STRICT) {
95
+ return typeof value === 'number';
96
+ }
97
+ return (
98
+ Number.isInteger(value) &&
99
+ value >= Number.MIN_SAFE_INTEGER &&
100
+ value <= Number.MAX_SAFE_INTEGER
101
+ );
102
+ }
103
+
104
+ private static isFloat(value: number, mode: ConversionMode): boolean {
105
+ if (mode !== ConversionMode.STRICT) {
106
+ return typeof value === 'number';
107
+ }
108
+ return value >= -Number.MAX_VALUE && value <= Number.MAX_VALUE;
109
+ }
110
+
111
+ private static getDefault(schema: Schema): number | null {
112
+ if (typeof schema.getDefaultValue() === 'number') {
113
+ return Number(schema.getDefaultValue);
114
+ }
115
+ return null;
116
+ }
117
+ }
@@ -0,0 +1,41 @@
1
+ import { Schema } from '../Schema';
2
+ import { ConversionMode } from './enums/ConversionMode';
3
+ import { isNullValue } from '../../../util/NullCheck';
4
+ import { ConvertorUtil } from '../../../util/json/ConvertorUtil';
5
+
6
+ export class StringConvertor {
7
+ public static convert(
8
+ parents: Schema[],
9
+ schema: Schema,
10
+ mode: ConversionMode,
11
+ element: any,
12
+ ): string | null {
13
+ if (isNullValue(element)) {
14
+ return ConvertorUtil.handleUnConvertibleValueWithDefault(
15
+ parents,
16
+ mode,
17
+ element,
18
+ this.getDefault(schema),
19
+ 'Expected a string but found null',
20
+ );
21
+ }
22
+
23
+ const value =
24
+ element ?? (typeof element === 'object' ? JSON.stringify(element) : String(element));
25
+
26
+ return this.getConvertedString(value, mode);
27
+ }
28
+
29
+ private static getConvertedString(value: string, mode: ConversionMode): string {
30
+ if (mode === ConversionMode.STRICT) {
31
+ return value.toString();
32
+ }
33
+ return value.trim();
34
+ }
35
+
36
+ private static getDefault(schema: Schema): any {
37
+ return schema.getDefaultValue() ?? null;
38
+ }
39
+
40
+ private constructor() {}
41
+ }
@@ -0,0 +1,11 @@
1
+ export enum ConversionMode {
2
+ STRICT = 'STRICT',
3
+ LENIENT = 'LENIENT',
4
+ USE_DEFAULT = 'USE_DEFAULT',
5
+ SKIP = 'SKIP',
6
+ }
7
+
8
+ export const genericValueOf = (mode: string): ConversionMode =>
9
+ ConversionMode[mode.toUpperCase() as keyof typeof ConversionMode];
10
+
11
+ export const getConversionModes = (): string[] => Object.values(ConversionMode);
@@ -0,0 +1,39 @@
1
+ import { ConversionMode } from '../enums/ConversionMode';
2
+
3
+ export class SchemaConversionException extends Error {
4
+ private readonly schemaPath: string;
5
+ private readonly source?: string | null;
6
+ private readonly mode?: ConversionMode | null;
7
+ cause?: Error;
8
+
9
+ constructor(
10
+ schemaPath: string,
11
+ source: string,
12
+ message: string,
13
+ mode?: ConversionMode,
14
+ sce: SchemaConversionException[] = [],
15
+ err?: Error,
16
+ ) {
17
+ super(message + (sce ? sce.map((e) => e.message).reduce((a, c) => a + '\n' + c, '') : ''));
18
+ this.schemaPath = schemaPath;
19
+ this.source = source ?? null;
20
+ this.mode = mode ?? null;
21
+ this.cause = err;
22
+ }
23
+
24
+ public getSchemaPath(): string {
25
+ return this.schemaPath;
26
+ }
27
+
28
+ public getSource(): string | null {
29
+ return this.source ?? null;
30
+ }
31
+
32
+ public getMode(): ConversionMode | null {
33
+ return this.mode ?? null;
34
+ }
35
+
36
+ public getCause(): Error | undefined {
37
+ return this.cause;
38
+ }
39
+ }
@@ -2,37 +2,73 @@ import { Repository } from '../../../Repository';
2
2
  import { Schema } from '../Schema';
3
3
  import { SchemaValidationException } from './exception/SchemaValidationException';
4
4
  import { SchemaValidator } from './SchemaValidator';
5
+ import { ConversionMode } from '../convertor/enums/ConversionMode';
5
6
 
6
7
  export class AnyOfAllOfOneOfValidator {
7
- public static validate(
8
+ public static async validate(
8
9
  parents: Schema[],
9
10
  schema: Schema,
10
11
  repository: Repository<Schema> | undefined,
11
12
  element: any,
12
- ): any {
13
+ convert?: boolean,
14
+ mode?: ConversionMode,
15
+ ): Promise<any> {
13
16
  let list: SchemaValidationException[] = [];
14
17
  if (schema.getOneOf() && !schema.getOneOf()) {
15
- AnyOfAllOfOneOfValidator.oneOf(parents, schema, repository, element, list);
18
+ return await AnyOfAllOfOneOfValidator.oneOf(
19
+ parents,
20
+ schema,
21
+ repository,
22
+ element,
23
+ list,
24
+ convert,
25
+ mode,
26
+ );
16
27
  } else if (schema.getAllOf() && !schema.getAllOf()) {
17
- AnyOfAllOfOneOfValidator.allOf(parents, schema, repository, element, list);
28
+ return await AnyOfAllOfOneOfValidator.allOf(
29
+ parents,
30
+ schema,
31
+ repository,
32
+ element,
33
+ list,
34
+ convert,
35
+ mode,
36
+ );
18
37
  } else if (schema.getAnyOf() && !schema.getAnyOf()) {
19
- AnyOfAllOfOneOfValidator.anyOf(parents, schema, repository, element, list);
38
+ return await AnyOfAllOfOneOfValidator.anyOf(
39
+ parents,
40
+ schema,
41
+ repository,
42
+ element,
43
+ list,
44
+ convert,
45
+ mode,
46
+ );
20
47
  }
21
48
 
22
49
  return element;
23
50
  }
24
51
 
25
- private static anyOf(
52
+ private static async anyOf(
26
53
  parents: Schema[],
27
54
  schema: Schema,
28
- repository: Repository<Schema> |undefined,
55
+ repository: Repository<Schema> | undefined,
29
56
  element: any,
30
57
  list: SchemaValidationException[],
31
- ) {
58
+ convert?: boolean,
59
+ mode?: ConversionMode,
60
+ ): Promise<any> {
32
61
  let flag: boolean = false;
33
62
  for (let s of schema.getAnyOf() ?? []) {
34
63
  try {
35
- AnyOfAllOfOneOfValidator.validate(parents, s, repository, element);
64
+ await AnyOfAllOfOneOfValidator.validate(
65
+ parents,
66
+ s,
67
+ repository,
68
+ element,
69
+ convert,
70
+ mode,
71
+ );
36
72
  flag = true;
37
73
  break;
38
74
  } catch (err: any) {
@@ -41,67 +77,91 @@ export class AnyOfAllOfOneOfValidator {
41
77
  }
42
78
  }
43
79
 
44
- if (!flag) {
45
- throw new SchemaValidationException(
46
- SchemaValidator.path(parents),
47
- "The value don't satisfy any of the schemas.",
48
- list,
49
- );
80
+ if (flag) {
81
+ return element;
50
82
  }
83
+
84
+ throw new SchemaValidationException(
85
+ SchemaValidator.path(parents),
86
+ "The value don't satisfy any of the schemas.",
87
+ list,
88
+ );
51
89
  }
52
90
 
53
- private static allOf(
91
+ private static async allOf(
54
92
  parents: Schema[],
55
93
  schema: Schema,
56
94
  repository: Repository<Schema> | undefined,
57
95
  element: any,
58
96
  list: SchemaValidationException[],
59
- ) {
97
+ convert?: boolean,
98
+ mode?: ConversionMode,
99
+ ): Promise<any> {
60
100
  let flag: number = 0;
61
101
  for (let s of schema.getAllOf() ?? []) {
62
102
  try {
63
- AnyOfAllOfOneOfValidator.validate(parents, s, repository, element);
103
+ await AnyOfAllOfOneOfValidator.validate(
104
+ parents,
105
+ s,
106
+ repository,
107
+ element,
108
+ convert,
109
+ mode,
110
+ );
64
111
  flag++;
65
112
  } catch (err: any) {
66
113
  list.push(err);
67
114
  }
68
115
  }
69
116
 
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
- );
117
+ if (flag === schema.getAllOf()?.length) {
118
+ return element;
76
119
  }
120
+
121
+ throw new SchemaValidationException(
122
+ SchemaValidator.path(parents),
123
+ "The value doesn't satisfy some of the schemas.",
124
+ list,
125
+ );
77
126
  }
78
127
 
79
- private static oneOf(
128
+ private static async oneOf(
80
129
  parents: Schema[],
81
130
  schema: Schema,
82
131
  repository: Repository<Schema> | undefined,
83
132
  element: any,
84
133
  list: SchemaValidationException[],
85
- ) {
134
+ convert?: boolean,
135
+ mode?: ConversionMode,
136
+ ): Promise<any> {
86
137
  let flag: number = 0;
87
138
  for (let s of schema.getOneOf() ?? []) {
88
139
  try {
89
- AnyOfAllOfOneOfValidator.validate(parents, s, repository, element);
140
+ await AnyOfAllOfOneOfValidator.validate(
141
+ parents,
142
+ s,
143
+ repository,
144
+ element,
145
+ convert,
146
+ mode,
147
+ );
90
148
  flag++;
91
- } catch (err : any) {
149
+ } catch (err: any) {
92
150
  list.push(err);
93
151
  }
94
152
  }
95
153
 
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
- );
154
+ if (flag === 1) {
155
+ return element;
104
156
  }
157
+
158
+ throw new SchemaValidationException(
159
+ SchemaValidator.path(parents),
160
+ flag == 0
161
+ ? 'The value does not satisfy any schema'
162
+ : 'The value satisfy more than one schema',
163
+ list,
164
+ );
105
165
  }
106
166
 
107
167
  private constructor() {}
@@ -4,6 +4,7 @@ import { ArraySchemaType } from '../array/ArraySchemaType';
4
4
  import { Schema } from '../Schema';
5
5
  import { SchemaValidationException } from './exception/SchemaValidationException';
6
6
  import { SchemaValidator } from './SchemaValidator';
7
+ import { ConversionMode } from '../convertor/enums/ConversionMode';
7
8
 
8
9
  export class ArrayValidator {
9
10
  public static async validate(
@@ -11,6 +12,8 @@ export class ArrayValidator {
11
12
  schema: Schema,
12
13
  repository: Repository<Schema> | undefined,
13
14
  element: any,
15
+ convert?: boolean,
16
+ mode?: ConversionMode,
14
17
  ): Promise<any> {
15
18
  if (isNullValue(element))
16
19
  throw new SchemaValidationException(
@@ -28,7 +31,7 @@ export class ArrayValidator {
28
31
 
29
32
  ArrayValidator.checkMinMaxItems(parents, schema, array);
30
33
 
31
- await ArrayValidator.checkItems(parents, schema, repository, array);
34
+ await ArrayValidator.checkItems(parents, schema, repository, array, convert, mode);
32
35
 
33
36
  ArrayValidator.checkUniqueItems(parents, schema, array);
34
37
 
@@ -137,6 +140,8 @@ export class ArrayValidator {
137
140
  schema: Schema,
138
141
  repository: Repository<Schema> | undefined,
139
142
  array: any[],
143
+ convert?: boolean,
144
+ mode?: ConversionMode,
140
145
  ) {
141
146
  if (!schema.getItems()) return;
142
147
 
@@ -145,13 +150,14 @@ export class ArrayValidator {
145
150
  if (type.getSingleSchema()) {
146
151
  for (let i = 0; i < array.length; i++) {
147
152
  let newParents: Schema[] = !parents ? [] : [...parents];
148
- let element: any = await SchemaValidator.validate(
153
+ array[i] = await SchemaValidator.validate(
149
154
  newParents,
150
155
  type.getSingleSchema(),
151
156
  repository,
152
157
  array[i],
158
+ convert,
159
+ mode,
153
160
  );
154
- array[i] = element;
155
161
  }
156
162
  }
157
163
 
@@ -180,16 +186,19 @@ export class ArrayValidator {
180
186
  repository: Repository<Schema> | undefined,
181
187
  array: any[],
182
188
  type: ArraySchemaType,
189
+ convert?: boolean,
190
+ mode?: ConversionMode,
183
191
  ) {
184
192
  for (let i = 0; i < type.getTupleSchema()?.length!; i++) {
185
193
  let newParents: Schema[] = !parents ? [] : [...parents];
186
- let element: any = await SchemaValidator.validate(
194
+ array[i] = await SchemaValidator.validate(
187
195
  newParents,
188
196
  type.getTupleSchema()![i],
189
197
  repository,
190
198
  array[i],
199
+ convert,
200
+ mode,
191
201
  );
192
- array[i] = element;
193
202
  }
194
203
  }
195
204
 
@@ -246,13 +255,12 @@ export class ArrayValidator {
246
255
  ) {
247
256
  for (let i = type.getTupleSchema()?.length!; i < array.length; i++) {
248
257
  let newParents: Schema[] = !parents ? [] : [...parents];
249
- let element: any = await SchemaValidator.validate(
258
+ array[i] = await SchemaValidator.validate(
250
259
  newParents,
251
260
  schemaType!,
252
261
  repository,
253
262
  array[i],
254
263
  );
255
- array[i] = element;
256
264
  }
257
265
  return;
258
266
  }
@@ -4,6 +4,7 @@ import { AdditionalType, Schema } from '../Schema';
4
4
  import { SchemaUtil } from '../SchemaUtil';
5
5
  import { SchemaValidationException } from './exception/SchemaValidationException';
6
6
  import { SchemaValidator } from './SchemaValidator';
7
+ import { ConversionMode } from '../convertor/enums/ConversionMode';
7
8
 
8
9
  export class ObjectValidator {
9
10
  public static async validate(
@@ -11,7 +12,9 @@ export class ObjectValidator {
11
12
  schema: Schema,
12
13
  repository: Repository<Schema> | undefined,
13
14
  element: any,
14
- ) {
15
+ convert?: boolean,
16
+ mode?: ConversionMode,
17
+ ): Promise<any> {
15
18
  if (isNullValue(element))
16
19
  throw new SchemaValidationException(
17
20
  SchemaValidator.path(parents),
@@ -38,7 +41,15 @@ export class ObjectValidator {
38
41
  }
39
42
 
40
43
  if (schema.getProperties()) {
41
- await ObjectValidator.checkProperties(parents, schema, repository, jsonObject, keys);
44
+ await ObjectValidator.checkProperties(
45
+ parents,
46
+ schema,
47
+ repository,
48
+ jsonObject,
49
+ keys,
50
+ convert,
51
+ mode,
52
+ );
42
53
  }
43
54
 
44
55
  if (schema.getPatternProperties()) {
@@ -52,7 +63,7 @@ export class ObjectValidator {
52
63
  }
53
64
 
54
65
  if (schema.getAdditionalProperties()) {
55
- await ObjectValidator.checkAddtionalProperties(
66
+ await ObjectValidator.checkAdditionalProperties(
56
67
  parents,
57
68
  schema,
58
69
  repository,
@@ -60,6 +71,8 @@ export class ObjectValidator {
60
71
  keys,
61
72
  );
62
73
  }
74
+
75
+ return jsonObject;
63
76
  }
64
77
 
65
78
  private static async checkPropertyNameSchema(
@@ -96,7 +109,7 @@ export class ObjectValidator {
96
109
  }
97
110
  }
98
111
 
99
- private static async checkAddtionalProperties(
112
+ private static async checkAdditionalProperties(
100
113
  parents: Schema[],
101
114
  schema: Schema,
102
115
  repository: Repository<Schema> | undefined,
@@ -108,13 +121,12 @@ export class ObjectValidator {
108
121
  for (let key of Array.from(keys.values())) {
109
122
  let newParents: Schema[] = !parents ? [] : [...parents];
110
123
 
111
- let element: any = await SchemaValidator.validate(
124
+ jsonObject[key] = await SchemaValidator.validate(
112
125
  newParents,
113
126
  apt.getSchemaValue(),
114
127
  repository,
115
128
  jsonObject[key],
116
129
  );
117
- jsonObject[key] = element;
118
130
  }
119
131
  } else {
120
132
  if (apt.getBooleanValue() === false && keys.size) {
@@ -137,20 +149,17 @@ export class ObjectValidator {
137
149
  for (const keyPattern of Array.from(schema.getPatternProperties()!.keys()))
138
150
  compiledPatterns.set(keyPattern, new RegExp(keyPattern));
139
151
 
140
- let goodKeys: string[] = [];
141
-
142
152
  for (const key of Array.from(keys.values())) {
143
153
  const newParents: Schema[] = !parents ? [] : [...parents];
144
154
 
145
155
  for (const e of Array.from(compiledPatterns.entries())) {
146
156
  if (e[1].test(key)) {
147
- const element: any = await SchemaValidator.validate(
157
+ jsonObject[key] = await SchemaValidator.validate(
148
158
  newParents,
149
159
  schema.getPatternProperties()!.get(e[0]),
150
160
  repository,
151
161
  jsonObject[key],
152
162
  );
153
- jsonObject[key] = element;
154
163
  keys.delete(key);
155
164
  break;
156
165
  }
@@ -164,6 +173,8 @@ export class ObjectValidator {
164
173
  repository: Repository<Schema> | undefined,
165
174
  jsonObject: any,
166
175
  keys: Set<string>,
176
+ convert?: boolean,
177
+ mode?: ConversionMode,
167
178
  ) {
168
179
  for (const each of Array.from(schema.getProperties()!)) {
169
180
  let value: any = jsonObject[each[0]];
@@ -174,14 +185,14 @@ export class ObjectValidator {
174
185
  }
175
186
 
176
187
  let newParents: Schema[] = !parents ? [] : [...parents];
177
- let element: any = await SchemaValidator.validate(
188
+ jsonObject[each[0]] = await SchemaValidator.validate(
178
189
  newParents,
179
190
  each[1],
180
191
  repository,
181
192
  value,
193
+ convert,
194
+ mode,
182
195
  );
183
-
184
- jsonObject[each[0]] = element;
185
196
  keys.delete(each[0]);
186
197
  }
187
198
  }