typia 9.7.2 → 10.0.0-dev.20251107

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 (116) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +153 -153
  3. package/lib/factories/ProtobufFactory.js +1 -1
  4. package/lib/factories/ProtobufFactory.mjs +1 -1
  5. package/lib/programmers/internal/json_schema_station.d.mts +2 -2
  6. package/lib/programmers/internal/json_schema_station.d.ts +2 -2
  7. package/lib/programmers/llm/LlmApplicationProgrammer.js +5 -1
  8. package/lib/programmers/llm/LlmApplicationProgrammer.js.map +1 -1
  9. package/lib/programmers/llm/LlmApplicationProgrammer.mjs +5 -1
  10. package/lib/programmers/llm/LlmSchemaProgrammer.js +1 -4
  11. package/lib/programmers/llm/LlmSchemaProgrammer.js.map +1 -1
  12. package/lib/programmers/llm/LlmSchemaProgrammer.mjs +1 -35
  13. package/package.json +121 -121
  14. package/src/AssertionGuard.ts +41 -41
  15. package/src/CamelCase.ts +75 -75
  16. package/src/IRandomGenerator.ts +337 -337
  17. package/src/IReadableURLSearchParams.ts +9 -9
  18. package/src/PascalCase.ts +71 -71
  19. package/src/Primitive.ts +90 -90
  20. package/src/Resolved.ts +72 -72
  21. package/src/SnakeCase.ts +127 -127
  22. package/src/TypeGuardError.ts +216 -216
  23. package/src/factories/MetadataCollection.ts +270 -270
  24. package/src/factories/MetadataCommentTagFactory.ts +632 -632
  25. package/src/factories/MetadataFactory.ts +402 -402
  26. package/src/factories/ProtobufFactory.ts +873 -873
  27. package/src/functional.ts +705 -705
  28. package/src/http.ts +972 -972
  29. package/src/internal/_ProtobufReader.ts +188 -188
  30. package/src/internal/_ProtobufSizer.ts +137 -137
  31. package/src/internal/_ProtobufWriter.ts +135 -135
  32. package/src/internal/_jsonStringifyString.ts +42 -42
  33. package/src/json.ts +643 -643
  34. package/src/llm.ts +615 -615
  35. package/src/misc.ts +594 -594
  36. package/src/module.ts +889 -889
  37. package/src/notations.ts +751 -751
  38. package/src/programmers/FeatureProgrammer.ts +605 -605
  39. package/src/programmers/ImportProgrammer.ts +179 -179
  40. package/src/programmers/RandomProgrammer.ts +1195 -1195
  41. package/src/programmers/helpers/ProtobufWire.ts +34 -34
  42. package/src/programmers/internal/check_array_length.ts +43 -43
  43. package/src/programmers/internal/check_bigint.ts +46 -46
  44. package/src/programmers/internal/check_dynamic_key.ts +197 -197
  45. package/src/programmers/internal/check_dynamic_properties.ts +231 -231
  46. package/src/programmers/internal/check_everything.ts +21 -21
  47. package/src/programmers/internal/check_native.ts +23 -23
  48. package/src/programmers/internal/check_number.ts +108 -108
  49. package/src/programmers/internal/check_object.ts +72 -72
  50. package/src/programmers/internal/check_string.ts +46 -46
  51. package/src/programmers/internal/check_template.ts +46 -46
  52. package/src/programmers/internal/check_union_array_like.ts +331 -331
  53. package/src/programmers/internal/decode_union_object.ts +110 -110
  54. package/src/programmers/internal/feature_object_entries.ts +59 -59
  55. package/src/programmers/internal/json_schema_escaped.ts +78 -78
  56. package/src/programmers/internal/json_schema_object.ts +150 -150
  57. package/src/programmers/internal/json_schema_station.ts +2 -2
  58. package/src/programmers/internal/metadata_to_pattern.ts +40 -40
  59. package/src/programmers/internal/postfix_of_tuple.ts +3 -3
  60. package/src/programmers/internal/prune_object_properties.ts +69 -69
  61. package/src/programmers/internal/stringify_dynamic_properties.ts +158 -158
  62. package/src/programmers/internal/stringify_native.ts +5 -5
  63. package/src/programmers/internal/stringify_regular_properties.ts +77 -77
  64. package/src/programmers/internal/template_to_pattern.ts +21 -21
  65. package/src/programmers/internal/wrap_metadata_rest_tuple.ts +21 -21
  66. package/src/programmers/json/JsonStringifyProgrammer.ts +1124 -1124
  67. package/src/programmers/llm/LlmApplicationProgrammer.ts +10 -1
  68. package/src/programmers/llm/LlmSchemaProgrammer.ts +2 -7
  69. package/src/protobuf.ts +820 -820
  70. package/src/reflect.ts +46 -46
  71. package/src/schemas/json/IJsonApplication.ts +77 -77
  72. package/src/schemas/json/IJsonSchemaCollection.ts +212 -212
  73. package/src/schemas/json/IJsonSchemaUnit.ts +263 -263
  74. package/src/schemas/metadata/IMetadataTypeTag.ts +14 -14
  75. package/src/schemas/metadata/Metadata.ts +669 -669
  76. package/src/schemas/metadata/MetadataAliasType.ts +57 -57
  77. package/src/schemas/metadata/MetadataApplication.ts +40 -40
  78. package/src/schemas/metadata/MetadataArray.ts +47 -47
  79. package/src/schemas/metadata/MetadataArrayType.ts +51 -51
  80. package/src/schemas/metadata/MetadataAtomic.ts +85 -85
  81. package/src/schemas/metadata/MetadataEscaped.ts +45 -45
  82. package/src/schemas/metadata/MetadataFunction.ts +45 -45
  83. package/src/schemas/metadata/MetadataObject.ts +46 -46
  84. package/src/schemas/metadata/MetadataObjectType.ts +137 -137
  85. package/src/schemas/metadata/MetadataParameter.ts +52 -52
  86. package/src/schemas/metadata/MetadataProperty.ts +53 -53
  87. package/src/schemas/metadata/MetadataTemplate.ts +78 -78
  88. package/src/schemas/metadata/MetadataTuple.ts +28 -28
  89. package/src/schemas/metadata/MetadataTupleType.ts +61 -61
  90. package/src/tags/Constant.ts +47 -47
  91. package/src/tags/ContentMediaType.ts +27 -27
  92. package/src/tags/Default.ts +52 -52
  93. package/src/tags/Example.ts +56 -56
  94. package/src/tags/Examples.ts +56 -56
  95. package/src/tags/ExclusiveMaximum.ts +44 -44
  96. package/src/tags/ExclusiveMinimum.ts +44 -44
  97. package/src/tags/Format.ts +78 -78
  98. package/src/tags/JsonSchemaPlugin.ts +36 -36
  99. package/src/tags/MaxItems.ts +31 -31
  100. package/src/tags/MaxLength.ts +25 -25
  101. package/src/tags/Maximum.ts +39 -39
  102. package/src/tags/MinItems.ts +31 -31
  103. package/src/tags/MinLength.ts +25 -25
  104. package/src/tags/Minimum.ts +39 -39
  105. package/src/tags/MultipleOf.ts +42 -42
  106. package/src/tags/Pattern.ts +49 -49
  107. package/src/tags/Sequence.ts +37 -37
  108. package/src/tags/TagBase.ts +102 -102
  109. package/src/tags/Type.ts +64 -64
  110. package/src/tags/UniqueItems.ts +34 -34
  111. package/src/tags/internal/FormatCheatSheet.ts +71 -71
  112. package/src/transformers/ITransformOptions.ts +70 -70
  113. package/src/transformers/ImportTransformer.ts +253 -253
  114. package/src/transformers/NoTransformConfigurationError.ts +16 -16
  115. package/src/transformers/features/llm/LlmApplicationTransformer.ts +224 -224
  116. package/src/typings/Equal.ts +18 -18
