@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.
Files changed (92) hide show
  1. package/lib/http/internal/HttpLlmApplicationComposer.mjs +5 -1
  2. package/lib/http/internal/HttpLlmApplicationComposer.mjs.map +1 -1
  3. package/lib/index.mjs +9 -9
  4. package/lib/utils/LlmJson.mjs +9 -2
  5. package/lib/utils/LlmJson.mjs.map +1 -1
  6. package/lib/utils/internal/stringifyValidationFailure.js +17 -15
  7. package/lib/utils/internal/stringifyValidationFailure.js.map +1 -1
  8. package/lib/utils/internal/stringifyValidationFailure.mjs +17 -15
  9. package/lib/utils/internal/stringifyValidationFailure.mjs.map +1 -1
  10. package/lib/validators/internal/OpenApiOneOfValidator.mjs +5 -1
  11. package/lib/validators/internal/OpenApiOneOfValidator.mjs.map +1 -1
  12. package/package.json +2 -2
  13. package/src/converters/LlmSchemaConverter.ts +647 -647
  14. package/src/converters/OpenApiConverter.ts +285 -285
  15. package/src/converters/index.ts +5 -5
  16. package/src/converters/internal/LlmDescriptionInverter.ts +178 -178
  17. package/src/converters/internal/LlmParametersComposer.ts +52 -52
  18. package/src/converters/internal/OpenApiConstraintShifter.ts +154 -154
  19. package/src/converters/internal/OpenApiExclusiveEmender.ts +46 -46
  20. package/src/converters/internal/OpenApiV3Downgrader.ts +355 -355
  21. package/src/converters/internal/OpenApiV3Upgrader.ts +470 -470
  22. package/src/converters/internal/OpenApiV3_1Upgrader.ts +685 -685
  23. package/src/converters/internal/SwaggerV2Downgrader.ts +424 -424
  24. package/src/converters/internal/SwaggerV2Upgrader.ts +523 -523
  25. package/src/http/HttpError.ts +107 -107
  26. package/src/http/HttpLlm.ts +167 -167
  27. package/src/http/HttpMigration.ts +92 -92
  28. package/src/http/index.ts +3 -3
  29. package/src/http/internal/HttpLlmApplicationComposer.ts +361 -361
  30. package/src/http/internal/HttpLlmFunctionFetcher.ts +37 -37
  31. package/src/http/internal/HttpMigrateApplicationComposer.ts +56 -56
  32. package/src/http/internal/HttpMigrateRouteAccessor.ts +135 -135
  33. package/src/http/internal/HttpMigrateRouteComposer.ts +505 -505
  34. package/src/http/internal/HttpMigrateRouteFetcher.ts +203 -203
  35. package/src/index.ts +4 -4
  36. package/src/utils/ArrayUtil.ts +42 -42
  37. package/src/utils/LlmJson.ts +141 -141
  38. package/src/utils/MapUtil.ts +15 -15
  39. package/src/utils/NamingConvention.ts +205 -205
  40. package/src/utils/Singleton.ts +17 -17
  41. package/src/utils/StringUtil.ts +14 -14
  42. package/src/utils/dedent.ts +57 -57
  43. package/src/utils/index.ts +8 -8
  44. package/src/utils/internal/EndpointUtil.ts +44 -44
  45. package/src/utils/internal/JsonDescriptor.ts +70 -70
  46. package/src/utils/internal/OpenApiTypeCheckerBase.ts +822 -822
  47. package/src/utils/internal/coerceLlmArguments.ts +314 -314
  48. package/src/utils/internal/parseLenientJson.ts +894 -894
  49. package/src/utils/internal/stringifyValidationFailure.ts +415 -411
  50. package/src/validators/LlmTypeChecker.ts +402 -402
  51. package/src/validators/OpenApiTypeChecker.ts +297 -297
  52. package/src/validators/OpenApiV3TypeChecker.ts +70 -70
  53. package/src/validators/OpenApiV3_1TypeChecker.ts +86 -86
  54. package/src/validators/OpenApiValidator.ts +94 -94
  55. package/src/validators/SwaggerV2TypeChecker.ts +71 -71
  56. package/src/validators/functional/_isBigintString.ts +8 -8
  57. package/src/validators/functional/_isFormatByte.ts +7 -7
  58. package/src/validators/functional/_isFormatDate.ts +3 -3
  59. package/src/validators/functional/_isFormatDateTime.ts +4 -4
  60. package/src/validators/functional/_isFormatDuration.ts +4 -4
  61. package/src/validators/functional/_isFormatEmail.ts +4 -4
  62. package/src/validators/functional/_isFormatHostname.ts +4 -4
  63. package/src/validators/functional/_isFormatIdnEmail.ts +4 -4
  64. package/src/validators/functional/_isFormatIdnHostname.ts +4 -4
  65. package/src/validators/functional/_isFormatIpv4.ts +4 -4
  66. package/src/validators/functional/_isFormatIpv6.ts +4 -4
  67. package/src/validators/functional/_isFormatIri.ts +3 -3
  68. package/src/validators/functional/_isFormatIriReference.ts +4 -4
  69. package/src/validators/functional/_isFormatJsonPointer.ts +3 -3
  70. package/src/validators/functional/_isFormatPassword.ts +1 -1
  71. package/src/validators/functional/_isFormatRegex.ts +8 -8
  72. package/src/validators/functional/_isFormatRelativeJsonPointer.ts +4 -4
  73. package/src/validators/functional/_isFormatTime.ts +4 -4
  74. package/src/validators/functional/_isFormatUri.ts +6 -6
  75. package/src/validators/functional/_isFormatUriReference.ts +5 -5
  76. package/src/validators/functional/_isFormatUriTemplate.ts +4 -4
  77. package/src/validators/functional/_isFormatUrl.ts +4 -4
  78. package/src/validators/functional/_isFormatUuid.ts +3 -3
  79. package/src/validators/functional/_isUniqueItems.ts +159 -159
  80. package/src/validators/index.ts +14 -14
  81. package/src/validators/internal/IOpenApiValidatorContext.ts +17 -17
  82. package/src/validators/internal/OpenApiArrayValidator.ts +49 -49
  83. package/src/validators/internal/OpenApiBooleanValidator.ts +11 -11
  84. package/src/validators/internal/OpenApiConstantValidator.ts +11 -11
  85. package/src/validators/internal/OpenApiIntegerValidator.ts +49 -49
  86. package/src/validators/internal/OpenApiNumberValidator.ts +48 -48
  87. package/src/validators/internal/OpenApiObjectValidator.ts +83 -83
  88. package/src/validators/internal/OpenApiOneOfValidator.ts +309 -309
  89. package/src/validators/internal/OpenApiSchemaNamingRule.ts +124 -124
  90. package/src/validators/internal/OpenApiStationValidator.ts +115 -115
  91. package/src/validators/internal/OpenApiStringValidator.ts +88 -88
  92. package/src/validators/internal/OpenApiTupleValidator.ts +55 -55
