@typia/utils 12.0.0-dev.20260309 → 12.0.0-dev.20260310
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/lib/http/internal/HttpLlmApplicationComposer.mjs +5 -1
- package/lib/http/internal/HttpLlmApplicationComposer.mjs.map +1 -1
- package/lib/index.mjs +9 -9
- package/lib/utils/LlmJson.mjs +9 -2
- package/lib/utils/LlmJson.mjs.map +1 -1
- package/lib/utils/internal/stringifyValidationFailure.js +17 -15
- package/lib/utils/internal/stringifyValidationFailure.js.map +1 -1
- package/lib/utils/internal/stringifyValidationFailure.mjs +17 -15
- package/lib/utils/internal/stringifyValidationFailure.mjs.map +1 -1
- package/lib/validators/internal/OpenApiOneOfValidator.mjs +5 -1
- package/lib/validators/internal/OpenApiOneOfValidator.mjs.map +1 -1
- package/package.json +2 -2
- package/src/converters/LlmSchemaConverter.ts +647 -647
- package/src/converters/OpenApiConverter.ts +285 -285
- package/src/converters/index.ts +5 -5
- package/src/converters/internal/LlmDescriptionInverter.ts +178 -178
- package/src/converters/internal/LlmParametersComposer.ts +52 -52
- package/src/converters/internal/OpenApiConstraintShifter.ts +154 -154
- package/src/converters/internal/OpenApiExclusiveEmender.ts +46 -46
- package/src/converters/internal/OpenApiV3Downgrader.ts +355 -355
- package/src/converters/internal/OpenApiV3Upgrader.ts +470 -470
- package/src/converters/internal/OpenApiV3_1Upgrader.ts +685 -685
- package/src/converters/internal/SwaggerV2Downgrader.ts +424 -424
- package/src/converters/internal/SwaggerV2Upgrader.ts +523 -523
- package/src/http/HttpError.ts +107 -107
- package/src/http/HttpLlm.ts +167 -167
- package/src/http/HttpMigration.ts +92 -92
- package/src/http/index.ts +3 -3
- package/src/http/internal/HttpLlmApplicationComposer.ts +361 -361
- package/src/http/internal/HttpLlmFunctionFetcher.ts +37 -37
- package/src/http/internal/HttpMigrateApplicationComposer.ts +56 -56
- package/src/http/internal/HttpMigrateRouteAccessor.ts +135 -135
- package/src/http/internal/HttpMigrateRouteComposer.ts +505 -505
- package/src/http/internal/HttpMigrateRouteFetcher.ts +203 -203
- package/src/index.ts +4 -4
- package/src/utils/ArrayUtil.ts +42 -42
- package/src/utils/LlmJson.ts +141 -141
- package/src/utils/MapUtil.ts +15 -15
- package/src/utils/NamingConvention.ts +205 -205
- package/src/utils/Singleton.ts +17 -17
- package/src/utils/StringUtil.ts +14 -14
- package/src/utils/dedent.ts +57 -57
- package/src/utils/index.ts +8 -8
- package/src/utils/internal/EndpointUtil.ts +44 -44
- package/src/utils/internal/JsonDescriptor.ts +70 -70
- package/src/utils/internal/OpenApiTypeCheckerBase.ts +822 -822
- package/src/utils/internal/coerceLlmArguments.ts +314 -314
- package/src/utils/internal/parseLenientJson.ts +894 -894
- package/src/utils/internal/stringifyValidationFailure.ts +415 -411
- package/src/validators/LlmTypeChecker.ts +402 -402
- package/src/validators/OpenApiTypeChecker.ts +297 -297
- package/src/validators/OpenApiV3TypeChecker.ts +70 -70
- package/src/validators/OpenApiV3_1TypeChecker.ts +86 -86
- package/src/validators/OpenApiValidator.ts +94 -94
- package/src/validators/SwaggerV2TypeChecker.ts +71 -71
- package/src/validators/functional/_isBigintString.ts +8 -8
- package/src/validators/functional/_isFormatByte.ts +7 -7
- package/src/validators/functional/_isFormatDate.ts +3 -3
- package/src/validators/functional/_isFormatDateTime.ts +4 -4
- package/src/validators/functional/_isFormatDuration.ts +4 -4
- package/src/validators/functional/_isFormatEmail.ts +4 -4
- package/src/validators/functional/_isFormatHostname.ts +4 -4
- package/src/validators/functional/_isFormatIdnEmail.ts +4 -4
- package/src/validators/functional/_isFormatIdnHostname.ts +4 -4
- package/src/validators/functional/_isFormatIpv4.ts +4 -4
- package/src/validators/functional/_isFormatIpv6.ts +4 -4
- package/src/validators/functional/_isFormatIri.ts +3 -3
- package/src/validators/functional/_isFormatIriReference.ts +4 -4
- package/src/validators/functional/_isFormatJsonPointer.ts +3 -3
- package/src/validators/functional/_isFormatPassword.ts +1 -1
- package/src/validators/functional/_isFormatRegex.ts +8 -8
- package/src/validators/functional/_isFormatRelativeJsonPointer.ts +4 -4
- package/src/validators/functional/_isFormatTime.ts +4 -4
- package/src/validators/functional/_isFormatUri.ts +6 -6
- package/src/validators/functional/_isFormatUriReference.ts +5 -5
- package/src/validators/functional/_isFormatUriTemplate.ts +4 -4
- package/src/validators/functional/_isFormatUrl.ts +4 -4
- package/src/validators/functional/_isFormatUuid.ts +3 -3
- package/src/validators/functional/_isUniqueItems.ts +159 -159
- package/src/validators/index.ts +14 -14
- package/src/validators/internal/IOpenApiValidatorContext.ts +17 -17
- package/src/validators/internal/OpenApiArrayValidator.ts +49 -49
- package/src/validators/internal/OpenApiBooleanValidator.ts +11 -11
- package/src/validators/internal/OpenApiConstantValidator.ts +11 -11
- package/src/validators/internal/OpenApiIntegerValidator.ts +49 -49
- package/src/validators/internal/OpenApiNumberValidator.ts +48 -48
- package/src/validators/internal/OpenApiObjectValidator.ts +83 -83
- package/src/validators/internal/OpenApiOneOfValidator.ts +309 -309
- package/src/validators/internal/OpenApiSchemaNamingRule.ts +124 -124
- package/src/validators/internal/OpenApiStationValidator.ts +115 -115
- package/src/validators/internal/OpenApiStringValidator.ts +88 -88
- 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
|
+
}
|