@@ -1,632 +1,632 @@
1
- import ts from "typescript";
2
-
3
- import { IMetadataTypeTag } from "../schemas/metadata/IMetadataTypeTag";
4
- import { Metadata } from "../schemas/metadata/Metadata";
5
-
6
- import { Writable } from "../typings/Writable";
7
-
8
- import { FormatCheatSheet } from "../tags/internal/FormatCheatSheet";
9
- import { MetadataFactory } from "./MetadataFactory";
10
- import { MetadataTypeTagFactory } from "./MetadataTypeTagFactory";
11
-
12
- /** @internal */
13
- export namespace MetadataCommentTagFactory {
14
- export const analyze = (props: {
15
- errors: MetadataFactory.IError[];
16
- metadata: Metadata;
17
- tags: ts.JSDocTagInfo[];
18
- explore: MetadataFactory.IExplore;
19
- }): void => {
20
- // PREPARE MESSAGE CONTAINER
21
- const messages: string[] = [];
22
- const report = (msg: string) => {
23
- messages.push(msg);
24
- return null;
25
- };
26
- const validateReport = (next: {
27
- property: string | null;
28
- message: string;
29
- }): false => {
30
- messages.push(
31
- `the property ${
32
- next.property === null
33
- ? `["typia.tag"]`
34
- : `["typia.tag.${next.property}"]`
35
- } ${next.message}.`,
36
- );
37
- return false;
38
- };
39
-
40
- // VALIDATE AND CONSTRUCT COMMENT TAGS
41
- for (const tag of props.tags) {
42
- const tagger: TagRecord | null = parse({
43
- report,
44
- tag,
45
- });
46
- if (tagger === null) continue;
47
- for (const [key, value] of Object.entries(tagger)) {
48
- const filtered: IMetadataTypeTag[] = value.filter(
49
- (v) => v.validate !== null,
50
- ) as IMetadataTypeTag[];
51
- if (key === "array") {
52
- if (props.metadata.arrays.length === 0) {
53
- report(`requires array type`);
54
- continue;
55
- }
56
- for (const a of props.metadata.arrays) {
57
- Writable(a).tags = a.tags.filter((x) =>
58
- MetadataTypeTagFactory.validate({
59
- report: validateReport,
60
- type: "array",
61
- tags: [...x, ...filtered],
62
- }),
63
- );
64
- if (a.tags.length === 0) a.tags.push(filtered);
65
- else for (const tags of a.tags) tags.push(...filtered);
66
- }
67
- } else {
68
- const atomic = props.metadata.atomics.find((a) => a.type == key);
69
- if (atomic === undefined)
70
- if (key === "bigint" || key === "number") {
71
- const opposite = key === "bigint" ? "number" : "bigint";
72
- if (
73
- tagger[opposite] !== undefined &&
74
- props.metadata.atomics.some((a) => a.type === opposite)
75
- )
76
- continue;
77
- } else if (
78
- key === "string" &&
79
- value[0]?.kind === "format" &&
80
- value[0]?.value === "date-time"
81
- )
82
- continue;
83
- else report(`requires ${key} type`);
84
- else {
85
- Writable(atomic).tags = atomic.tags.filter((x) =>
86
- MetadataTypeTagFactory.validate({
87
- report: validateReport,
88
- type: key as "string",
89
- tags: [...x, ...filtered],
90
- }),
91
- );
92
- if (atomic.tags.length === 0) atomic.tags.push(filtered);
93
- else for (const tags of atomic.tags) tags.push(...filtered);
94
- }
95
- }
96
- }
97
- }
98
-
99
- // DO REPORT
100
- if (messages.length !== 0)
101
- props.errors.push({
102
- name: "comment tag(s)",
103
- explore: props.explore,
104
- messages,
105
- });
106
- };
107
-
108
- const parse = (props: {
109
- report: (msg: string) => null;
110
- tag: ts.JSDocTagInfo;
111
- }): TagRecord | null => {
112
- const next = PARSER[props.tag.name];
113
- if (next === undefined) return {};
114
-
115
- const value = (props.tag.text || [])[0]?.text;
116
- if (value === undefined && props.tag.name !== "uniqueItems")
117
- return props.report(`no comment tag value`);
118
- return next({
119
- report: props.report,
120
- value: value!,
121
- });
122
- };
123
-
124
- export const get = (props: {
125
- kind: string;
126
- type: "array" | "bigint" | "number" | "string";
127
- value: string;
128
- }): IMetadataTypeTag[] => {
129
- const output: IMetadataTypeTag[] | undefined = PARSER[props.kind]?.({
130
- report: () => null,
131
- value: props.value,
132
- })?.[props.type];
133
- if (output === undefined)
134
- throw new Error(
135
- `no tag found for (kind: ${props.kind}, type: ${props.type}).`,
136
- );
137
- return output;
138
- };
139
- }
140
-
141
- /** @internal */
142
- type TagRecord = {
143
- [P in Target]?: NotDeterminedTypeTag[];
144
- };
145
-
146
- /** @internal */
147
- type Target = "bigint" | "number" | "string" | "array";
148
-
149
- /** @internal */
150
- type NotDeterminedTypeTag = Omit<IMetadataTypeTag, "validate" | "schema"> & {
151
- validate: string | undefined;
152
- schema: object | undefined;
153
- };
154
-
155
- /** @internal */
156
- const PARSER: Record<
157
- string,
158
- (props: { report: (msg: string) => null; value: string }) => {
159
- [P in Target]?: NotDeterminedTypeTag[];
160
- }
161
- > = {
162
- /* -----------------------------------------------------------
163
- ARRAY
164
- ----------------------------------------------------------- */
165
- items: ({ report, value }) => ({
166
- array: [
167
- {
168
- name: `MinItems<${value}>`,
169
- target: "array",
170
- kind: "minItems",
171
- value: parse_integer({
172
- report,
173
- value,
174
- unsigned: true,
175
- }),
176
- validate: `${value} <= $input.length`,
177
- exclusive: true,
178
- schema: {
179
- minItems: parse_integer({
180
- report,
181
- value,
182
- unsigned: true,
183
- }),
184
- },
185
- },
186
- {
187
- name: `MaxItems<${value}>`,
188
- target: "array",
189
- kind: "maxItems",
190
- value: parse_integer({
191
- report,
192
- value,
193
- unsigned: true,
194
- }),
195
- validate: `$input.length <= ${value}`,
196
- exclusive: true,
197
- schema: {
198
- maxItems: parse_integer({
199
- report,
200
- unsigned: true,
201
- value,
202
- }),
203
- },
204
- },
205
- ],
206
- }),
207
- minItems: ({ report, value }) => ({
208
- array: [
209
- {
210
- name: `MinItems<${value}>`,
211
- target: "array",
212
- kind: "minItems",
213
- value: parse_integer({
214
- report,
215
- value,
216
- unsigned: true,
217
- }),
218
- validate: `${value} <= $input.length`,
219
- exclusive: true,
220
- schema: {
221
- minItems: parse_integer({
222
- report,
223
- value,
224
- unsigned: true,
225
- }),
226
- },
227
- },
228
- ],
229
- }),
230
- maxItems: ({ report, value }) => ({
231
- array: [
232
- {
233
- name: `MaxItems<${value}>`,
234
- target: "array",
235
- kind: "maxItems",
236
- value: parse_integer({
237
- report,
238
- value,
239
- unsigned: true,
240
- }),
241
- validate: `$input.length <= ${value}`,
242
- exclusive: true,
243
- schema: {
244
- maxItems: parse_integer({
245
- report,
246
- value,
247
- unsigned: true,
248
- }),
249
- },
250
- },
251
- ],
252
- }),
253
- uniqueItems: () => ({
254
- array: [
255
- {
256
- name: `UniqueItems`,
257
- target: "array",
258
- kind: "uniqueItems",
259
- value: true,
260
- validate: `$input.length <= 1 || (new Set($input).size === $input.length)`,
261
- exclusive: true,
262
- schema: {
263
- uniqueItems: true,
264
- },
265
- },
266
- ],
267
- }),
268
-
269
- /* -----------------------------------------------------------
270
- NUMBER
271
- ----------------------------------------------------------- */
272
- type: ({ value }) => {
273
- // EMENDATIONS
274
- if (value.startsWith("{") && value.endsWith("}"))
275
- value = value.substring(1, value.length - 1);
276
- if (value === "int") value = "int32";
277
- else if (value === "uint") value = "uint32";
278
-
279
- // MUST BE ONE OF THEM
280
- if (
281
- ["int32", "uint32", "int64", "uint64", "float", "double"].includes(
282
- value,
283
- ) === false
284
- )
285
- return {};
286
- return {
287
- number: [
288
- {
289
- name: `Type<${JSON.stringify(value)}>`,
290
- target: "number",
291
- kind: "type",
292
- value: value,
293
- validate:
294
- value === "int32"
295
- ? `Math.floor($input) === $input && -2147483648 <= $input && $input <= 2147483647`
296
- : value === "uint32"
297
- ? `Math.floor($input) === $input && 0 <= $input && $input <= 4294967295`
298
- : value === "int64"
299
- ? `Math.floor($input) === $input && -9223372036854775808 <= $input && $input <= 9223372036854775807`
300
- : value === "uint64"
301
- ? `Math.floor($input) === $input && 0 <= $input && $input <= 18446744073709551615`
302
- : value === "float"
303
- ? `-1.175494351e38 <= $input && $input <= 3.4028235e38`
304
- : `true`,
305
- exclusive: true,
306
- schema: ["int32", "int64"].includes(value)
307
- ? { type: "integer" }
308
- : ["uint32", "uint64"].includes(value)
309
- ? { type: "integer", minimum: 0 }
310
- : undefined,
311
- },
312
- ],
313
- bigint:
314
- value === "int64" || "uint64"
315
- ? [
316
- {
317
- name: `Type<${JSON.stringify(value)}>`,
318
- target: "bigint",
319
- kind: "type",
320
- value: value,
321
- validate: value === "int64" ? "true" : "BigInt(0) <= $input",
322
- exclusive: true,
323
- schema: value === "uint64" ? { minimum: 0 } : undefined,
324
- },
325
- ]
326
- : [],
327
- };
328
- },
329
- minimum: (props) => ({
330
- number: [
331
- {
332
- name: `Minimum<${props.value}>`,
333
- target: "number",
334
- kind: "minimum",
335
- value: parse_number(props),
336
- validate: `${props.value} <= $input`,
337
- exclusive: ["minimum", "exclusiveMinimum"],
338
- schema: {
339
- minimum: parse_number(props),
340
- },
341
- },
342
- ],
343
- bigint: [
344
- {
345
- name: `Minimum<${props.value}n>`,
346
- target: "bigint",
347
- kind: "minimum",
348
- value: (() => {
349
- const parsed = parse_integer({
350
- report: props.report,
351
- value: props.value,
352
- unsigned: false,
353
- });
354
- return parsed === null ? null : BigInt(parsed);
355
- })(),
356
- validate: `${props.value} <= $input`,
357
- exclusive: ["minimum", "exclusiveMinimum"],
358
- schema: {
359
- minimum: parse_number(props),
360
- },
361
- },
362
- ],
363
- }),
364
- maximum: (props) => ({
365
- number: [
366
- {
367
- name: `Maximum<${props.value}>`,
368
- target: "number",
369
- kind: "maximum",
370
- value: parse_number(props),
371
- validate: `$input <= ${props.value}`,
372
- exclusive: ["maximum", "exclusiveMaximum"],
373
- schema: {
374
- maximum: parse_number(props),
375
- },
376
- },
377
- ],
378
- bigint: [
379
- {
380
- name: `Maximum<${props.value}n>`,
381
- target: "bigint",
382
- kind: "maximum",
383
- value: (() => {
384
- const parsed = parse_integer({
385
- report: props.report,
386
- value: props.value,
387
- unsigned: false,
388
- });
389
- return parsed === null ? null : BigInt(parsed);
390
- })(),
391
- validate: `$input <= ${props.value}`,
392
- exclusive: ["maximum", "exclusiveMaximum"],
393
- schema: {
394
- maximum: parse_number(props),
395
- },
396
- },
397
- ],
398
- }),
399
- exclusiveMinimum: (props) => ({
400
- number: [
401
- {
402
- name: `ExclusiveMinimum<${props.value}>`,
403
- target: "number",
404
- kind: "exclusiveMinimum",
405
- value: parse_number(props),
406
- validate: `${props.value} < $input`,
407
- exclusive: ["minimum", "exclusiveMinimum"],
408
- schema: {
409
- exclusiveMinimum: parse_number(props),
410
- },
411
- },
412
- ],
413
- bigint: [
414
- {
415
- name: `ExclusiveMinimum<${props.value}n>`,
416
- target: "bigint",
417
- kind: "exclusiveMinimum",
418
- value: (() => {
419
- const parsed = parse_integer({
420
- report: props.report,
421
- value: props.value,
422
- unsigned: false,
423
- });
424
- return parsed === null ? null : BigInt(parsed);
425
- })(),
426
- validate: `${props.value} < $input`,
427
- exclusive: ["minimum", "exclusiveMinimum"],
428
- schema: {
429
- exclusiveMinimum: parse_number(props),
430
- },
431
- },
432
- ],
433
- }),
434
- exclusiveMaximum: (props) => ({
435
- number: [
436
- {
437
- name: `ExclusiveMaximum<${props.value}>`,
438
- target: "number",
439
- kind: "exclusiveMaximum",
440
- value: parse_number(props),
441
- validate: `$input < ${props.value}`,
442
- exclusive: ["maximum", "exclusiveMaximum"],
443
- schema: {
444
- exclusiveMaximum: parse_number(props),
445
- },
446
- },
447
- ],
448
- bigint: [
449
- {
450
- name: `ExclusiveMaximum<${props.value}n>`,
451
- target: "bigint",
452
- kind: "exclusiveMaximum",
453
- value: (() => {
454
- const parsed = parse_integer({
455
- report: props.report,
456
- value: props.value,
457
- unsigned: false,
458
- });
459
- return parsed === null ? null : BigInt(parsed);
460
- })(),
461
- validate: `$input < ${props.value}`,
462
- exclusive: ["maximum", "exclusiveMaximum"],
463
- schema: {
464
- exclusiveMaximum: parse_number(props),
465
- },
466
- },
467
- ],
468
- }),
469
- multipleOf: (props) => ({
470
- number: [
471
- {
472
- name: `MultipleOf<${props.value}>`,
473
- target: "number",
474
- kind: "multipleOf",
475
- value: parse_number(props),
476
- validate: `$input % ${props.value} === 0`,
477
- exclusive: true,
478
- schema: {
479
- multipleOf: parse_number(props),
480
- },
481
- },
482
- ],
483
- bigint: [
484
- {
485
- name: `MultipleOf<${props.value}n>`,
486
- target: "bigint",
487
- kind: "multipleOf",
488
- value: (() => {
489
- const parsed = parse_integer({
490
- report: props.report,
491
- value: props.value,
492
- unsigned: false,
493
- });
494
- return parsed === null ? null : BigInt(parsed);
495
- })(),
496
- validate: `$input % ${props.value}n === 0n`,
497
- exclusive: true,
498
- schema: {
499
- multipleOf: parse_number(props),
500
- },
501
- },
502
- ],
503
- }),
504
-
505
- /* -----------------------------------------------------------
506
- STRING
507
- ----------------------------------------------------------- */
508
- format: ({ value }) => {
509
- const matched = FORMATS.get(value);
510
- if (matched === undefined) return {};
511
- return {
512
- string: [
513
- {
514
- name: `Format<${JSON.stringify(matched[0])}>`,
515
- target: "string",
516
- kind: "format",
517
- value: matched[0],
518
- validate: matched[1],
519
- exclusive: true,
520
- schema: {
521
- format: matched[0],
522
- },
523
- },
524
- ],
525
- };
526
- },
527
- pattern: ({ value }) => ({
528
- string: [
529
- {
530
- name: `Pattern<${JSON.stringify(value)}>`,
531
- target: "string",
532
- kind: "pattern",
533
- value: value,
534
- validate: `RegExp(${JSON.stringify(value)}).test($input)`,
535
- exclusive: ["format"],
536
- schema: {
537
- pattern: value,
538
- },
539
- },
540
- ],
541
- }),
542
- length: (props) => ({
543
- string: [
544
- {
545
- name: `MinLength<${props.value}>`,
546
- target: "string",
547
- kind: "minLength",
548
- value: parse_number(props),
549
- validate: `${props.value} <= $input.length`,
550
- exclusive: true,
551
- schema: {
552
- minLength: parse_number(props),
553
- },
554
- },
555
- {
556
- name: `MaxLength<${props.value}>`,
557
- target: "string",
558
- kind: "maxLength",
559
- value: parse_number(props),
560
- validate: `$input.length <= ${props.value}`,
561
- exclusive: true,
562
- schema: {
563
- maxLength: parse_number(props),
564
- },
565
- },
566
- ],
567
- }),
568
- minLength: ({ report, value }) => ({
569
- string: [
570
- {
571
- name: `MinLength<${value}>`,
572
- target: "string",
573
- kind: "minLength",
574
- value: parse_number({ report, value }),
575
- validate: `${value} <= $input.length`,
576
- exclusive: true,
577
- schema: {
578
- minLength: parse_number({ report, value }),
579
- },
580
- },
581
- ],
582
- }),
583
- maxLength: ({ report, value }) => ({
584
- string: [
585
- {
586
- name: `MaxLength<${value}>`,
587
- target: "string",
588
- kind: "maxLength",
589
- value: parse_number({ report, value }),
590
- validate: `$input.length <= ${value}`,
591
- exclusive: true,
592
- schema: {
593
- maxLength: parse_number({ report, value }),
594
- },
595
- },
596
- ],
597
- }),
598
- };
599
-
600
- /** @internal */
601
- const parse_number = (props: {
602
- report: (msg: string) => null;
603
- value: string;
604
- }): number | null => {
605
- const parsed: number = Number(props.value);
606
- if (isNaN(parsed) === true) return props.report(`invalid number`);
607
- return parsed;
608
- };
609
-
610
- /** @internal */
611
- const parse_integer = (props: {
612
- report: (msg: string) => null;
613
- unsigned: boolean;
614
- value: string;
615
- }): number | null => {
616
- const parsed: number | null = parse_number(props);
617
- if (parsed === null) return null;
618
- else if (Math.floor(parsed) !== parsed)
619
- return props.report(`invalid integer`);
620
- else if (props.unsigned === true && parsed < 0)
621
- return props.report(`invalid unsigned integer`);
622
- return parsed;
623
- };
624
-
625
- /** @internal */
626
- const FORMATS: Map<string, [string, string]> = new Map([
627
- ...Object.entries(FormatCheatSheet).map(
628
- ([key, value]) => [key, [key, value]] as any,
629
- ),
630
- ["datetime", ["date-time", `!isNaN(new Date($input).getTime())`]],
631
- ["dateTime", ["date-time", `!isNaN(new Date($input).getTime())`]],
632
- ]);
1
+ import ts from "typescript";
2
+
3
+ import { IMetadataTypeTag } from "../schemas/metadata/IMetadataTypeTag";
4
+ import { Metadata } from "../schemas/metadata/Metadata";
5
+
6
+ import { Writable } from "../typings/Writable";
7
+
8
+ import { FormatCheatSheet } from "../tags/internal/FormatCheatSheet";
9
+ import { MetadataFactory } from "./MetadataFactory";
10
+ import { MetadataTypeTagFactory } from "./MetadataTypeTagFactory";
11
+
12
+ /** @internal */
13
+ export namespace MetadataCommentTagFactory {
14
+ export const analyze = (props: {
15
+ errors: MetadataFactory.IError[];
16
+ metadata: Metadata;
17
+ tags: ts.JSDocTagInfo[];
18
+ explore: MetadataFactory.IExplore;
19
+ }): void => {
20
+ // PREPARE MESSAGE CONTAINER
21
+ const messages: string[] = [];
22
+ const report = (msg: string) => {
23
+ messages.push(msg);
24
+ return null;
25
+ };
26
+ const validateReport = (next: {
27
+ property: string | null;
28
+ message: string;
29
+ }): false => {
30
+ messages.push(
31
+ `the property ${
32
+ next.property === null
33
+ ? `["typia.tag"]`
34
+ : `["typia.tag.${next.property}"]`
35
+ } ${next.message}.`,
36
+ );
37
+ return false;
38
+ };
39
+
40
+ // VALIDATE AND CONSTRUCT COMMENT TAGS
41
+ for (const tag of props.tags) {
42
+ const tagger: TagRecord | null = parse({
43
+ report,
44
+ tag,
45
+ });
46
+ if (tagger === null) continue;
47
+ for (const [key, value] of Object.entries(tagger)) {
48
+ const filtered: IMetadataTypeTag[] = value.filter(
49
+ (v) => v.validate !== null,
50
+ ) as IMetadataTypeTag[];
51
+ if (key === "array") {
52
+ if (props.metadata.arrays.length === 0) {
53
+ report(`requires array type`);
54
+ continue;
55
+ }
56
+ for (const a of props.metadata.arrays) {
57
+ Writable(a).tags = a.tags.filter((x) =>
58
+ MetadataTypeTagFactory.validate({
59
+ report: validateReport,
60
+ type: "array",
61
+ tags: [...x, ...filtered],
62
+ }),
63
+ );
64
+ if (a.tags.length === 0) a.tags.push(filtered);
65
+ else for (const tags of a.tags) tags.push(...filtered);
66
+ }
67
+ } else {
68
+ const atomic = props.metadata.atomics.find((a) => a.type == key);
69
+ if (atomic === undefined)
70
+ if (key === "bigint" || key === "number") {
71
+ const opposite = key === "bigint" ? "number" : "bigint";
72
+ if (
73
+ tagger[opposite] !== undefined &&
74
+ props.metadata.atomics.some((a) => a.type === opposite)
75
+ )
76
+ continue;
77
+ } else if (
78
+ key === "string" &&
79
+ value[0]?.kind === "format" &&
80
+ value[0]?.value === "date-time"
81
+ )
82
+ continue;
83
+ else report(`requires ${key} type`);
84
+ else {
85
+ Writable(atomic).tags = atomic.tags.filter((x) =>
86
+ MetadataTypeTagFactory.validate({
87
+ report: validateReport,
88
+ type: key as "string",
89
+ tags: [...x, ...filtered],
90
+ }),
91
+ );
92
+ if (atomic.tags.length === 0) atomic.tags.push(filtered);
93
+ else for (const tags of atomic.tags) tags.push(...filtered);
94
+ }
95
+ }
96
+ }
97
+ }
98
+
99
+ // DO REPORT
100
+ if (messages.length !== 0)
101
+ props.errors.push({
102
+ name: "comment tag(s)",
103
+ explore: props.explore,
104
+ messages,
105
+ });
106
+ };
107
+
108
+ const parse = (props: {
109
+ report: (msg: string) => null;
110
+ tag: ts.JSDocTagInfo;
111
+ }): TagRecord | null => {
112
+ const next = PARSER[props.tag.name];
113
+ if (next === undefined) return {};
114
+
115
+ const value = (props.tag.text || [])[0]?.text;
116
+ if (value === undefined && props.tag.name !== "uniqueItems")
117
+ return props.report(`no comment tag value`);
118
+ return next({
119
+ report: props.report,
120
+ value: value!,
121
+ });
122
+ };
123
+
124
+ export const get = (props: {
125
+ kind: string;
126
+ type: "array" | "bigint" | "number" | "string";
127
+ value: string;
128
+ }): IMetadataTypeTag[] => {
129
+ const output: IMetadataTypeTag[] | undefined = PARSER[props.kind]?.({
130
+ report: () => null,
131
+ value: props.value,
132
+ })?.[props.type];
133
+ if (output === undefined)
134
+ throw new Error(
135
+ `no tag found for (kind: ${props.kind}, type: ${props.type}).`,
136
+ );
137
+ return output;
138
+ };
139
+ }
140
+
141
+ /** @internal */
142
+ type TagRecord = {
143
+ [P in Target]?: NotDeterminedTypeTag[];
144
+ };
145
+
146
+ /** @internal */
147
+ type Target = "bigint" | "number" | "string" | "array";
148
+
149
+ /** @internal */
150
+ type NotDeterminedTypeTag = Omit<IMetadataTypeTag, "validate" | "schema"> & {
151
+ validate: string | undefined;
152
+ schema: object | undefined;
153
+ };
154
+
155
+ /** @internal */
156
+ const PARSER: Record<
157
+ string,
158
+ (props: { report: (msg: string) => null; value: string }) => {
159
+ [P in Target]?: NotDeterminedTypeTag[];
160
+ }
161
+ > = {
162
+ /* -----------------------------------------------------------
163
+ ARRAY
164
+ ----------------------------------------------------------- */
165
+ items: ({ report, value }) => ({
166
+ array: [
167
+ {
168
+ name: `MinItems<${value}>`,
169
+ target: "array",
170
+ kind: "minItems",
171
+ value: parse_integer({
172
+ report,
173
+ value,
174
+ unsigned: true,
175
+ }),
176
+ validate: `${value} <= $input.length`,
177
+ exclusive: true,
178
+ schema: {
179
+ minItems: parse_integer({
180
+ report,
181
+ value,
182
+ unsigned: true,
183
+ }),
184
+ },
185
+ },
186
+ {
187
+ name: `MaxItems<${value}>`,
188
+ target: "array",
189
+ kind: "maxItems",
190
+ value: parse_integer({
191
+ report,
192
+ value,
193
+ unsigned: true,
194
+ }),
195
+ validate: `$input.length <= ${value}`,
196
+ exclusive: true,
197
+ schema: {
198
+ maxItems: parse_integer({
199
+ report,
200
+ unsigned: true,
201
+ value,
202
+ }),
203
+ },
204
+ },
205
+ ],
206
+ }),
207
+ minItems: ({ report, value }) => ({
208
+ array: [
209
+ {
210
+ name: `MinItems<${value}>`,
211
+ target: "array",
212
+ kind: "minItems",
213
+ value: parse_integer({
214
+ report,
215
+ value,
216
+ unsigned: true,
217
+ }),
218
+ validate: `${value} <= $input.length`,
219
+ exclusive: true,
220
+ schema: {
221
+ minItems: parse_integer({
222
+ report,
223
+ value,
224
+ unsigned: true,
225
+ }),
226
+ },
227
+ },
228
+ ],
229
+ }),
230
+ maxItems: ({ report, value }) => ({
231
+ array: [
232
+ {
233
+ name: `MaxItems<${value}>`,
234
+ target: "array",
235
+ kind: "maxItems",
236
+ value: parse_integer({
237
+ report,
238
+ value,
239
+ unsigned: true,
240
+ }),
241
+ validate: `$input.length <= ${value}`,
242
+ exclusive: true,
243
+ schema: {
244
+ maxItems: parse_integer({
245
+ report,
246
+ value,
247
+ unsigned: true,
248
+ }),
249
+ },
250
+ },
251
+ ],
252
+ }),
253
+ uniqueItems: () => ({
254
+ array: [
255
+ {
256
+ name: `UniqueItems`,
257
+ target: "array",
258
+ kind: "uniqueItems",
259
+ value: true,
260
+ validate: `$input.length <= 1 || (new Set($input).size === $input.length)`,
261
+ exclusive: true,
262
+ schema: {
263
+ uniqueItems: true,
264
+ },
265
+ },
266
+ ],
267
+ }),
268
+
269
+ /* -----------------------------------------------------------
270
+ NUMBER
271
+ ----------------------------------------------------------- */
272
+ type: ({ value }) => {
273
+ // EMENDATIONS
274
+ if (value.startsWith("{") && value.endsWith("}"))
275
+ value = value.substring(1, value.length - 1);
276
+ if (value === "int") value = "int32";
277
+ else if (value === "uint") value = "uint32";
278
+
279
+ // MUST BE ONE OF THEM
280
+ if (
281
+ ["int32", "uint32", "int64", "uint64", "float", "double"].includes(
282
+ value,
283
+ ) === false
284
+ )
285
+ return {};
286
+ return {
287
+ number: [
288
+ {
289
+ name: `Type<${JSON.stringify(value)}>`,
290
+ target: "number",
291
+ kind: "type",
292
+ value: value,
293
+ validate:
294
+ value === "int32"
295
+ ? `Math.floor($input) === $input && -2147483648 <= $input && $input <= 2147483647`
296
+ : value === "uint32"
297
+ ? `Math.floor($input) === $input && 0 <= $input && $input <= 4294967295`
298
+ : value === "int64"
299
+ ? `Math.floor($input) === $input && -9223372036854775808 <= $input && $input <= 9223372036854775807`
300
+ : value === "uint64"
301
+ ? `Math.floor($input) === $input && 0 <= $input && $input <= 18446744073709551615`
302
+ : value === "float"
303
+ ? `-1.175494351e38 <= $input && $input <= 3.4028235e38`
304
+ : `true`,
305
+ exclusive: true,
306
+ schema: ["int32", "int64"].includes(value)
307
+ ? { type: "integer" }
308
+ : ["uint32", "uint64"].includes(value)
309
+ ? { type: "integer", minimum: 0 }
310
+ : undefined,
311
+ },
312
+ ],
313
+ bigint:
314
+ value === "int64" || "uint64"
315
+ ? [
316
+ {
317
+ name: `Type<${JSON.stringify(value)}>`,
318
+ target: "bigint",
319
+ kind: "type",
320
+ value: value,
321
+ validate: value === "int64" ? "true" : "BigInt(0) <= $input",
322
+ exclusive: true,
323
+ schema: value === "uint64" ? { minimum: 0 } : undefined,
324
+ },
325
+ ]
326
+ : [],
327
+ };
328
+ },
329
+ minimum: (props) => ({
330
+ number: [
331
+ {
332
+ name: `Minimum<${props.value}>`,
333
+ target: "number",
334
+ kind: "minimum",
335
+ value: parse_number(props),
336
+ validate: `${props.value} <= $input`,
337
+ exclusive: ["minimum", "exclusiveMinimum"],
338
+ schema: {
339
+ minimum: parse_number(props),
340
+ },
341
+ },
342
+ ],
343
+ bigint: [
344
+ {
345
+ name: `Minimum<${props.value}n>`,
346
+ target: "bigint",
347
+ kind: "minimum",
348
+ value: (() => {
349
+ const parsed = parse_integer({
350
+ report: props.report,
351
+ value: props.value,
352
+ unsigned: false,
353
+ });
354
+ return parsed === null ? null : BigInt(parsed);
355
+ })(),
356
+ validate: `${props.value} <= $input`,
357
+ exclusive: ["minimum", "exclusiveMinimum"],
358
+ schema: {
359
+ minimum: parse_number(props),
360
+ },
361
+ },
362
+ ],
363
+ }),
364
+ maximum: (props) => ({
365
+ number: [
366
+ {
367
+ name: `Maximum<${props.value}>`,
368
+ target: "number",
369
+ kind: "maximum",
370
+ value: parse_number(props),
371
+ validate: `$input <= ${props.value}`,
372
+ exclusive: ["maximum", "exclusiveMaximum"],
373
+ schema: {
374
+ maximum: parse_number(props),
375
+ },
376
+ },
377
+ ],
378
+ bigint: [
379
+ {
380
+ name: `Maximum<${props.value}n>`,
381
+ target: "bigint",
382
+ kind: "maximum",
383
+ value: (() => {
384
+ const parsed = parse_integer({
385
+ report: props.report,
386
+ value: props.value,
387
+ unsigned: false,
388
+ });
389
+ return parsed === null ? null : BigInt(parsed);
390
+ })(),
391
+ validate: `$input <= ${props.value}`,
392
+ exclusive: ["maximum", "exclusiveMaximum"],
393
+ schema: {
394
+ maximum: parse_number(props),
395
+ },
396
+ },
397
+ ],
398
+ }),
399
+ exclusiveMinimum: (props) => ({
400
+ number: [
401
+ {
402
+ name: `ExclusiveMinimum<${props.value}>`,
403
+ target: "number",
404
+ kind: "exclusiveMinimum",
405
+ value: parse_number(props),
406
+ validate: `${props.value} < $input`,
407
+ exclusive: ["minimum", "exclusiveMinimum"],
408
+ schema: {
409
+ exclusiveMinimum: parse_number(props),
410
+ },
411
+ },
412
+ ],
413
+ bigint: [
414
+ {
415
+ name: `ExclusiveMinimum<${props.value}n>`,
416
+ target: "bigint",
417
+ kind: "exclusiveMinimum",
418
+ value: (() => {
419
+ const parsed = parse_integer({
420
+ report: props.report,
421
+ value: props.value,
422
+ unsigned: false,
423
+ });
424
+ return parsed === null ? null : BigInt(parsed);
425
+ })(),
426
+ validate: `${props.value} < $input`,
427
+ exclusive: ["minimum", "exclusiveMinimum"],
428
+ schema: {
429
+ exclusiveMinimum: parse_number(props),
430
+ },
431
+ },
432
+ ],
433
+ }),
434
+ exclusiveMaximum: (props) => ({
435
+ number: [
436
+ {
437
+ name: `ExclusiveMaximum<${props.value}>`,
438
+ target: "number",
439
+ kind: "exclusiveMaximum",
440
+ value: parse_number(props),
441
+ validate: `$input < ${props.value}`,
442
+ exclusive: ["maximum", "exclusiveMaximum"],
443
+ schema: {
444
+ exclusiveMaximum: parse_number(props),
445
+ },
446
+ },
447
+ ],
448
+ bigint: [
449
+ {
450
+ name: `ExclusiveMaximum<${props.value}n>`,
451
+ target: "bigint",
452
+ kind: "exclusiveMaximum",
453
+ value: (() => {
454
+ const parsed = parse_integer({
455
+ report: props.report,
456
+ value: props.value,
457
+ unsigned: false,
458
+ });
459
+ return parsed === null ? null : BigInt(parsed);
460
+ })(),
461
+ validate: `$input < ${props.value}`,
462
+ exclusive: ["maximum", "exclusiveMaximum"],
463
+ schema: {
464
+ exclusiveMaximum: parse_number(props),
465
+ },
466
+ },
467
+ ],
468
+ }),
469
+ multipleOf: (props) => ({
470
+ number: [
471
+ {
472
+ name: `MultipleOf<${props.value}>`,
473
+ target: "number",
474
+ kind: "multipleOf",
475
+ value: parse_number(props),
476
+ validate: `$input % ${props.value} === 0`,
477
+ exclusive: true,
478
+ schema: {
479
+ multipleOf: parse_number(props),
480
+ },
481
+ },
482
+ ],
483
+ bigint: [
484
+ {
485
+ name: `MultipleOf<${props.value}n>`,
486
+ target: "bigint",
487
+ kind: "multipleOf",
488
+ value: (() => {
489
+ const parsed = parse_integer({
490
+ report: props.report,
491
+ value: props.value,
492
+ unsigned: false,
493
+ });
494
+ return parsed === null ? null : BigInt(parsed);
495
+ })(),
496
+ validate: `$input % ${props.value}n === 0n`,
497
+ exclusive: true,
498
+ schema: {
499
+ multipleOf: parse_number(props),
500
+ },
501
+ },
502
+ ],
503
+ }),
504
+
505
+ /* -----------------------------------------------------------
506
+ STRING
507
+ ----------------------------------------------------------- */
508
+ format: ({ value }) => {
509
+ const matched = FORMATS.get(value);
510
+ if (matched === undefined) return {};
511
+ return {
512
+ string: [
513
+ {
514
+ name: `Format<${JSON.stringify(matched[0])}>`,
515
+ target: "string",
516
+ kind: "format",
517
+ value: matched[0],
518
+ validate: matched[1],
519
+ exclusive: true,
520
+ schema: {
521
+ format: matched[0],
522
+ },
523
+ },
524
+ ],
525
+ };
526
+ },
527
+ pattern: ({ value }) => ({
528
+ string: [
529
+ {
530
+ name: `Pattern<${JSON.stringify(value)}>`,
531
+ target: "string",
532
+ kind: "pattern",
533
+ value: value,
534
+ validate: `RegExp(${JSON.stringify(value)}).test($input)`,
535
+ exclusive: ["format"],
536
+ schema: {
537
+ pattern: value,
538
+ },
539
+ },
540
+ ],
541
+ }),
542
+ length: (props) => ({
543
+ string: [
544
+ {
545
+ name: `MinLength<${props.value}>`,
546
+ target: "string",
547
+ kind: "minLength",
548
+ value: parse_number(props),
549
+ validate: `${props.value} <= $input.length`,
550
+ exclusive: true,
551
+ schema: {
552
+ minLength: parse_number(props),
553
+ },
554
+ },
555
+ {
556
+ name: `MaxLength<${props.value}>`,
557
+ target: "string",
558
+ kind: "maxLength",
559
+ value: parse_number(props),
560
+ validate: `$input.length <= ${props.value}`,
561
+ exclusive: true,
562
+ schema: {
563
+ maxLength: parse_number(props),
564
+ },
565
+ },
566
+ ],
567
+ }),
568
+ minLength: ({ report, value }) => ({
569
+ string: [
570
+ {
571
+ name: `MinLength<${value}>`,
572
+ target: "string",
573
+ kind: "minLength",
574
+ value: parse_number({ report, value }),
575
+ validate: `${value} <= $input.length`,
576
+ exclusive: true,
577
+ schema: {
578
+ minLength: parse_number({ report, value }),
579
+ },
580
+ },
581
+ ],
582
+ }),
583
+ maxLength: ({ report, value }) => ({
584
+ string: [
585
+ {
586
+ name: `MaxLength<${value}>`,
587
+ target: "string",
588
+ kind: "maxLength",
589
+ value: parse_number({ report, value }),
590
+ validate: `$input.length <= ${value}`,
591
+ exclusive: true,
592
+ schema: {
593
+ maxLength: parse_number({ report, value }),
594
+ },
595
+ },
596
+ ],
597
+ }),
598
+ };
599
+
600
+ /** @internal */
601
+ const parse_number = (props: {
602
+ report: (msg: string) => null;
603
+ value: string;
604
+ }): number | null => {
605
+ const parsed: number = Number(props.value);
606
+ if (isNaN(parsed) === true) return props.report(`invalid number`);
607
+ return parsed;
608
+ };
609
+
610
+ /** @internal */
611
+ const parse_integer = (props: {
612
+ report: (msg: string) => null;
613
+ unsigned: boolean;
614
+ value: string;
615
+ }): number | null => {
616
+ const parsed: number | null = parse_number(props);
617
+ if (parsed === null) return null;
618
+ else if (Math.floor(parsed) !== parsed)
619
+ return props.report(`invalid integer`);
620
+ else if (props.unsigned === true && parsed < 0)
621
+ return props.report(`invalid unsigned integer`);
622
+ return parsed;
623
+ };
624
+
625
+ /** @internal */
626
+ const FORMATS: Map<string, [string, string]> = new Map([
627
+ ...Object.entries(FormatCheatSheet).map(
628
+ ([key, value]) => [key, [key, value]] as any,
629
+ ),
630
+ ["datetime", ["date-time", `!isNaN(new Date($input).getTime())`]],
631
+ ["dateTime", ["date-time", `!isNaN(new Date($input).getTime())`]],
632
+ ]);