@@ -1,822 +1,822 @@
1
- import { IJsonSchemaTransformError, IResult, OpenApi } from "@typia/interface";
2
-
3
- import { MapUtil } from "../MapUtil";
4
- import { JsonDescriptor } from "./JsonDescriptor";
5
-
6
- /** @internal */
7
- export namespace OpenApiTypeCheckerBase {
8
- /* -----------------------------------------------------------
9
- TYPE CHECKERS
10
- ----------------------------------------------------------- */
11
- export const isNull = (
12
- schema: OpenApi.IJsonSchema,
13
- ): schema is OpenApi.IJsonSchema.INull =>
14
- (schema as OpenApi.IJsonSchema.INull).type === "null";
15
-
16
- export const isUnknown = (
17
- schema: OpenApi.IJsonSchema,
18
- ): schema is OpenApi.IJsonSchema.IUnknown =>
19
- (schema as OpenApi.IJsonSchema.IUnknown).type === undefined &&
20
- !isConstant(schema) &&
21
- !isOneOf(schema) &&
22
- !isReference(schema);
23
-
24
- export const isConstant = (
25
- schema: OpenApi.IJsonSchema,
26
- ): schema is OpenApi.IJsonSchema.IConstant =>
27
- (schema as OpenApi.IJsonSchema.IConstant).const !== undefined;
28
-
29
- export const isBoolean = (
30
- schema: OpenApi.IJsonSchema,
31
- ): schema is OpenApi.IJsonSchema.IBoolean =>
32
- (schema as OpenApi.IJsonSchema.IBoolean).type === "boolean";
33
-
34
- export const isInteger = (
35
- schema: OpenApi.IJsonSchema,
36
- ): schema is OpenApi.IJsonSchema.IInteger =>
37
- (schema as OpenApi.IJsonSchema.IInteger).type === "integer";
38
-
39
- export const isNumber = (
40
- schema: OpenApi.IJsonSchema,
41
- ): schema is OpenApi.IJsonSchema.INumber =>
42
- (schema as OpenApi.IJsonSchema.INumber).type === "number";
43
-
44
- export const isString = (
45
- schema: OpenApi.IJsonSchema,
46
- ): schema is OpenApi.IJsonSchema.IString =>
47
- (schema as OpenApi.IJsonSchema.IString).type === "string";
48
-
49
- export const isArray = (
50
- schema: OpenApi.IJsonSchema,
51
- ): schema is OpenApi.IJsonSchema.IArray =>
52
- (schema as OpenApi.IJsonSchema.IArray).type === "array" &&
53
- (schema as OpenApi.IJsonSchema.IArray).items !== undefined;
54
-
55
- export const isTuple = (
56
- schema: OpenApi.IJsonSchema,
57
- ): schema is OpenApi.IJsonSchema.ITuple =>
58
- (schema as OpenApi.IJsonSchema.ITuple).type === "array" &&
59
- (schema as OpenApi.IJsonSchema.ITuple).prefixItems !== undefined;
60
-
61
- export const isObject = (
62
- schema: OpenApi.IJsonSchema,
63
- ): schema is OpenApi.IJsonSchema.IObject =>
64
- (schema as OpenApi.IJsonSchema.IObject).type === "object";
65
-
66
- export const isReference = (
67
- schema: OpenApi.IJsonSchema,
68
- ): schema is OpenApi.IJsonSchema.IReference =>
69
- (schema as any).$ref !== undefined;
70
-
71
- export const isOneOf = (
72
- schema: OpenApi.IJsonSchema,
73
- ): schema is OpenApi.IJsonSchema.IOneOf =>
74
- (schema as OpenApi.IJsonSchema.IOneOf).oneOf !== undefined;
75
-
76
- export const isRecursiveReference = (props: {
77
- prefix: string;
78
- components: OpenApi.IComponents;
79
- schema: OpenApi.IJsonSchema;
80
- }): boolean => {
81
- if (isReference(props.schema) === false) return false;
82
- const current: string =
83
- props.schema.$ref.split(props.prefix)[1] ??
84
- props.schema.$ref.split("/").at(-1)!;
85
- let counter: number = 0;
86
- visit({
87
- prefix: props.prefix,
88
- components: props.components,
89
- schema: props.schema,
90
- closure: (schema) => {
91
- if (isReference(schema)) {
92
- const next: string =
93
- schema.$ref.split(props.prefix)[1] ??
94
- schema.$ref.split("/").at(-1)!;
95
- if (current === next) ++counter;
96
- }
97
- },
98
- });
99
- return counter > 1;
100
- };
101
-
102
- /* -----------------------------------------------------------
103
- OPERATORS
104
- ----------------------------------------------------------- */
105
- export const unreference = (props: {
106
- prefix: string;
107
- method: string;
108
- components: OpenApi.IComponents;
109
- schema: OpenApi.IJsonSchema;
110
- accessor?: string;
111
- refAccessor?: string;
112
- }): IResult<OpenApi.IJsonSchema, IJsonSchemaTransformError> => {
113
- const reasons: IJsonSchemaTransformError.IReason[] = [];
114
- const result: OpenApi.IJsonSchema | null = unreferenceSchema({
115
- prefix: props.prefix,
116
- refAccessor:
117
- props.refAccessor ??
118
- `$input.${props.prefix
119
- .substring(2)
120
- .split("/")
121
- .filter((s) => !!s.length)
122
- .join(".")}`,
123
- accessor: props.accessor ?? "$input.schema",
124
- components: props.components,
125
- schema: props.schema,
126
- reasons,
127
- });
128
- if (result === null)
129
- return {
130
- success: false,
131
- error: {
132
- method: props.method,
133
- message: `failed to unreference due to unable to find.`,
134
- reasons,
135
- },
136
- };
137
- return {
138
- success: true,
139
- value: result,
140
- };
141
- };
142
-
143
- export const escape = (props: {
144
- prefix: string;
145
- method: string;
146
- components: OpenApi.IComponents;
147
- schema: OpenApi.IJsonSchema;
148
- recursive: false | number;
149
- accessor?: string;
150
- refAccessor?: string;
151
- }): IResult<OpenApi.IJsonSchema, IJsonSchemaTransformError> => {
152
- const reasons: IJsonSchemaTransformError.IReason[] = [];
153
- const result: OpenApi.IJsonSchema | null =
154
- escapeSchema({
155
- ...props,
156
- reasons,
157
- visited: new Map(),
158
- accessor: props.accessor ?? "$input.schema",
159
- refAccessor: props.refAccessor ?? getReference(props.prefix),
160
- }) || null;
161
- if (result === null)
162
- return {
163
- success: false,
164
- error: {
165
- method: props.method,
166
- message: `failed to escape some reference type(s) due to unable to find${Number(props.recursive) === 0 ? " or recursive relationship" : ""}.`,
167
- reasons,
168
- },
169
- };
170
- return {
171
- success: true,
172
- value: result,
173
- };
174
- };
175
-
176
- export const visit = (props: {
177
- prefix: string;
178
- closure: (schema: OpenApi.IJsonSchema, accessor: string) => void;
179
- components: OpenApi.IComponents;
180
- schema: OpenApi.IJsonSchema;
181
- accessor?: string;
182
- refAccessor?: string;
183
- }): void => {
184
- const already: Set<string> = new Set();
185
- const refAccessor: string =
186
- props.refAccessor ?? `$input.${getReference(props.prefix)}`;
187
- const next = (schema: OpenApi.IJsonSchema, accessor: string): void => {
188
- props.closure(schema, accessor);
189
- if (isReference(schema)) {
190
- const key: string = schema.$ref.split(props.prefix).pop()!;
191
- if (already.has(key) === true) return;
192
- already.add(key);
193
- const found: OpenApi.IJsonSchema | undefined =
194
- props.components.schemas?.[key];
195
- if (found !== undefined)
196
- next(found, `${refAccessor}[${JSON.stringify(key)}]`);
197
- } else if (isOneOf(schema))
198
- schema.oneOf.forEach((s, i) => next(s, `${accessor}.oneOf[${i}]`));
199
- else if (isObject(schema)) {
200
- for (const [key, value] of Object.entries(schema.properties ?? {}))
201
- next(value, `${accessor}.properties[${JSON.stringify(key)}]`);
202
- if (
203
- typeof schema.additionalProperties === "object" &&
204
- schema.additionalProperties !== null
205
- )
206
- next(schema.additionalProperties, `${accessor}.additionalProperties`);
207
- } else if (isArray(schema)) next(schema.items, `${accessor}.items`);
208
- else if (isTuple(schema)) {
209
- (schema.prefixItems ?? []).forEach((s, i) =>
210
- next(s, `${accessor}.prefixItems[${i}]`),
211
- );
212
- if (
213
- typeof schema.additionalItems === "object" &&
214
- schema.additionalItems !== null
215
- )
216
- next(schema.additionalItems, `${accessor}.additionalItems`);
217
- }
218
- };
219
- next(props.schema, props.accessor ?? "$input.schema");
220
- };
221
-
222
- export const covers = (props: {
223
- prefix: string;
224
- components: OpenApi.IComponents;
225
- x: OpenApi.IJsonSchema;
226
- y: OpenApi.IJsonSchema;
227
- }): boolean =>
228
- coverStation({
229
- prefix: props.prefix,
230
- components: props.components,
231
- x: props.x,
232
- y: props.y,
233
- visited: new Map(),
234
- });
235
-
236
- const unreferenceSchema = (props: {
237
- prefix: string;
238
- refAccessor: string;
239
- accessor: string;
240
- components: OpenApi.IComponents;
241
- schema: OpenApi.IJsonSchema;
242
- reasons: IJsonSchemaTransformError.IReason[];
243
- first?: string;
244
- }): OpenApi.IJsonSchema | null => {
245
- if (isReference(props.schema) === false) return props.schema;
246
- const key: string = props.schema.$ref.split(props.prefix).pop()!;
247
- const found: OpenApi.IJsonSchema | undefined =
248
- props.components.schemas?.[key];
249
- if (found === undefined) {
250
- props.reasons.push({
251
- schema: props.schema,
252
- accessor: props.accessor,
253
- message: `unable to find reference type ${JSON.stringify(key)}.`,
254
- });
255
- return null;
256
- } else if (isReference(found) === false) return found;
257
- else if (props.first === key) {
258
- props.reasons.push({
259
- schema: props.schema,
260
- accessor: props.accessor,
261
- message: `recursive reference type ${JSON.stringify(key)}.`,
262
- });
263
- return null;
264
- }
265
- return unreferenceSchema({
266
- ...props,
267
- accessor: `${props.refAccessor}[${JSON.stringify(key)}]`,
268
- first: key,
269
- });
270
- };
271
-
272
- const escapeSchema = (props: {
273
- components: OpenApi.IComponents;
274
- prefix: string;
275
- schema: OpenApi.IJsonSchema;
276
- recursive: false | number;
277
- visited: Map<string, number>;
278
- reasons: IJsonSchemaTransformError.IReason[];
279
- accessor: string;
280
- refAccessor: string;
281
- }): OpenApi.IJsonSchema | null | undefined => {
282
- if (isReference(props.schema)) {
283
- // REFERENCE
284
- const key: string =
285
- props.schema.$ref.split(props.prefix)[1] ??
286
- props.schema.$ref.split("/").at(-1)!;
287
- const target: OpenApi.IJsonSchema | undefined =
288
- props.components.schemas?.[key];
289
- if (target === undefined) {
290
- props.reasons.push({
291
- schema: props.schema,
292
- accessor: props.accessor,
293
- message: `unable to find reference type ${JSON.stringify(key)}.`,
294
- });
295
- return null;
296
- } else if (props.visited.has(key) === true) {
297
- if (props.recursive === false) return null;
298
- const depth: number = props.visited.get(key)!;
299
- if (depth > props.recursive) {
300
- if (props.recursive === 0) {
301
- props.reasons.push({
302
- schema: props.schema,
303
- accessor: props.accessor,
304
- message: `recursive reference type ${JSON.stringify(key)}.`,
305
- });
306
- return null;
307
- }
308
- return undefined;
309
- }
310
- props.visited.set(key, depth + 1);
311
- const res: OpenApi.IJsonSchema | null | undefined = escapeSchema({
312
- ...props,
313
- schema: target,
314
- accessor: `${props.refAccessor}[${JSON.stringify(key)}]`,
315
- });
316
- return res
317
- ? {
318
- ...res,
319
- description: JsonDescriptor.cascade({
320
- prefix: props.prefix,
321
- components: props.components,
322
- schema: props.schema,
323
- escape: true,
324
- }),
325
- }
326
- : res;
327
- } else {
328
- const res: OpenApi.IJsonSchema | null | undefined = escapeSchema({
329
- ...props,
330
- schema: target,
331
- accessor: `${props.refAccessor}[${JSON.stringify(key)}]`,
332
- visited: new Map([...props.visited, [key, 1]]),
333
- });
334
- return res
335
- ? {
336
- ...res,
337
- description: JsonDescriptor.cascade({
338
- prefix: props.prefix,
339
- components: props.components,
340
- schema: props.schema,
341
- escape: true,
342
- }),
343
- }
344
- : res;
345
- }
346
- } else if (isOneOf(props.schema)) {
347
- // UNION
348
- const elements: Array<OpenApi.IJsonSchema | null | undefined> =
349
- props.schema.oneOf.map((s, i) =>
350
- escapeSchema({
351
- ...props,
352
- schema: s,
353
- accessor: `${props.accessor}.oneOf[${i}]`,
354
- }),
355
- );
356
- if (elements.some((v) => v === null)) return null;
357
- const filtered: OpenApi.IJsonSchema[] = elements.filter(
358
- (v) => v !== undefined,
359
- ) as OpenApi.IJsonSchema[];
360
- if (filtered.length === 0) return undefined;
361
- return {
362
- ...props.schema,
363
- oneOf: filtered
364
- .map((v) =>
365
- flatSchema({
366
- prefix: props.prefix,
367
- components: props.components,
368
- schema: v,
369
- }),
370
- )
371
- .flat(),
372
- };
373
- } else if (isObject(props.schema)) {
374
- // OBJECT
375
- const object: OpenApi.IJsonSchema.IObject = props.schema;
376
- const properties: Array<
377
- [string, OpenApi.IJsonSchema | null | undefined]
378
- > = Object.entries(object.properties ?? {}).map(([k, s]) => [
379
- k,
380
- escapeSchema({
381
- ...props,
382
- schema: s,
383
- visited: props.visited,
384
- accessor: `${props.accessor}.properties[${JSON.stringify(k)}]`,
385
- }),
386
- ]);
387
- const additionalProperties:
388
- | OpenApi.IJsonSchema
389
- | null
390
- | boolean
391
- | undefined = object.additionalProperties
392
- ? typeof object.additionalProperties === "object" &&
393
- object.additionalProperties !== null
394
- ? escapeSchema({
395
- ...props,
396
- schema: object.additionalProperties,
397
- accessor: `${props.accessor}.additionalProperties`,
398
- })
399
- : object.additionalProperties
400
- : false;
401
- if (
402
- properties.some(([_k, v]) => v === null) ||
403
- additionalProperties === null
404
- )
405
- return null;
406
- else if (
407
- properties.some(
408
- ([k, v]) => v === undefined && object.required?.includes(k) === true,
409
- ) === true
410
- )
411
- return undefined;
412
- return {
413
- ...object,
414
- properties: Object.fromEntries(
415
- properties.filter(([_k, v]) => v !== undefined) as Array<
416
- [string, OpenApi.IJsonSchema]
417
- >,
418
- ),
419
- additionalProperties: additionalProperties ?? false,
420
- required:
421
- object.required?.filter((k) =>
422
- properties.some(([key, value]) => key === k && value !== undefined),
423
- ) ?? [],
424
- };
425
- } else if (isTuple(props.schema)) {
426
- // TUPLE
427
- const elements: Array<OpenApi.IJsonSchema | null | undefined> =
428
- props.schema.prefixItems.map((s, i) =>
429
- escapeSchema({
430
- ...props,
431
- schema: s,
432
- accessor: `${props.accessor}.prefixItems[${i}]`,
433
- }),
434
- );
435
- const additionalItems: OpenApi.IJsonSchema | null | boolean | undefined =
436
- props.schema.additionalItems
437
- ? typeof props.schema.additionalItems === "object" &&
438
- props.schema.additionalItems !== null
439
- ? escapeSchema({
440
- ...props,
441
- schema: props.schema.additionalItems,
442
- accessor: `${props.accessor}.additionalItems`,
443
- })
444
- : props.schema.additionalItems
445
- : false;
446
- if (elements.some((v) => v === null) || additionalItems === null)
447
- return null;
448
- else if (elements.some((v) => v === undefined)) return undefined;
449
- return {
450
- ...props.schema,
451
- prefixItems: elements as OpenApi.IJsonSchema[],
452
- additionalItems: additionalItems ?? false,
453
- };
454
- } else if (isArray(props.schema)) {
455
- // ARRAY
456
- const items: OpenApi.IJsonSchema | null | undefined = escapeSchema({
457
- ...props,
458
- schema: props.schema.items,
459
- accessor: `${props.accessor}.items`,
460
- });
461
- if (items === null) return null;
462
- else if (items === undefined)
463
- return {
464
- ...props.schema,
465
- minItems: undefined,
466
- maxItems: 0,
467
- items: {},
468
- };
469
- return {
470
- ...props.schema,
471
- items: items,
472
- };
473
- }
474
- return props.schema;
475
- };
476
-
477
- const coverStation = (p: {
478
- prefix: string;
479
- components: OpenApi.IComponents;
480
- visited: Map<OpenApi.IJsonSchema, Map<OpenApi.IJsonSchema, boolean>>;
481
- x: OpenApi.IJsonSchema;
482
- y: OpenApi.IJsonSchema;
483
- }): boolean => {
484
- const cache: boolean | undefined = p.visited.get(p.x)?.get(p.y);
485
- if (cache !== undefined) return cache;
486
-
487
- // FOR RECURSIVE CASE
488
- const nested: Map<OpenApi.IJsonSchema, boolean> = MapUtil.take(
489
- p.visited,
490
- p.x,
491
- () => new Map(),
492
- );
493
- nested.set(p.y, true);
494
-
495
- // COMPUTE IT
496
- const result: boolean = coverSchema(p);
497
- nested.set(p.y, result);
498
- return result;
499
- };
500
-
501
- const coverSchema = (p: {
502
- prefix: string;
503
- components: OpenApi.IComponents;
504
- visited: Map<OpenApi.IJsonSchema, Map<OpenApi.IJsonSchema, boolean>>;
505
- x: OpenApi.IJsonSchema;
506
- y: OpenApi.IJsonSchema;
507
- }): boolean => {
508
- // CHECK EQUALITY
509
- if (p.x === p.y) return true;
510
- else if (isReference(p.x) && isReference(p.y) && p.x.$ref === p.y.$ref)
511
- return true;
512
-
513
- // COMPARE WITH FLATTENING
514
- const alpha: OpenApi.IJsonSchema[] = flatSchema({
515
- prefix: p.prefix,
516
- components: p.components,
517
- schema: p.x,
518
- });
519
- const beta: OpenApi.IJsonSchema[] = flatSchema({
520
- prefix: p.prefix,
521
- components: p.components,
522
- schema: p.y,
523
- });
524
- if (alpha.some((x) => isUnknown(x))) return true;
525
- else if (beta.some((x) => isUnknown(x))) return false;
526
- return beta.every((b) =>
527
- alpha.some((a) =>
528
- coverEscapedSchema({
529
- prefix: p.prefix,
530
- components: p.components,
531
- visited: p.visited,
532
- x: a,
533
- y: b,
534
- }),
535
- ),
536
- );
537
- };
538
-
539
- const coverEscapedSchema = (p: {
540
- prefix: string;
541
- components: OpenApi.IComponents;
542
- visited: Map<OpenApi.IJsonSchema, Map<OpenApi.IJsonSchema, boolean>>;
543
- x: OpenApi.IJsonSchema;
544
- y: OpenApi.IJsonSchema;
545
- }): boolean => {
546
- // CHECK EQUALITY
547
- if (p.x === p.y) return true;
548
- else if (isUnknown(p.x)) return true;
549
- else if (isUnknown(p.y)) return false;
550
- else if (isNull(p.x)) return isNull(p.y);
551
- // ATOMIC CASE
552
- else if (isConstant(p.x)) return isConstant(p.y) && p.x.const === p.y.const;
553
- else if (isBoolean(p.x))
554
- return (
555
- isBoolean(p.y) || (isConstant(p.y) && typeof p.y.const === "boolean")
556
- );
557
- else if (isInteger(p.x))
558
- return (isInteger(p.y) || isConstant(p.y)) && coverInteger(p.x, p.y);
559
- else if (isNumber(p.x))
560
- return (
561
- (isConstant(p.y) || isInteger(p.y) || isNumber(p.y)) &&
562
- coverNumber(p.x, p.y)
563
- );
564
- else if (isString(p.x))
565
- return (isConstant(p.y) || isString(p.y)) && coverString(p.x, p.y);
566
- // INSTANCE CASE
567
- else if (isArray(p.x))
568
- return (
569
- (isArray(p.y) || isTuple(p.y)) &&
570
- coverArray({
571
- prefix: p.prefix,
572
- components: p.components,
573
- visited: p.visited,
574
- x: p.x,
575
- y: p.y,
576
- })
577
- );
578
- else if (isObject(p.x))
579
- return (
580
- isObject(p.y) &&
581
- coverObject({
582
- prefix: p.prefix,
583
- components: p.components,
584
- visited: p.visited,
585
- x: p.x,
586
- y: p.y,
587
- })
588
- );
589
- else if (isReference(p.x)) return isReference(p.y) && p.x.$ref === p.y.$ref;
590
- return false;
591
- };
592
-
593
- const coverArray = (p: {
594
- prefix: string;
595
- components: OpenApi.IComponents;
596
- visited: Map<OpenApi.IJsonSchema, Map<OpenApi.IJsonSchema, boolean>>;
597
- x: OpenApi.IJsonSchema.IArray;
598
- y: OpenApi.IJsonSchema.IArray | OpenApi.IJsonSchema.ITuple;
599
- }): boolean => {
600
- if (isTuple(p.y))
601
- return (
602
- p.y.prefixItems.every((v) =>
603
- coverStation({
604
- prefix: p.prefix,
605
- components: p.components,
606
- visited: p.visited,
607
- x: p.x.items,
608
- y: v,
609
- }),
610
- ) &&
611
- (p.y.additionalItems === undefined ||
612
- (typeof p.y.additionalItems === "object" &&
613
- coverStation({
614
- prefix: p.prefix,
615
- components: p.components,
616
- visited: p.visited,
617
- x: p.x.items,
618
- y: p.y.additionalItems,
619
- })))
620
- );
621
- else if (
622
- !(
623
- p.x.minItems === undefined ||
624
- (p.y.minItems !== undefined && p.x.minItems <= p.y.minItems)
625
- )
626
- )
627
- return false;
628
- else if (
629
- !(
630
- p.x.maxItems === undefined ||
631
- (p.y.maxItems !== undefined && p.x.maxItems >= p.y.maxItems)
632
- )
633
- )
634
- return false;
635
- return coverStation({
636
- prefix: p.prefix,
637
- components: p.components,
638
- visited: p.visited,
639
- x: p.x.items,
640
- y: p.y.items,
641
- });
642
- };
643
-
644
- const coverObject = (p: {
645
- prefix: string;
646
- components: OpenApi.IComponents;
647
- visited: Map<OpenApi.IJsonSchema, Map<OpenApi.IJsonSchema, boolean>>;
648
- x: OpenApi.IJsonSchema.IObject;
649
- y: OpenApi.IJsonSchema.IObject;
650
- }): boolean => {
651
- if (!p.x.additionalProperties && !!p.y.additionalProperties) return false;
652
- else if (
653
- !!p.x.additionalProperties &&
654
- !!p.y.additionalProperties &&
655
- ((typeof p.x.additionalProperties === "object" &&
656
- p.y.additionalProperties === true) ||
657
- (typeof p.x.additionalProperties === "object" &&
658
- typeof p.y.additionalProperties === "object" &&
659
- !coverStation({
660
- prefix: p.prefix,
661
- components: p.components,
662
- visited: p.visited,
663
- x: p.x.additionalProperties,
664
- y: p.y.additionalProperties,
665
- })))
666
- )
667
- return false;
668
- return Object.entries(p.y.properties ?? {}).every(([key, b]) => {
669
- const a: OpenApi.IJsonSchema | undefined = p.x.properties?.[key];
670
- if (a === undefined) return false;
671
- else if (
672
- p.x.required?.includes(key) === true &&
673
- (p.y.required?.includes(key) ?? false) === false
674
- )
675
- return false;
676
- return coverStation({
677
- prefix: p.prefix,
678
- components: p.components,
679
- visited: p.visited,
680
- x: a,
681
- y: b,
682
- });
683
- });
684
- };
685
-
686
- export const coverInteger = (
687
- x: OpenApi.IJsonSchema.IInteger,
688
- y: OpenApi.IJsonSchema.IConstant | OpenApi.IJsonSchema.IInteger,
689
- ): boolean => {
690
- if (isConstant(y))
691
- return typeof y.const === "number" && Number.isInteger(y.const);
692
- return x.type === y.type && coverNumericRange(x, y);
693
- };
694
-
695
- export const coverNumber = (
696
- x: OpenApi.IJsonSchema.INumber,
697
- y:
698
- | OpenApi.IJsonSchema.IConstant
699
- | OpenApi.IJsonSchema.IInteger
700
- | OpenApi.IJsonSchema.INumber,
701
- ): boolean => {
702
- if (isConstant(y)) return typeof y.const === "number";
703
- return (
704
- (x.type === y.type || (x.type === "number" && y.type === "integer")) &&
705
- coverNumericRange(x, y)
706
- );
707
- };
708
-
709
- export const coverString = (
710
- x: OpenApi.IJsonSchema.IString,
711
- y: OpenApi.IJsonSchema.IConstant | OpenApi.IJsonSchema.IString,
712
- ): boolean => {
713
- if (isConstant(y)) return typeof y.const === "string";
714
- return [
715
- x.format === undefined ||
716
- (y.format !== undefined && coverFormat(x.format, y.format)),
717
- x.pattern === undefined || x.pattern === y.pattern,
718
- x.minLength === undefined ||
719
- (y.minLength !== undefined && x.minLength <= y.minLength),
720
- x.maxLength === undefined ||
721
- (y.maxLength !== undefined && x.maxLength >= y.maxLength),
722
- ].every((v) => v);
723
- };
724
-
725
- const coverFormat = (
726
- x: Required<OpenApi.IJsonSchema.IString>["format"],
727
- y: Required<OpenApi.IJsonSchema.IString>["format"],
728
- ): boolean =>
729
- x === y ||
730
- (x === "idn-email" && y === "email") ||
731
- (x === "idn-hostname" && y === "hostname") ||
732
- (["uri", "iri"].includes(x) && y === "url") ||
733
- (x === "iri" && y === "uri") ||
734
- (x === "iri-reference" && y === "uri-reference");
735
-
736
- const flatSchema = (props: {
737
- prefix: string;
738
- components: OpenApi.IComponents;
739
- schema: OpenApi.IJsonSchema;
740
- }): OpenApi.IJsonSchema[] => {
741
- const schema = escapeReferenceOfFlatSchema(props);
742
- if (isOneOf(schema))
743
- return schema.oneOf
744
- .map((v) =>
745
- flatSchema({
746
- prefix: props.prefix,
747
- components: props.components,
748
- schema: v,
749
- }),
750
- )
751
- .flat();
752
- return [schema];
753
- };
754
-
755
- const escapeReferenceOfFlatSchema = (props: {
756
- prefix: string;
757
- components: OpenApi.IComponents;
758
- schema: OpenApi.IJsonSchema;
759
- }): Exclude<OpenApi.IJsonSchema, OpenApi.IJsonSchema.IReference> => {
760
- if (isReference(props.schema) === false) return props.schema;
761
- const key = props.schema.$ref.replace(props.prefix, "");
762
- const found: OpenApi.IJsonSchema | undefined = escapeReferenceOfFlatSchema({
763
- prefix: props.prefix,
764
- components: props.components,
765
- schema: props.components.schemas?.[key] ?? {},
766
- });
767
- if (found === undefined)
768
- throw new Error(
769
- `Reference type not found: ${JSON.stringify(props.schema.$ref)}`,
770
- );
771
- return escapeReferenceOfFlatSchema({
772
- prefix: props.prefix,
773
- components: props.components,
774
- schema: found,
775
- });
776
- };
777
-
778
- export const coverNumericRange = (
779
- x: Pick<
780
- OpenApi.IJsonSchema.INumber,
781
- | "minimum"
782
- | "maximum"
783
- | "exclusiveMinimum"
784
- | "exclusiveMaximum"
785
- | "multipleOf"
786
- >,
787
- y: Pick<
788
- OpenApi.IJsonSchema.INumber,
789
- | "minimum"
790
- | "maximum"
791
- | "exclusiveMinimum"
792
- | "exclusiveMaximum"
793
- | "multipleOf"
794
- >,
795
- ): boolean =>
796
- [
797
- x.minimum === undefined ||
798
- (y.minimum !== undefined && x.minimum <= y.minimum) ||
799
- (y.exclusiveMinimum !== undefined && x.minimum < y.exclusiveMinimum),
800
- x.maximum === undefined ||
801
- (y.maximum !== undefined && x.maximum >= y.maximum) ||
802
- (y.exclusiveMaximum !== undefined && x.maximum > y.exclusiveMaximum),
803
- x.exclusiveMinimum === undefined ||
804
- (y.minimum !== undefined && x.exclusiveMinimum <= y.minimum) ||
805
- (y.exclusiveMinimum !== undefined &&
806
- x.exclusiveMinimum <= y.exclusiveMinimum),
807
- x.exclusiveMaximum === undefined ||
808
- (y.maximum !== undefined && x.exclusiveMaximum >= y.maximum) ||
809
- (y.exclusiveMaximum !== undefined &&
810
- x.exclusiveMaximum >= y.exclusiveMaximum),
811
- x.multipleOf === undefined ||
812
- (y.multipleOf !== undefined &&
813
- y.multipleOf / x.multipleOf ===
814
- Math.floor(y.multipleOf / x.multipleOf)),
815
- ].every((v) => v);
816
- }
817
-
818
- const getReference = (prefix: string): string =>
819
- prefix
820
- .split("/")
821
- .filter((str, i) => !!str.length && !(i === 0 && str === "#"))
822
- .join(".");
1
+ import { IJsonSchemaTransformError, IResult, OpenApi } from "@typia/interface";
2
+
3
+ import { MapUtil } from "../MapUtil";
4
+ import { JsonDescriptor } from "./JsonDescriptor";
5
+
6
+ /** @internal */
7
+ export namespace OpenApiTypeCheckerBase {
8
+ /* -----------------------------------------------------------
9
+ TYPE CHECKERS
10
+ ----------------------------------------------------------- */
11
+ export const isNull = (
12
+ schema: OpenApi.IJsonSchema,
13
+ ): schema is OpenApi.IJsonSchema.INull =>
14
+ (schema as OpenApi.IJsonSchema.INull).type === "null";
15
+
16
+ export const isUnknown = (
17
+ schema: OpenApi.IJsonSchema,
18
+ ): schema is OpenApi.IJsonSchema.IUnknown =>
19
+ (schema as OpenApi.IJsonSchema.IUnknown).type === undefined &&
20
+ !isConstant(schema) &&
21
+ !isOneOf(schema) &&
22
+ !isReference(schema);
23
+
24
+ export const isConstant = (
25
+ schema: OpenApi.IJsonSchema,
26
+ ): schema is OpenApi.IJsonSchema.IConstant =>
27
+ (schema as OpenApi.IJsonSchema.IConstant).const !== undefined;
28
+
29
+ export const isBoolean = (
30
+ schema: OpenApi.IJsonSchema,
31
+ ): schema is OpenApi.IJsonSchema.IBoolean =>
32
+ (schema as OpenApi.IJsonSchema.IBoolean).type === "boolean";
33
+
34
+ export const isInteger = (
35
+ schema: OpenApi.IJsonSchema,
36
+ ): schema is OpenApi.IJsonSchema.IInteger =>
37
+ (schema as OpenApi.IJsonSchema.IInteger).type === "integer";
38
+
39
+ export const isNumber = (
40
+ schema: OpenApi.IJsonSchema,
41
+ ): schema is OpenApi.IJsonSchema.INumber =>
42
+ (schema as OpenApi.IJsonSchema.INumber).type === "number";
43
+
44
+ export const isString = (
45
+ schema: OpenApi.IJsonSchema,
46
+ ): schema is OpenApi.IJsonSchema.IString =>
47
+ (schema as OpenApi.IJsonSchema.IString).type === "string";
48
+
49
+ export const isArray = (
50
+ schema: OpenApi.IJsonSchema,
51
+ ): schema is OpenApi.IJsonSchema.IArray =>
52
+ (schema as OpenApi.IJsonSchema.IArray).type === "array" &&
53
+ (schema as OpenApi.IJsonSchema.IArray).items !== undefined;
54
+
55
+ export const isTuple = (
56
+ schema: OpenApi.IJsonSchema,
57
+ ): schema is OpenApi.IJsonSchema.ITuple =>
58
+ (schema as OpenApi.IJsonSchema.ITuple).type === "array" &&
59
+ (schema as OpenApi.IJsonSchema.ITuple).prefixItems !== undefined;
60
+
61
+ export const isObject = (
62
+ schema: OpenApi.IJsonSchema,
63
+ ): schema is OpenApi.IJsonSchema.IObject =>
64
+ (schema as OpenApi.IJsonSchema.IObject).type === "object";
65
+
66
+ export const isReference = (
67
+ schema: OpenApi.IJsonSchema,
68
+ ): schema is OpenApi.IJsonSchema.IReference =>
69
+ (schema as any).$ref !== undefined;
70
+
71
+ export const isOneOf = (
72
+ schema: OpenApi.IJsonSchema,
73
+ ): schema is OpenApi.IJsonSchema.IOneOf =>
74
+ (schema as OpenApi.IJsonSchema.IOneOf).oneOf !== undefined;
75
+
76
+ export const isRecursiveReference = (props: {
77
+ prefix: string;
78
+ components: OpenApi.IComponents;
79
+ schema: OpenApi.IJsonSchema;
80
+ }): boolean => {
81
+ if (isReference(props.schema) === false) return false;
82
+ const current: string =
83
+ props.schema.$ref.split(props.prefix)[1] ??
84
+ props.schema.$ref.split("/").at(-1)!;
85
+ let counter: number = 0;
86
+ visit({
87
+ prefix: props.prefix,
88
+ components: props.components,
89
+ schema: props.schema,
90
+ closure: (schema) => {
91
+ if (isReference(schema)) {
92
+ const next: string =
93
+ schema.$ref.split(props.prefix)[1] ??
94
+ schema.$ref.split("/").at(-1)!;
95
+ if (current === next) ++counter;
96
+ }
97
+ },
98
+ });
99
+ return counter > 1;
100
+ };
101
+
102
+ /* -----------------------------------------------------------
103
+ OPERATORS
104
+ ----------------------------------------------------------- */
105
+ export const unreference = (props: {
106
+ prefix: string;
107
+ method: string;
108
+ components: OpenApi.IComponents;
109
+ schema: OpenApi.IJsonSchema;
110
+ accessor?: string;
111
+ refAccessor?: string;
112
+ }): IResult<OpenApi.IJsonSchema, IJsonSchemaTransformError> => {
113
+ const reasons: IJsonSchemaTransformError.IReason[] = [];
114
+ const result: OpenApi.IJsonSchema | null = unreferenceSchema({
115
+ prefix: props.prefix,
116
+ refAccessor:
117
+ props.refAccessor ??
118
+ `$input.${props.prefix
119
+ .substring(2)
120
+ .split("/")
121
+ .filter((s) => !!s.length)
122
+ .join(".")}`,
123
+ accessor: props.accessor ?? "$input.schema",
124
+ components: props.components,
125
+ schema: props.schema,
126
+ reasons,
127
+ });
128
+ if (result === null)
129
+ return {
130
+ success: false,
131
+ error: {
132
+ method: props.method,
133
+ message: `failed to unreference due to unable to find.`,
134
+ reasons,
135
+ },
136
+ };
137
+ return {
138
+ success: true,
139
+ value: result,
140
+ };
141
+ };
142
+
143
+ export const escape = (props: {
144
+ prefix: string;
145
+ method: string;
146
+ components: OpenApi.IComponents;
147
+ schema: OpenApi.IJsonSchema;
148
+ recursive: false | number;
149
+ accessor?: string;
150
+ refAccessor?: string;
151
+ }): IResult<OpenApi.IJsonSchema, IJsonSchemaTransformError> => {
152
+ const reasons: IJsonSchemaTransformError.IReason[] = [];
153
+ const result: OpenApi.IJsonSchema | null =
154
+ escapeSchema({
155
+ ...props,
156
+ reasons,
157
+ visited: new Map(),
158
+ accessor: props.accessor ?? "$input.schema",
159
+ refAccessor: props.refAccessor ?? getReference(props.prefix),
160
+ }) || null;
161
+ if (result === null)
162
+ return {
163
+ success: false,
164
+ error: {
165
+ method: props.method,
166
+ message: `failed to escape some reference type(s) due to unable to find${Number(props.recursive) === 0 ? " or recursive relationship" : ""}.`,
167
+ reasons,
168
+ },
169
+ };
170
+ return {
171
+ success: true,
172
+ value: result,
173
+ };
174
+ };
175
+
176
+ export const visit = (props: {
177
+ prefix: string;
178
+ closure: (schema: OpenApi.IJsonSchema, accessor: string) => void;
179
+ components: OpenApi.IComponents;
180
+ schema: OpenApi.IJsonSchema;
181
+ accessor?: string;
182
+ refAccessor?: string;
183
+ }): void => {
184
+ const already: Set<string> = new Set();
185
+ const refAccessor: string =
186
+ props.refAccessor ?? `$input.${getReference(props.prefix)}`;
187
+ const next = (schema: OpenApi.IJsonSchema, accessor: string): void => {
188
+ props.closure(schema, accessor);
189
+ if (isReference(schema)) {
190
+ const key: string = schema.$ref.split(props.prefix).pop()!;
191
+ if (already.has(key) === true) return;
192
+ already.add(key);
193
+ const found: OpenApi.IJsonSchema | undefined =
194
+ props.components.schemas?.[key];
195
+ if (found !== undefined)
196
+ next(found, `${refAccessor}[${JSON.stringify(key)}]`);
197
+ } else if (isOneOf(schema))
198
+ schema.oneOf.forEach((s, i) => next(s, `${accessor}.oneOf[${i}]`));
199
+ else if (isObject(schema)) {
200
+ for (const [key, value] of Object.entries(schema.properties ?? {}))
201
+ next(value, `${accessor}.properties[${JSON.stringify(key)}]`);
202
+ if (
203
+ typeof schema.additionalProperties === "object" &&
204
+ schema.additionalProperties !== null
205
+ )
206
+ next(schema.additionalProperties, `${accessor}.additionalProperties`);
207
+ } else if (isArray(schema)) next(schema.items, `${accessor}.items`);
208
+ else if (isTuple(schema)) {
209
+ (schema.prefixItems ?? []).forEach((s, i) =>
210
+ next(s, `${accessor}.prefixItems[${i}]`),
211
+ );
212
+ if (
213
+ typeof schema.additionalItems === "object" &&
214
+ schema.additionalItems !== null
215
+ )
216
+ next(schema.additionalItems, `${accessor}.additionalItems`);
217
+ }
218
+ };
219
+ next(props.schema, props.accessor ?? "$input.schema");
220
+ };
221
+
222
+ export const covers = (props: {
223
+ prefix: string;
224
+ components: OpenApi.IComponents;
225
+ x: OpenApi.IJsonSchema;
226
+ y: OpenApi.IJsonSchema;
227
+ }): boolean =>
228
+ coverStation({
229
+ prefix: props.prefix,
230
+ components: props.components,
231
+ x: props.x,
232
+ y: props.y,
233
+ visited: new Map(),
234
+ });
235
+
236
+ const unreferenceSchema = (props: {
237
+ prefix: string;
238
+ refAccessor: string;
239
+ accessor: string;
240
+ components: OpenApi.IComponents;
241
+ schema: OpenApi.IJsonSchema;
242
+ reasons: IJsonSchemaTransformError.IReason[];
243
+ first?: string;
244
+ }): OpenApi.IJsonSchema | null => {
245
+ if (isReference(props.schema) === false) return props.schema;
246
+ const key: string = props.schema.$ref.split(props.prefix).pop()!;
247
+ const found: OpenApi.IJsonSchema | undefined =
248
+ props.components.schemas?.[key];
249
+ if (found === undefined) {
250
+ props.reasons.push({
251
+ schema: props.schema,
252
+ accessor: props.accessor,
253
+ message: `unable to find reference type ${JSON.stringify(key)}.`,
254
+ });
255
+ return null;
256
+ } else if (isReference(found) === false) return found;
257
+ else if (props.first === key) {
258
+ props.reasons.push({
259
+ schema: props.schema,
260
+ accessor: props.accessor,
261
+ message: `recursive reference type ${JSON.stringify(key)}.`,
262
+ });
263
+ return null;
264
+ }
265
+ return unreferenceSchema({
266
+ ...props,
267
+ accessor: `${props.refAccessor}[${JSON.stringify(key)}]`,
268
+ first: key,
269
+ });
270
+ };
271
+
272
+ const escapeSchema = (props: {
273
+ components: OpenApi.IComponents;
274
+ prefix: string;
275
+ schema: OpenApi.IJsonSchema;
276
+ recursive: false | number;
277
+ visited: Map<string, number>;
278
+ reasons: IJsonSchemaTransformError.IReason[];
279
+ accessor: string;
280
+ refAccessor: string;
281
+ }): OpenApi.IJsonSchema | null | undefined => {
282
+ if (isReference(props.schema)) {
283
+ // REFERENCE
284
+ const key: string =
285
+ props.schema.$ref.split(props.prefix)[1] ??
286
+ props.schema.$ref.split("/").at(-1)!;
287
+ const target: OpenApi.IJsonSchema | undefined =
288
+ props.components.schemas?.[key];
289
+ if (target === undefined) {
290
+ props.reasons.push({
291
+ schema: props.schema,
292
+ accessor: props.accessor,
293
+ message: `unable to find reference type ${JSON.stringify(key)}.`,
294
+ });
295
+ return null;
296
+ } else if (props.visited.has(key) === true) {
297
+ if (props.recursive === false) return null;
298
+ const depth: number = props.visited.get(key)!;
299
+ if (depth > props.recursive) {
300
+ if (props.recursive === 0) {
301
+ props.reasons.push({
302
+ schema: props.schema,
303
+ accessor: props.accessor,
304
+ message: `recursive reference type ${JSON.stringify(key)}.`,
305
+ });
306
+ return null;
307
+ }
308
+ return undefined;
309
+ }
310
+ props.visited.set(key, depth + 1);
311
+ const res: OpenApi.IJsonSchema | null | undefined = escapeSchema({
312
+ ...props,
313
+ schema: target,
314
+ accessor: `${props.refAccessor}[${JSON.stringify(key)}]`,
315
+ });
316
+ return res
317
+ ? {
318
+ ...res,
319
+ description: JsonDescriptor.cascade({
320
+ prefix: props.prefix,
321
+ components: props.components,
322
+ schema: props.schema,
323
+ escape: true,
324
+ }),
325
+ }
326
+ : res;
327
+ } else {
328
+ const res: OpenApi.IJsonSchema | null | undefined = escapeSchema({
329
+ ...props,
330
+ schema: target,
331
+ accessor: `${props.refAccessor}[${JSON.stringify(key)}]`,
332
+ visited: new Map([...props.visited, [key, 1]]),
333
+ });
334
+ return res
335
+ ? {
336
+ ...res,
337
+ description: JsonDescriptor.cascade({
338
+ prefix: props.prefix,
339
+ components: props.components,
340
+ schema: props.schema,
341
+ escape: true,
342
+ }),
343
+ }
344
+ : res;
345
+ }
346
+ } else if (isOneOf(props.schema)) {
347
+ // UNION
348
+ const elements: Array<OpenApi.IJsonSchema | null | undefined> =
349
+ props.schema.oneOf.map((s, i) =>
350
+ escapeSchema({
351
+ ...props,
352
+ schema: s,
353
+ accessor: `${props.accessor}.oneOf[${i}]`,
354
+ }),
355
+ );
356
+ if (elements.some((v) => v === null)) return null;
357
+ const filtered: OpenApi.IJsonSchema[] = elements.filter(
358
+ (v) => v !== undefined,
359
+ ) as OpenApi.IJsonSchema[];
360
+ if (filtered.length === 0) return undefined;
361
+ return {
362
+ ...props.schema,
363
+ oneOf: filtered
364
+ .map((v) =>
365
+ flatSchema({
366
+ prefix: props.prefix,
367
+ components: props.components,
368
+ schema: v,
369
+ }),
370
+ )
371
+ .flat(),
372
+ };
373
+ } else if (isObject(props.schema)) {
374
+ // OBJECT
375
+ const object: OpenApi.IJsonSchema.IObject = props.schema;
376
+ const properties: Array<
377
+ [string, OpenApi.IJsonSchema | null | undefined]
378
+ > = Object.entries(object.properties ?? {}).map(([k, s]) => [
379
+ k,
380
+ escapeSchema({
381
+ ...props,
382
+ schema: s,
383
+ visited: props.visited,
384
+ accessor: `${props.accessor}.properties[${JSON.stringify(k)}]`,
385
+ }),
386
+ ]);
387
+ const additionalProperties:
388
+ | OpenApi.IJsonSchema
389
+ | null
390
+ | boolean
391
+ | undefined = object.additionalProperties
392
+ ? typeof object.additionalProperties === "object" &&
393
+ object.additionalProperties !== null
394
+ ? escapeSchema({
395
+ ...props,
396
+ schema: object.additionalProperties,
397
+ accessor: `${props.accessor}.additionalProperties`,
398
+ })
399
+ : object.additionalProperties
400
+ : false;
401
+ if (
402
+ properties.some(([_k, v]) => v === null) ||
403
+ additionalProperties === null
404
+ )
405
+ return null;
406
+ else if (
407
+ properties.some(
408
+ ([k, v]) => v === undefined && object.required?.includes(k) === true,
409
+ ) === true
410
+ )
411
+ return undefined;
412
+ return {
413
+ ...object,
414
+ properties: Object.fromEntries(
415
+ properties.filter(([_k, v]) => v !== undefined) as Array<
416
+ [string, OpenApi.IJsonSchema]
417
+ >,
418
+ ),
419
+ additionalProperties: additionalProperties ?? false,
420
+ required:
421
+ object.required?.filter((k) =>
422
+ properties.some(([key, value]) => key === k && value !== undefined),
423
+ ) ?? [],
424
+ };
425
+ } else if (isTuple(props.schema)) {
426
+ // TUPLE
427
+ const elements: Array<OpenApi.IJsonSchema | null | undefined> =
428
+ props.schema.prefixItems.map((s, i) =>
429
+ escapeSchema({
430
+ ...props,
431
+ schema: s,
432
+ accessor: `${props.accessor}.prefixItems[${i}]`,
433
+ }),
434
+ );
435
+ const additionalItems: OpenApi.IJsonSchema | null | boolean | undefined =
436
+ props.schema.additionalItems
437
+ ? typeof props.schema.additionalItems === "object" &&
438
+ props.schema.additionalItems !== null
439
+ ? escapeSchema({
440
+ ...props,
441
+ schema: props.schema.additionalItems,
442
+ accessor: `${props.accessor}.additionalItems`,
443
+ })
444
+ : props.schema.additionalItems
445
+ : false;
446
+ if (elements.some((v) => v === null) || additionalItems === null)
447
+ return null;
448
+ else if (elements.some((v) => v === undefined)) return undefined;
449
+ return {
450
+ ...props.schema,
451
+ prefixItems: elements as OpenApi.IJsonSchema[],
452
+ additionalItems: additionalItems ?? false,
453
+ };
454
+ } else if (isArray(props.schema)) {
455
+ // ARRAY
456
+ const items: OpenApi.IJsonSchema | null | undefined = escapeSchema({
457
+ ...props,
458
+ schema: props.schema.items,
459
+ accessor: `${props.accessor}.items`,
460
+ });
461
+ if (items === null) return null;
462
+ else if (items === undefined)
463
+ return {
464
+ ...props.schema,
465
+ minItems: undefined,
466
+ maxItems: 0,
467
+ items: {},
468
+ };
469
+ return {
470
+ ...props.schema,
471
+ items: items,
472
+ };
473
+ }
474
+ return props.schema;
475
+ };
476
+
477
+ const coverStation = (p: {
478
+ prefix: string;
479
+ components: OpenApi.IComponents;
480
+ visited: Map<OpenApi.IJsonSchema, Map<OpenApi.IJsonSchema, boolean>>;
481
+ x: OpenApi.IJsonSchema;
482
+ y: OpenApi.IJsonSchema;
483
+ }): boolean => {
484
+ const cache: boolean | undefined = p.visited.get(p.x)?.get(p.y);
485
+ if (cache !== undefined) return cache;
486
+
487
+ // FOR RECURSIVE CASE
488
+ const nested: Map<OpenApi.IJsonSchema, boolean> = MapUtil.take(
489
+ p.visited,
490
+ p.x,
491
+ () => new Map(),
492
+ );
493
+ nested.set(p.y, true);
494
+
495
+ // COMPUTE IT
496
+ const result: boolean = coverSchema(p);
497
+ nested.set(p.y, result);
498
+ return result;
499
+ };
500
+
501
+ const coverSchema = (p: {
502
+ prefix: string;
503
+ components: OpenApi.IComponents;
504
+ visited: Map<OpenApi.IJsonSchema, Map<OpenApi.IJsonSchema, boolean>>;
505
+ x: OpenApi.IJsonSchema;
506
+ y: OpenApi.IJsonSchema;
507
+ }): boolean => {
508
+ // CHECK EQUALITY
509
+ if (p.x === p.y) return true;
510
+ else if (isReference(p.x) && isReference(p.y) && p.x.$ref === p.y.$ref)
511
+ return true;
512
+
513
+ // COMPARE WITH FLATTENING
514
+ const alpha: OpenApi.IJsonSchema[] = flatSchema({
515
+ prefix: p.prefix,
516
+ components: p.components,
517
+ schema: p.x,
518
+ });
519
+ const beta: OpenApi.IJsonSchema[] = flatSchema({
520
+ prefix: p.prefix,
521
+ components: p.components,
522
+ schema: p.y,
523
+ });
524
+ if (alpha.some((x) => isUnknown(x))) return true;
525
+ else if (beta.some((x) => isUnknown(x))) return false;
526
+ return beta.every((b) =>
527
+ alpha.some((a) =>
528
+ coverEscapedSchema({
529
+ prefix: p.prefix,
530
+ components: p.components,
531
+ visited: p.visited,
532
+ x: a,
533
+ y: b,
534
+ }),
535
+ ),
536
+ );
537
+ };
538
+
539
+ const coverEscapedSchema = (p: {
540
+ prefix: string;
541
+ components: OpenApi.IComponents;
542
+ visited: Map<OpenApi.IJsonSchema, Map<OpenApi.IJsonSchema, boolean>>;
543
+ x: OpenApi.IJsonSchema;
544
+ y: OpenApi.IJsonSchema;
545
+ }): boolean => {
546
+ // CHECK EQUALITY
547
+ if (p.x === p.y) return true;
548
+ else if (isUnknown(p.x)) return true;
549
+ else if (isUnknown(p.y)) return false;
550
+ else if (isNull(p.x)) return isNull(p.y);
551
+ // ATOMIC CASE
552
+ else if (isConstant(p.x)) return isConstant(p.y) && p.x.const === p.y.const;
553
+ else if (isBoolean(p.x))
554
+ return (
555
+ isBoolean(p.y) || (isConstant(p.y) && typeof p.y.const === "boolean")
556
+ );
557
+ else if (isInteger(p.x))
558
+ return (isInteger(p.y) || isConstant(p.y)) && coverInteger(p.x, p.y);
559
+ else if (isNumber(p.x))
560
+ return (
561
+ (isConstant(p.y) || isInteger(p.y) || isNumber(p.y)) &&
562
+ coverNumber(p.x, p.y)
563
+ );
564
+ else if (isString(p.x))
565
+ return (isConstant(p.y) || isString(p.y)) && coverString(p.x, p.y);
566
+ // INSTANCE CASE
567
+ else if (isArray(p.x))
568
+ return (
569
+ (isArray(p.y) || isTuple(p.y)) &&
570
+ coverArray({
571
+ prefix: p.prefix,
572
+ components: p.components,
573
+ visited: p.visited,
574
+ x: p.x,
575
+ y: p.y,
576
+ })
577
+ );
578
+ else if (isObject(p.x))
579
+ return (
580
+ isObject(p.y) &&
581
+ coverObject({
582
+ prefix: p.prefix,
583
+ components: p.components,
584
+ visited: p.visited,
585
+ x: p.x,
586
+ y: p.y,
587
+ })
588
+ );
589
+ else if (isReference(p.x)) return isReference(p.y) && p.x.$ref === p.y.$ref;
590
+ return false;
591
+ };
592
+
593
+ const coverArray = (p: {
594
+ prefix: string;
595
+ components: OpenApi.IComponents;
596
+ visited: Map<OpenApi.IJsonSchema, Map<OpenApi.IJsonSchema, boolean>>;
597
+ x: OpenApi.IJsonSchema.IArray;
598
+ y: OpenApi.IJsonSchema.IArray | OpenApi.IJsonSchema.ITuple;
599
+ }): boolean => {
600
+ if (isTuple(p.y))
601
+ return (
602
+ p.y.prefixItems.every((v) =>
603
+ coverStation({
604
+ prefix: p.prefix,
605
+ components: p.components,
606
+ visited: p.visited,
607
+ x: p.x.items,
608
+ y: v,
609
+ }),
610
+ ) &&
611
+ (p.y.additionalItems === undefined ||
612
+ (typeof p.y.additionalItems === "object" &&
613
+ coverStation({
614
+ prefix: p.prefix,
615
+ components: p.components,
616
+ visited: p.visited,
617
+ x: p.x.items,
618
+ y: p.y.additionalItems,
619
+ })))
620
+ );
621
+ else if (
622
+ !(
623
+ p.x.minItems === undefined ||
624
+ (p.y.minItems !== undefined && p.x.minItems <= p.y.minItems)
625
+ )
626
+ )
627
+ return false;
628
+ else if (
629
+ !(
630
+ p.x.maxItems === undefined ||
631
+ (p.y.maxItems !== undefined && p.x.maxItems >= p.y.maxItems)
632
+ )
633
+ )
634
+ return false;
635
+ return coverStation({
636
+ prefix: p.prefix,
637
+ components: p.components,
638
+ visited: p.visited,
639
+ x: p.x.items,
640
+ y: p.y.items,
641
+ });
642
+ };
643
+
644
+ const coverObject = (p: {
645
+ prefix: string;
646
+ components: OpenApi.IComponents;
647
+ visited: Map<OpenApi.IJsonSchema, Map<OpenApi.IJsonSchema, boolean>>;
648
+ x: OpenApi.IJsonSchema.IObject;
649
+ y: OpenApi.IJsonSchema.IObject;
650
+ }): boolean => {
651
+ if (!p.x.additionalProperties && !!p.y.additionalProperties) return false;
652
+ else if (
653
+ !!p.x.additionalProperties &&
654
+ !!p.y.additionalProperties &&
655
+ ((typeof p.x.additionalProperties === "object" &&
656
+ p.y.additionalProperties === true) ||
657
+ (typeof p.x.additionalProperties === "object" &&
658
+ typeof p.y.additionalProperties === "object" &&
659
+ !coverStation({
660
+ prefix: p.prefix,
661
+ components: p.components,
662
+ visited: p.visited,
663
+ x: p.x.additionalProperties,
664
+ y: p.y.additionalProperties,
665
+ })))
666
+ )
667
+ return false;
668
+ return Object.entries(p.y.properties ?? {}).every(([key, b]) => {
669
+ const a: OpenApi.IJsonSchema | undefined = p.x.properties?.[key];
670
+ if (a === undefined) return false;
671
+ else if (
672
+ p.x.required?.includes(key) === true &&
673
+ (p.y.required?.includes(key) ?? false) === false
674
+ )
675
+ return false;
676
+ return coverStation({
677
+ prefix: p.prefix,
678
+ components: p.components,
679
+ visited: p.visited,
680
+ x: a,
681
+ y: b,
682
+ });
683
+ });
684
+ };
685
+
686
+ export const coverInteger = (
687
+ x: OpenApi.IJsonSchema.IInteger,
688
+ y: OpenApi.IJsonSchema.IConstant | OpenApi.IJsonSchema.IInteger,
689
+ ): boolean => {
690
+ if (isConstant(y))
691
+ return typeof y.const === "number" && Number.isInteger(y.const);
692
+ return x.type === y.type && coverNumericRange(x, y);
693
+ };
694
+
695
+ export const coverNumber = (
696
+ x: OpenApi.IJsonSchema.INumber,
697
+ y:
698
+ | OpenApi.IJsonSchema.IConstant
699
+ | OpenApi.IJsonSchema.IInteger
700
+ | OpenApi.IJsonSchema.INumber,
701
+ ): boolean => {
702
+ if (isConstant(y)) return typeof y.const === "number";
703
+ return (
704
+ (x.type === y.type || (x.type === "number" && y.type === "integer")) &&
705
+ coverNumericRange(x, y)
706
+ );
707
+ };
708
+
709
+ export const coverString = (
710
+ x: OpenApi.IJsonSchema.IString,
711
+ y: OpenApi.IJsonSchema.IConstant | OpenApi.IJsonSchema.IString,
712
+ ): boolean => {
713
+ if (isConstant(y)) return typeof y.const === "string";
714
+ return [
715
+ x.format === undefined ||
716
+ (y.format !== undefined && coverFormat(x.format, y.format)),
717
+ x.pattern === undefined || x.pattern === y.pattern,
718
+ x.minLength === undefined ||
719
+ (y.minLength !== undefined && x.minLength <= y.minLength),
720
+ x.maxLength === undefined ||
721
+ (y.maxLength !== undefined && x.maxLength >= y.maxLength),
722
+ ].every((v) => v);
723
+ };
724
+
725
+ const coverFormat = (
726
+ x: Required<OpenApi.IJsonSchema.IString>["format"],
727
+ y: Required<OpenApi.IJsonSchema.IString>["format"],
728
+ ): boolean =>
729
+ x === y ||
730
+ (x === "idn-email" && y === "email") ||
731
+ (x === "idn-hostname" && y === "hostname") ||
732
+ (["uri", "iri"].includes(x) && y === "url") ||
733
+ (x === "iri" && y === "uri") ||
734
+ (x === "iri-reference" && y === "uri-reference");
735
+
736
+ const flatSchema = (props: {
737
+ prefix: string;
738
+ components: OpenApi.IComponents;
739
+ schema: OpenApi.IJsonSchema;
740
+ }): OpenApi.IJsonSchema[] => {
741
+ const schema = escapeReferenceOfFlatSchema(props);
742
+ if (isOneOf(schema))
743
+ return schema.oneOf
744
+ .map((v) =>
745
+ flatSchema({
746
+ prefix: props.prefix,
747
+ components: props.components,
748
+ schema: v,
749
+ }),
750
+ )
751
+ .flat();
752
+ return [schema];
753
+ };
754
+
755
+ const escapeReferenceOfFlatSchema = (props: {
756
+ prefix: string;
757
+ components: OpenApi.IComponents;
758
+ schema: OpenApi.IJsonSchema;
759
+ }): Exclude<OpenApi.IJsonSchema, OpenApi.IJsonSchema.IReference> => {
760
+ if (isReference(props.schema) === false) return props.schema;
761
+ const key = props.schema.$ref.replace(props.prefix, "");
762
+ const found: OpenApi.IJsonSchema | undefined = escapeReferenceOfFlatSchema({
763
+ prefix: props.prefix,
764
+ components: props.components,
765
+ schema: props.components.schemas?.[key] ?? {},
766
+ });
767
+ if (found === undefined)
768
+ throw new Error(
769
+ `Reference type not found: ${JSON.stringify(props.schema.$ref)}`,
770
+ );
771
+ return escapeReferenceOfFlatSchema({
772
+ prefix: props.prefix,
773
+ components: props.components,
774
+ schema: found,
775
+ });
776
+ };
777
+
778
+ export const coverNumericRange = (
779
+ x: Pick<
780
+ OpenApi.IJsonSchema.INumber,
781
+ | "minimum"
782
+ | "maximum"
783
+ | "exclusiveMinimum"
784
+ | "exclusiveMaximum"
785
+ | "multipleOf"
786
+ >,
787
+ y: Pick<
788
+ OpenApi.IJsonSchema.INumber,
789
+ | "minimum"
790
+ | "maximum"
791
+ | "exclusiveMinimum"
792
+ | "exclusiveMaximum"
793
+ | "multipleOf"
794
+ >,
795
+ ): boolean =>
796
+ [
797
+ x.minimum === undefined ||
798
+ (y.minimum !== undefined && x.minimum <= y.minimum) ||
799
+ (y.exclusiveMinimum !== undefined && x.minimum < y.exclusiveMinimum),
800
+ x.maximum === undefined ||
801
+ (y.maximum !== undefined && x.maximum >= y.maximum) ||
802
+ (y.exclusiveMaximum !== undefined && x.maximum > y.exclusiveMaximum),
803
+ x.exclusiveMinimum === undefined ||
804
+ (y.minimum !== undefined && x.exclusiveMinimum <= y.minimum) ||
805
+ (y.exclusiveMinimum !== undefined &&
806
+ x.exclusiveMinimum <= y.exclusiveMinimum),
807
+ x.exclusiveMaximum === undefined ||
808
+ (y.maximum !== undefined && x.exclusiveMaximum >= y.maximum) ||
809
+ (y.exclusiveMaximum !== undefined &&
810
+ x.exclusiveMaximum >= y.exclusiveMaximum),
811
+ x.multipleOf === undefined ||
812
+ (y.multipleOf !== undefined &&
813
+ y.multipleOf / x.multipleOf ===
814
+ Math.floor(y.multipleOf / x.multipleOf)),
815
+ ].every((v) => v);
816
+ }
817
+
818
+ const getReference = (prefix: string): string =>
819
+ prefix
820
+ .split("/")
821
+ .filter((str, i) => !!str.length && !(i === 0 && str === "#"))
822
+ .join(".");