@typia/utils 12.0.0-dev.20260309 → 12.0.0-dev.20260311

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 (103) hide show
  1. package/lib/converters/LlmSchemaConverter.d.ts +0 -1
  2. package/lib/converters/LlmSchemaConverter.js +4 -31
  3. package/lib/converters/LlmSchemaConverter.js.map +1 -1
  4. package/lib/converters/LlmSchemaConverter.mjs +2 -32
  5. package/lib/converters/LlmSchemaConverter.mjs.map +1 -1
  6. package/lib/http/HttpLlm.js +4 -5
  7. package/lib/http/HttpLlm.js.map +1 -1
  8. package/lib/http/HttpLlm.mjs +0 -1
  9. package/lib/http/HttpLlm.mjs.map +1 -1
  10. package/lib/http/internal/HttpLlmApplicationComposer.js +3 -4
  11. package/lib/http/internal/HttpLlmApplicationComposer.js.map +1 -1
  12. package/lib/http/internal/HttpLlmApplicationComposer.mjs +5 -2
  13. package/lib/http/internal/HttpLlmApplicationComposer.mjs.map +1 -1
  14. package/lib/index.mjs +9 -9
  15. package/lib/utils/LlmJson.mjs +9 -2
  16. package/lib/utils/LlmJson.mjs.map +1 -1
  17. package/lib/utils/internal/stringifyValidationFailure.js +17 -15
  18. package/lib/utils/internal/stringifyValidationFailure.js.map +1 -1
  19. package/lib/utils/internal/stringifyValidationFailure.mjs +17 -15
  20. package/lib/utils/internal/stringifyValidationFailure.mjs.map +1 -1
  21. package/lib/validators/internal/OpenApiOneOfValidator.mjs +5 -1
  22. package/lib/validators/internal/OpenApiOneOfValidator.mjs.map +1 -1
  23. package/package.json +2 -2
  24. package/src/converters/LlmSchemaConverter.ts +617 -647
  25. package/src/converters/OpenApiConverter.ts +285 -285
  26. package/src/converters/index.ts +5 -5
  27. package/src/converters/internal/LlmDescriptionInverter.ts +178 -178
  28. package/src/converters/internal/LlmParametersComposer.ts +52 -52
  29. package/src/converters/internal/OpenApiConstraintShifter.ts +154 -154
  30. package/src/converters/internal/OpenApiExclusiveEmender.ts +46 -46
  31. package/src/converters/internal/OpenApiV3Downgrader.ts +355 -355
  32. package/src/converters/internal/OpenApiV3Upgrader.ts +470 -470
  33. package/src/converters/internal/OpenApiV3_1Upgrader.ts +685 -685
  34. package/src/converters/internal/SwaggerV2Downgrader.ts +424 -424
  35. package/src/converters/internal/SwaggerV2Upgrader.ts +523 -523
  36. package/src/http/HttpError.ts +107 -107
  37. package/src/http/HttpLlm.ts +166 -167
  38. package/src/http/HttpMigration.ts +92 -92
  39. package/src/http/index.ts +3 -3
  40. package/src/http/internal/HttpLlmApplicationComposer.ts +360 -361
  41. package/src/http/internal/HttpLlmFunctionFetcher.ts +37 -37
  42. package/src/http/internal/HttpMigrateApplicationComposer.ts +56 -56
  43. package/src/http/internal/HttpMigrateRouteAccessor.ts +135 -135
  44. package/src/http/internal/HttpMigrateRouteComposer.ts +505 -505
  45. package/src/http/internal/HttpMigrateRouteFetcher.ts +203 -203
  46. package/src/index.ts +4 -4
  47. package/src/utils/ArrayUtil.ts +42 -42
  48. package/src/utils/LlmJson.ts +141 -141
  49. package/src/utils/MapUtil.ts +15 -15
  50. package/src/utils/NamingConvention.ts +205 -205
  51. package/src/utils/Singleton.ts +17 -17
  52. package/src/utils/StringUtil.ts +14 -14
  53. package/src/utils/dedent.ts +57 -57
  54. package/src/utils/index.ts +8 -8
  55. package/src/utils/internal/EndpointUtil.ts +44 -44
  56. package/src/utils/internal/JsonDescriptor.ts +70 -70
  57. package/src/utils/internal/OpenApiTypeCheckerBase.ts +822 -822
  58. package/src/utils/internal/coerceLlmArguments.ts +314 -314
  59. package/src/utils/internal/parseLenientJson.ts +894 -894
  60. package/src/utils/internal/stringifyValidationFailure.ts +415 -411
  61. package/src/validators/LlmTypeChecker.ts +402 -402
  62. package/src/validators/OpenApiTypeChecker.ts +297 -297
  63. package/src/validators/OpenApiV3TypeChecker.ts +70 -70
  64. package/src/validators/OpenApiV3_1TypeChecker.ts +86 -86
  65. package/src/validators/OpenApiValidator.ts +94 -94
  66. package/src/validators/SwaggerV2TypeChecker.ts +71 -71
  67. package/src/validators/functional/_isBigintString.ts +8 -8
  68. package/src/validators/functional/_isFormatByte.ts +7 -7
  69. package/src/validators/functional/_isFormatDate.ts +3 -3
  70. package/src/validators/functional/_isFormatDateTime.ts +4 -4
  71. package/src/validators/functional/_isFormatDuration.ts +4 -4
  72. package/src/validators/functional/_isFormatEmail.ts +4 -4
  73. package/src/validators/functional/_isFormatHostname.ts +4 -4
  74. package/src/validators/functional/_isFormatIdnEmail.ts +4 -4
  75. package/src/validators/functional/_isFormatIdnHostname.ts +4 -4
  76. package/src/validators/functional/_isFormatIpv4.ts +4 -4
  77. package/src/validators/functional/_isFormatIpv6.ts +4 -4
  78. package/src/validators/functional/_isFormatIri.ts +3 -3
  79. package/src/validators/functional/_isFormatIriReference.ts +4 -4
  80. package/src/validators/functional/_isFormatJsonPointer.ts +3 -3
  81. package/src/validators/functional/_isFormatPassword.ts +1 -1
  82. package/src/validators/functional/_isFormatRegex.ts +8 -8
  83. package/src/validators/functional/_isFormatRelativeJsonPointer.ts +4 -4
  84. package/src/validators/functional/_isFormatTime.ts +4 -4
  85. package/src/validators/functional/_isFormatUri.ts +6 -6
  86. package/src/validators/functional/_isFormatUriReference.ts +5 -5
  87. package/src/validators/functional/_isFormatUriTemplate.ts +4 -4
  88. package/src/validators/functional/_isFormatUrl.ts +4 -4
  89. package/src/validators/functional/_isFormatUuid.ts +3 -3
  90. package/src/validators/functional/_isUniqueItems.ts +159 -159
  91. package/src/validators/index.ts +14 -14
  92. package/src/validators/internal/IOpenApiValidatorContext.ts +17 -17
  93. package/src/validators/internal/OpenApiArrayValidator.ts +49 -49
  94. package/src/validators/internal/OpenApiBooleanValidator.ts +11 -11
  95. package/src/validators/internal/OpenApiConstantValidator.ts +11 -11
  96. package/src/validators/internal/OpenApiIntegerValidator.ts +49 -49
  97. package/src/validators/internal/OpenApiNumberValidator.ts +48 -48
  98. package/src/validators/internal/OpenApiObjectValidator.ts +83 -83
  99. package/src/validators/internal/OpenApiOneOfValidator.ts +309 -309
  100. package/src/validators/internal/OpenApiSchemaNamingRule.ts +124 -124
  101. package/src/validators/internal/OpenApiStationValidator.ts +115 -115
  102. package/src/validators/internal/OpenApiStringValidator.ts +88 -88
  103. package/src/validators/internal/OpenApiTupleValidator.ts +55 -55
@@ -1,309 +1,309 @@
1
- import { OpenApi } from "@typia/interface";
2
-
3
- import { MapUtil } from "../../utils";
4
- import { OpenApiTypeChecker } from "../OpenApiTypeChecker";
5
- import { IOpenApiValidatorContext } from "./IOpenApiValidatorContext";
6
- import { OpenApiStationValidator } from "./OpenApiStationValidator";
7
-
8
- export namespace OpenApiOneOfValidator {
9
- export const validate = (
10
- ctx: IOpenApiValidatorContext<OpenApi.IJsonSchema.IOneOf>,
11
- ): boolean => {
12
- const discriminator: IDiscriminator = getDiscriminator(ctx);
13
- for (const item of discriminator.branches)
14
- if (item.predicator(ctx.value))
15
- return OpenApiStationValidator.validate({
16
- ...ctx,
17
- schema: item.schema,
18
- });
19
- if (discriminator.branches.length !== 0)
20
- return validate({
21
- ...ctx,
22
- schema: {
23
- oneOf: discriminator.remainders,
24
- },
25
- });
26
- const matched: OpenApi.IJsonSchema | undefined =
27
- discriminator.remainders.find(
28
- (schema) =>
29
- OpenApiStationValidator.validate({
30
- ...ctx,
31
- schema,
32
- exceptionable: false,
33
- equals: false,
34
- }) === true,
35
- );
36
- if (matched === undefined) return ctx.report(ctx);
37
- return ctx.equals === true
38
- ? OpenApiStationValidator.validate({
39
- ...ctx,
40
- schema: matched,
41
- })
42
- : true;
43
- };
44
-
45
- const getDiscriminator = (
46
- ctx: IOpenApiValidatorContext<OpenApi.IJsonSchema.IOneOf>,
47
- ): IDiscriminator => {
48
- const resolvedList: IFlatSchema[] = ctx.schema.oneOf.map((schema) =>
49
- getFlattened({
50
- components: ctx.components,
51
- schema,
52
- visited: new Set(),
53
- }),
54
- );
55
-
56
- // FIND ANY TYPE
57
- const anything: IFlatSchema | undefined = resolvedList.find((resolved) =>
58
- OpenApiTypeChecker.isUnknown(resolved.escaped),
59
- );
60
- if (anything)
61
- return {
62
- branches: [],
63
- remainders: [anything.schema],
64
- };
65
-
66
- // CHECK NULLABLES
67
- const nullables: IFlatSchema<OpenApi.IJsonSchema.INull>[] =
68
- resolvedList.filter(
69
- (resolved): resolved is IFlatSchema<OpenApi.IJsonSchema.INull> =>
70
- OpenApiTypeChecker.isNull(resolved.schema),
71
- );
72
- const significant: IFlatSchema<OpenApi.IJsonSchema>[] = resolvedList.filter(
73
- (resolved) => false === OpenApiTypeChecker.isNull(resolved.escaped),
74
- );
75
- if (significant.length === 1)
76
- return {
77
- branches: [
78
- {
79
- schema: significant[0]!.schema,
80
- predicator: (value) => value !== null,
81
- },
82
- ],
83
- remainders: nullables.map((nullable) => nullable.schema),
84
- };
85
-
86
- // DISCRIMINATIONS
87
- const tuples = significant.filter((flat) =>
88
- OpenApiTypeChecker.isTuple(flat.escaped),
89
- );
90
- const arrays = significant.filter(
91
- (flat): flat is IFlatSchema<OpenApi.IJsonSchema.IArray> =>
92
- OpenApiTypeChecker.isArray(flat.escaped),
93
- );
94
- const branches: IDiscriminatorBranch[] = [
95
- ...(tuples.length === 0 && arrays.length !== 0
96
- ? discriminateArrays(
97
- ctx,
98
- significant.filter(
99
- (flat): flat is IFlatSchema<OpenApi.IJsonSchema.IArray> =>
100
- OpenApiTypeChecker.isArray(flat.schema),
101
- ),
102
- )
103
- : []),
104
- ...discriminateObjects(
105
- ctx,
106
- significant.filter(
107
- (flat): flat is IFlatSchema<OpenApi.IJsonSchema.IObject> =>
108
- OpenApiTypeChecker.isObject(flat.escaped),
109
- ),
110
- tuples.length + arrays.length === 0,
111
- ),
112
- ];
113
- return {
114
- branches,
115
- remainders: ctx.schema.oneOf.filter(
116
- (x) => branches.some((y) => y.schema === x) === false,
117
- ),
118
- };
119
- };
120
-
121
- const discriminateArrays = (
122
- ctx: IOpenApiValidatorContext<OpenApi.IJsonSchema.IOneOf>,
123
- arraySchemas: IFlatSchema<OpenApi.IJsonSchema.IArray>[],
124
- ): IDiscriminatorBranch[] => {
125
- if (arraySchemas.length === 1)
126
- return [
127
- {
128
- schema: arraySchemas[0]!.schema,
129
- predicator: (value) => Array.isArray(value),
130
- },
131
- ];
132
- return arraySchemas
133
- .filter((flat, i, array) =>
134
- array.every(
135
- (item, j) =>
136
- i === j ||
137
- OpenApiTypeChecker.covers({
138
- components: ctx.components,
139
- x: item.escaped.items,
140
- y: flat.escaped.items,
141
- }) === false,
142
- ),
143
- )
144
- .map(
145
- (flat) =>
146
- ({
147
- schema: flat.schema,
148
- predicator: (value) =>
149
- Array.isArray(value) &&
150
- (value.length === 0 ||
151
- OpenApiStationValidator.validate({
152
- ...ctx,
153
- schema: (flat.escaped as OpenApi.IJsonSchema.IArray).items,
154
- value: value[0]!,
155
- path: `${ctx.path}[0]`,
156
- exceptionable: false,
157
- equals: false,
158
- })),
159
- }) satisfies IDiscriminatorBranch,
160
- );
161
- };
162
-
163
- const discriminateObjects = (
164
- ctx: IOpenApiValidatorContext<OpenApi.IJsonSchema.IOneOf>,
165
- objectSchemas: IFlatSchema<OpenApi.IJsonSchema.IObject>[],
166
- noArray: boolean,
167
- ): IDiscriminatorBranch[] => {
168
- if (objectSchemas.length === 1)
169
- return [
170
- {
171
- schema: objectSchemas[0]!.schema,
172
- predicator: noArray
173
- ? (value) => typeof value === "object" && value !== null
174
- : (value) =>
175
- typeof value === "object" &&
176
- value !== null &&
177
- Array.isArray(value) === false,
178
- },
179
- ];
180
-
181
- // KEEP ONLY REQUIRED PROPERTIES
182
- objectSchemas = objectSchemas
183
- .filter(
184
- (flat) =>
185
- flat.escaped.properties !== undefined &&
186
- flat.escaped.required !== undefined,
187
- )
188
- .map(
189
- (flat) =>
190
- ({
191
- ...flat,
192
- escaped: {
193
- ...flat.escaped,
194
- properties: Object.fromEntries(
195
- Object.entries(flat.escaped.properties ?? {}).map(
196
- ([key, value]) => [
197
- key,
198
- getFlattened({
199
- components: ctx.components,
200
- schema: value,
201
- visited: new Set(),
202
- }).escaped,
203
- ],
204
- ),
205
- ),
206
- },
207
- }) satisfies IFlatSchema<OpenApi.IJsonSchema.IObject>,
208
- );
209
-
210
- // PROPERTY MATRIX
211
- const matrix: Map<string, Array<OpenApi.IJsonSchema | null>> = new Map();
212
- objectSchemas.forEach((obj, i) => {
213
- for (const [key, value] of Object.entries(obj.escaped.properties ?? {})) {
214
- if (!!obj.escaped.required?.includes(key) === false) continue;
215
- MapUtil.take(matrix, key, () =>
216
- new Array(objectSchemas.length).fill(null),
217
- )[i] = value;
218
- }
219
- });
220
-
221
- // THE BRANCHES
222
- return objectSchemas
223
- .map((obj, i) => {
224
- const candidates: string[] = [];
225
- for (const [key, value] of Object.entries(
226
- obj.escaped.properties ?? {},
227
- )) {
228
- if (!!obj.escaped.required?.includes(key) === false) continue;
229
-
230
- const neighbors: OpenApi.IJsonSchema[] = matrix
231
- .get(key)!
232
- .filter((_oppo, j) => i !== j)
233
- .filter((oppo) => oppo !== null);
234
- const unique: boolean = OpenApiTypeChecker.isConstant(value)
235
- ? neighbors.every(
236
- (oppo) =>
237
- OpenApiTypeChecker.isConstant(oppo) &&
238
- value.const !== oppo.const,
239
- )
240
- : neighbors.length === 0;
241
- if (unique) candidates.push(key);
242
- }
243
- if (candidates.length === 0) return null;
244
- const top: string =
245
- candidates.find((key) =>
246
- OpenApiTypeChecker.isConstant(obj.escaped.properties![key]!),
247
- ) ?? candidates[0]!;
248
- const target: OpenApi.IJsonSchema = obj.escaped.properties![top]!;
249
- return {
250
- schema: obj.schema,
251
- predicator: OpenApiTypeChecker.isConstant(target)
252
- ? (value) =>
253
- typeof value === "object" &&
254
- value !== null &&
255
- (value as any)[top] === target.const
256
- : (value) =>
257
- typeof value === "object" &&
258
- value !== null &&
259
- (value as any)[top] !== undefined,
260
- } satisfies IDiscriminatorBranch;
261
- })
262
- .filter((b) => b !== null);
263
- };
264
- }
265
-
266
- const getFlattened = (props: {
267
- components: OpenApi.IComponents;
268
- schema: OpenApi.IJsonSchema;
269
- visited: Set<string>;
270
- }): IFlatSchema => {
271
- if (OpenApiTypeChecker.isReference(props.schema)) {
272
- const key: string = props.schema.$ref.split("/").pop() ?? "";
273
- if (props.visited.has(key))
274
- return {
275
- schema: props.schema,
276
- escaped: {},
277
- };
278
- props.visited.add(key);
279
- return {
280
- ...getFlattened({
281
- components: props.components,
282
- schema: props.components.schemas?.[key] ?? {},
283
- visited: props.visited,
284
- }),
285
- schema: props.schema,
286
- };
287
- }
288
- return {
289
- schema: props.schema,
290
- escaped: props.schema,
291
- };
292
- };
293
-
294
- interface IDiscriminator {
295
- branches: IDiscriminatorBranch[];
296
- remainders: OpenApi.IJsonSchema[];
297
- }
298
-
299
- interface IDiscriminatorBranch {
300
- schema: OpenApi.IJsonSchema;
301
- predicator: (value: unknown) => boolean;
302
- }
303
-
304
- interface IFlatSchema<
305
- Schema extends OpenApi.IJsonSchema = OpenApi.IJsonSchema,
306
- > {
307
- schema: OpenApi.IJsonSchema;
308
- escaped: Schema;
309
- }
1
+ import { OpenApi } from "@typia/interface";
2
+
3
+ import { MapUtil } from "../../utils";
4
+ import { OpenApiTypeChecker } from "../OpenApiTypeChecker";
5
+ import { IOpenApiValidatorContext } from "./IOpenApiValidatorContext";
6
+ import { OpenApiStationValidator } from "./OpenApiStationValidator";
7
+
8
+ export namespace OpenApiOneOfValidator {
9
+ export const validate = (
10
+ ctx: IOpenApiValidatorContext<OpenApi.IJsonSchema.IOneOf>,
11
+ ): boolean => {
12
+ const discriminator: IDiscriminator = getDiscriminator(ctx);
13
+ for (const item of discriminator.branches)
14
+ if (item.predicator(ctx.value))
15
+ return OpenApiStationValidator.validate({
16
+ ...ctx,
17
+ schema: item.schema,
18
+ });
19
+ if (discriminator.branches.length !== 0)
20
+ return validate({
21
+ ...ctx,
22
+ schema: {
23
+ oneOf: discriminator.remainders,
24
+ },
25
+ });
26
+ const matched: OpenApi.IJsonSchema | undefined =
27
+ discriminator.remainders.find(
28
+ (schema) =>
29
+ OpenApiStationValidator.validate({
30
+ ...ctx,
31
+ schema,
32
+ exceptionable: false,
33
+ equals: false,
34
+ }) === true,
35
+ );
36
+ if (matched === undefined) return ctx.report(ctx);
37
+ return ctx.equals === true
38
+ ? OpenApiStationValidator.validate({
39
+ ...ctx,
40
+ schema: matched,
41
+ })
42
+ : true;
43
+ };
44
+
45
+ const getDiscriminator = (
46
+ ctx: IOpenApiValidatorContext<OpenApi.IJsonSchema.IOneOf>,
47
+ ): IDiscriminator => {
48
+ const resolvedList: IFlatSchema[] = ctx.schema.oneOf.map((schema) =>
49
+ getFlattened({
50
+ components: ctx.components,
51
+ schema,
52
+ visited: new Set(),
53
+ }),
54
+ );
55
+
56
+ // FIND ANY TYPE
57
+ const anything: IFlatSchema | undefined = resolvedList.find((resolved) =>
58
+ OpenApiTypeChecker.isUnknown(resolved.escaped),
59
+ );
60
+ if (anything)
61
+ return {
62
+ branches: [],
63
+ remainders: [anything.schema],
64
+ };
65
+
66
+ // CHECK NULLABLES
67
+ const nullables: IFlatSchema<OpenApi.IJsonSchema.INull>[] =
68
+ resolvedList.filter(
69
+ (resolved): resolved is IFlatSchema<OpenApi.IJsonSchema.INull> =>
70
+ OpenApiTypeChecker.isNull(resolved.schema),
71
+ );
72
+ const significant: IFlatSchema<OpenApi.IJsonSchema>[] = resolvedList.filter(
73
+ (resolved) => false === OpenApiTypeChecker.isNull(resolved.escaped),
74
+ );
75
+ if (significant.length === 1)
76
+ return {
77
+ branches: [
78
+ {
79
+ schema: significant[0]!.schema,
80
+ predicator: (value) => value !== null,
81
+ },
82
+ ],
83
+ remainders: nullables.map((nullable) => nullable.schema),
84
+ };
85
+
86
+ // DISCRIMINATIONS
87
+ const tuples = significant.filter((flat) =>
88
+ OpenApiTypeChecker.isTuple(flat.escaped),
89
+ );
90
+ const arrays = significant.filter(
91
+ (flat): flat is IFlatSchema<OpenApi.IJsonSchema.IArray> =>
92
+ OpenApiTypeChecker.isArray(flat.escaped),
93
+ );
94
+ const branches: IDiscriminatorBranch[] = [
95
+ ...(tuples.length === 0 && arrays.length !== 0
96
+ ? discriminateArrays(
97
+ ctx,
98
+ significant.filter(
99
+ (flat): flat is IFlatSchema<OpenApi.IJsonSchema.IArray> =>
100
+ OpenApiTypeChecker.isArray(flat.schema),
101
+ ),
102
+ )
103
+ : []),
104
+ ...discriminateObjects(
105
+ ctx,
106
+ significant.filter(
107
+ (flat): flat is IFlatSchema<OpenApi.IJsonSchema.IObject> =>
108
+ OpenApiTypeChecker.isObject(flat.escaped),
109
+ ),
110
+ tuples.length + arrays.length === 0,
111
+ ),
112
+ ];
113
+ return {
114
+ branches,
115
+ remainders: ctx.schema.oneOf.filter(
116
+ (x) => branches.some((y) => y.schema === x) === false,
117
+ ),
118
+ };
119
+ };
120
+
121
+ const discriminateArrays = (
122
+ ctx: IOpenApiValidatorContext<OpenApi.IJsonSchema.IOneOf>,
123
+ arraySchemas: IFlatSchema<OpenApi.IJsonSchema.IArray>[],
124
+ ): IDiscriminatorBranch[] => {
125
+ if (arraySchemas.length === 1)
126
+ return [
127
+ {
128
+ schema: arraySchemas[0]!.schema,
129
+ predicator: (value) => Array.isArray(value),
130
+ },
131
+ ];
132
+ return arraySchemas
133
+ .filter((flat, i, array) =>
134
+ array.every(
135
+ (item, j) =>
136
+ i === j ||
137
+ OpenApiTypeChecker.covers({
138
+ components: ctx.components,
139
+ x: item.escaped.items,
140
+ y: flat.escaped.items,
141
+ }) === false,
142
+ ),
143
+ )
144
+ .map(
145
+ (flat) =>
146
+ ({
147
+ schema: flat.schema,
148
+ predicator: (value) =>
149
+ Array.isArray(value) &&
150
+ (value.length === 0 ||
151
+ OpenApiStationValidator.validate({
152
+ ...ctx,
153
+ schema: (flat.escaped as OpenApi.IJsonSchema.IArray).items,
154
+ value: value[0]!,
155
+ path: `${ctx.path}[0]`,
156
+ exceptionable: false,
157
+ equals: false,
158
+ })),
159
+ }) satisfies IDiscriminatorBranch,
160
+ );
161
+ };
162
+
163
+ const discriminateObjects = (
164
+ ctx: IOpenApiValidatorContext<OpenApi.IJsonSchema.IOneOf>,
165
+ objectSchemas: IFlatSchema<OpenApi.IJsonSchema.IObject>[],
166
+ noArray: boolean,
167
+ ): IDiscriminatorBranch[] => {
168
+ if (objectSchemas.length === 1)
169
+ return [
170
+ {
171
+ schema: objectSchemas[0]!.schema,
172
+ predicator: noArray
173
+ ? (value) => typeof value === "object" && value !== null
174
+ : (value) =>
175
+ typeof value === "object" &&
176
+ value !== null &&
177
+ Array.isArray(value) === false,
178
+ },
179
+ ];
180
+
181
+ // KEEP ONLY REQUIRED PROPERTIES
182
+ objectSchemas = objectSchemas
183
+ .filter(
184
+ (flat) =>
185
+ flat.escaped.properties !== undefined &&
186
+ flat.escaped.required !== undefined,
187
+ )
188
+ .map(
189
+ (flat) =>
190
+ ({
191
+ ...flat,
192
+ escaped: {
193
+ ...flat.escaped,
194
+ properties: Object.fromEntries(
195
+ Object.entries(flat.escaped.properties ?? {}).map(
196
+ ([key, value]) => [
197
+ key,
198
+ getFlattened({
199
+ components: ctx.components,
200
+ schema: value,
201
+ visited: new Set(),
202
+ }).escaped,
203
+ ],
204
+ ),
205
+ ),
206
+ },
207
+ }) satisfies IFlatSchema<OpenApi.IJsonSchema.IObject>,
208
+ );
209
+
210
+ // PROPERTY MATRIX
211
+ const matrix: Map<string, Array<OpenApi.IJsonSchema | null>> = new Map();
212
+ objectSchemas.forEach((obj, i) => {
213
+ for (const [key, value] of Object.entries(obj.escaped.properties ?? {})) {
214
+ if (!!obj.escaped.required?.includes(key) === false) continue;
215
+ MapUtil.take(matrix, key, () =>
216
+ new Array(objectSchemas.length).fill(null),
217
+ )[i] = value;
218
+ }
219
+ });
220
+
221
+ // THE BRANCHES
222
+ return objectSchemas
223
+ .map((obj, i) => {
224
+ const candidates: string[] = [];
225
+ for (const [key, value] of Object.entries(
226
+ obj.escaped.properties ?? {},
227
+ )) {
228
+ if (!!obj.escaped.required?.includes(key) === false) continue;
229
+
230
+ const neighbors: OpenApi.IJsonSchema[] = matrix
231
+ .get(key)!
232
+ .filter((_oppo, j) => i !== j)
233
+ .filter((oppo) => oppo !== null);
234
+ const unique: boolean = OpenApiTypeChecker.isConstant(value)
235
+ ? neighbors.every(
236
+ (oppo) =>
237
+ OpenApiTypeChecker.isConstant(oppo) &&
238
+ value.const !== oppo.const,
239
+ )
240
+ : neighbors.length === 0;
241
+ if (unique) candidates.push(key);
242
+ }
243
+ if (candidates.length === 0) return null;
244
+ const top: string =
245
+ candidates.find((key) =>
246
+ OpenApiTypeChecker.isConstant(obj.escaped.properties![key]!),
247
+ ) ?? candidates[0]!;
248
+ const target: OpenApi.IJsonSchema = obj.escaped.properties![top]!;
249
+ return {
250
+ schema: obj.schema,
251
+ predicator: OpenApiTypeChecker.isConstant(target)
252
+ ? (value) =>
253
+ typeof value === "object" &&
254
+ value !== null &&
255
+ (value as any)[top] === target.const
256
+ : (value) =>
257
+ typeof value === "object" &&
258
+ value !== null &&
259
+ (value as any)[top] !== undefined,
260
+ } satisfies IDiscriminatorBranch;
261
+ })
262
+ .filter((b) => b !== null);
263
+ };
264
+ }
265
+
266
+ const getFlattened = (props: {
267
+ components: OpenApi.IComponents;
268
+ schema: OpenApi.IJsonSchema;
269
+ visited: Set<string>;
270
+ }): IFlatSchema => {
271
+ if (OpenApiTypeChecker.isReference(props.schema)) {
272
+ const key: string = props.schema.$ref.split("/").pop() ?? "";
273
+ if (props.visited.has(key))
274
+ return {
275
+ schema: props.schema,
276
+ escaped: {},
277
+ };
278
+ props.visited.add(key);
279
+ return {
280
+ ...getFlattened({
281
+ components: props.components,
282
+ schema: props.components.schemas?.[key] ?? {},
283
+ visited: props.visited,
284
+ }),
285
+ schema: props.schema,
286
+ };
287
+ }
288
+ return {
289
+ schema: props.schema,
290
+ escaped: props.schema,
291
+ };
292
+ };
293
+
294
+ interface IDiscriminator {
295
+ branches: IDiscriminatorBranch[];
296
+ remainders: OpenApi.IJsonSchema[];
297
+ }
298
+
299
+ interface IDiscriminatorBranch {
300
+ schema: OpenApi.IJsonSchema;
301
+ predicator: (value: unknown) => boolean;
302
+ }
303
+
304
+ interface IFlatSchema<
305
+ Schema extends OpenApi.IJsonSchema = OpenApi.IJsonSchema,
306
+ > {
307
+ schema: OpenApi.IJsonSchema;
308
+ escaped: Schema;
309
+ }