@typia/transform 12.0.0-dev.20260307-2 → 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 (171) hide show
  1. package/lib/CallExpressionTransformer.js +8 -0
  2. package/lib/CallExpressionTransformer.js.map +1 -1
  3. package/lib/CallExpressionTransformer.mjs +8 -0
  4. package/lib/CallExpressionTransformer.mjs.map +1 -1
  5. package/lib/features/json/JsonApplicationTransformer.js +3 -4
  6. package/lib/features/json/JsonApplicationTransformer.js.map +1 -1
  7. package/lib/features/json/JsonApplicationTransformer.mjs +4 -5
  8. package/lib/features/json/JsonApplicationTransformer.mjs.map +1 -1
  9. package/lib/features/json/JsonSchemaTransformer.js +2 -8
  10. package/lib/features/json/JsonSchemaTransformer.js.map +1 -1
  11. package/lib/features/json/JsonSchemaTransformer.mjs +3 -9
  12. package/lib/features/json/JsonSchemaTransformer.mjs.map +1 -1
  13. package/lib/features/json/JsonSchemasTransformer.js +2 -8
  14. package/lib/features/json/JsonSchemasTransformer.js.map +1 -1
  15. package/lib/features/json/JsonSchemasTransformer.mjs +3 -9
  16. package/lib/features/json/JsonSchemasTransformer.mjs.map +1 -1
  17. package/lib/features/llm/LlmApplicationTransformer.js +56 -20
  18. package/lib/features/llm/LlmApplicationTransformer.js.map +1 -1
  19. package/lib/features/llm/LlmApplicationTransformer.mjs +56 -20
  20. package/lib/features/llm/LlmApplicationTransformer.mjs.map +1 -1
  21. package/lib/features/llm/LlmCoerceTransformer.d.ts +5 -0
  22. package/lib/features/llm/LlmCoerceTransformer.js +81 -0
  23. package/lib/features/llm/LlmCoerceTransformer.js.map +1 -0
  24. package/lib/features/llm/LlmCoerceTransformer.mjs +77 -0
  25. package/lib/features/llm/LlmCoerceTransformer.mjs.map +1 -0
  26. package/lib/features/llm/LlmControllerTransformer.js +12 -10
  27. package/lib/features/llm/LlmControllerTransformer.js.map +1 -1
  28. package/lib/features/llm/LlmControllerTransformer.mjs +12 -10
  29. package/lib/features/llm/LlmControllerTransformer.mjs.map +1 -1
  30. package/lib/features/llm/LlmCreateCoerceTransformer.d.ts +5 -0
  31. package/lib/features/llm/LlmCreateCoerceTransformer.js +75 -0
  32. package/lib/features/llm/LlmCreateCoerceTransformer.js.map +1 -0
  33. package/lib/features/llm/LlmCreateCoerceTransformer.mjs +71 -0
  34. package/lib/features/llm/LlmCreateCoerceTransformer.mjs.map +1 -0
  35. package/lib/features/llm/LlmCreateParseTransformer.d.ts +5 -0
  36. package/lib/features/llm/LlmCreateParseTransformer.js +75 -0
  37. package/lib/features/llm/LlmCreateParseTransformer.js.map +1 -0
  38. package/lib/features/llm/LlmCreateParseTransformer.mjs +71 -0
  39. package/lib/features/llm/LlmCreateParseTransformer.mjs.map +1 -0
  40. package/lib/features/llm/LlmParametersTransformer.d.ts +1 -1
  41. package/lib/features/llm/LlmParametersTransformer.js +3 -6
  42. package/lib/features/llm/LlmParametersTransformer.js.map +1 -1
  43. package/lib/features/llm/LlmParametersTransformer.mjs +4 -7
  44. package/lib/features/llm/LlmParametersTransformer.mjs.map +1 -1
  45. package/lib/features/llm/LlmParseTransformer.d.ts +5 -0
  46. package/lib/features/llm/LlmParseTransformer.js +81 -0
  47. package/lib/features/llm/LlmParseTransformer.js.map +1 -0
  48. package/lib/features/llm/LlmParseTransformer.mjs +77 -0
  49. package/lib/features/llm/LlmParseTransformer.mjs.map +1 -0
  50. package/lib/features/llm/LlmSchemaTransformer.d.ts +1 -1
  51. package/lib/features/llm/LlmSchemaTransformer.js +10 -25
  52. package/lib/features/llm/LlmSchemaTransformer.js.map +1 -1
  53. package/lib/features/llm/LlmSchemaTransformer.mjs +11 -26
  54. package/lib/features/llm/LlmSchemaTransformer.mjs.map +1 -1
  55. package/package.json +4 -4
  56. package/src/CallExpressionTransformer.ts +579 -571
  57. package/src/FileTransformer.ts +143 -143
  58. package/src/ITransformProps.ts +20 -20
  59. package/src/ImportTransformer.ts +262 -262
  60. package/src/NodeTransformer.ts +25 -25
  61. package/src/TransformerError.ts +85 -85
  62. package/src/TypiaGenerator.ts +172 -172
  63. package/src/features/AssertTransformer.ts +24 -24
  64. package/src/features/CreateAssertTransformer.ts +24 -24
  65. package/src/features/CreateIsTransformer.ts +18 -18
  66. package/src/features/CreateRandomTransformer.ts +42 -42
  67. package/src/features/CreateValidateTransformer.ts +18 -18
  68. package/src/features/IsTransformer.ts +18 -18
  69. package/src/features/RandomTransformer.ts +40 -40
  70. package/src/features/ValidateTransformer.ts +18 -18
  71. package/src/features/functional/FunctionalGenericTransformer.ts +55 -55
  72. package/src/features/http/CreateHttpAssertFormDataTransformer.ts +13 -13
  73. package/src/features/http/CreateHttpAssertHeadersTransformer.ts +13 -13
  74. package/src/features/http/CreateHttpAssertQueryTransformer.ts +13 -13
  75. package/src/features/http/CreateHttpFormDataTransformer.ts +13 -13
  76. package/src/features/http/CreateHttpHeadersTransformer.ts +13 -13
  77. package/src/features/http/CreateHttpIsFormDataTransformer.ts +13 -13
  78. package/src/features/http/CreateHttpIsHeadersTransformer.ts +13 -13
  79. package/src/features/http/CreateHttpIsQueryTransformer.ts +13 -13
  80. package/src/features/http/CreateHttpParameterTransformer.ts +13 -13
  81. package/src/features/http/CreateHttpQueryTransformer.ts +13 -13
  82. package/src/features/http/CreateHttpValidateFormDataTransformer.ts +13 -13
  83. package/src/features/http/CreateHttpValidateHeadersTransformer.ts +13 -13
  84. package/src/features/http/CreateHttpValidateQueryTransformer.ts +13 -13
  85. package/src/features/http/HttpAssertFormDataTransformer.ts +13 -13
  86. package/src/features/http/HttpAssertHeadersTransformer.ts +13 -13
  87. package/src/features/http/HttpAssertQueryTransformer.ts +13 -13
  88. package/src/features/http/HttpFormDataTransformer.ts +13 -13
  89. package/src/features/http/HttpHeadersTransformer.ts +13 -13
  90. package/src/features/http/HttpIsFormDataTransformer.ts +13 -13
  91. package/src/features/http/HttpIsHeadersTransformer.ts +13 -13
  92. package/src/features/http/HttpIsQueryTransformer.ts +13 -13
  93. package/src/features/http/HttpParameterTransformer.ts +13 -13
  94. package/src/features/http/HttpQueryTransformer.ts +13 -13
  95. package/src/features/http/HttpValidateFormDataTransformer.ts +13 -13
  96. package/src/features/http/HttpValidateHeadersTransformer.ts +13 -13
  97. package/src/features/http/HttpValidateQueryTransformer.ts +13 -13
  98. package/src/features/json/JsonApplicationTransformer.ts +99 -102
  99. package/src/features/json/JsonAssertParseTransformer.ts +13 -13
  100. package/src/features/json/JsonAssertStringifyTransformer.ts +13 -13
  101. package/src/features/json/JsonCreateAssertParseTransformer.ts +13 -13
  102. package/src/features/json/JsonCreateAssertStringifyTransformer.ts +13 -13
  103. package/src/features/json/JsonCreateIsParseTransformer.ts +13 -13
  104. package/src/features/json/JsonCreateIsStringifyTransformer.ts +13 -13
  105. package/src/features/json/JsonCreateStringifyTransformer.ts +13 -13
  106. package/src/features/json/JsonCreateValidateParseTransformer.ts +13 -13
  107. package/src/features/json/JsonCreateValidateStringifyProgrammer.ts +13 -13
  108. package/src/features/json/JsonIsParseTransformer.ts +13 -13
  109. package/src/features/json/JsonIsStringifyTransformer.ts +13 -13
  110. package/src/features/json/JsonSchemaTransformer.ts +120 -132
  111. package/src/features/json/JsonSchemasTransformer.ts +130 -142
  112. package/src/features/json/JsonStringifyTransformer.ts +13 -13
  113. package/src/features/json/JsonValidateParseTransformer.ts +13 -13
  114. package/src/features/json/JsonValidateStringifyTransformer.ts +13 -13
  115. package/src/features/llm/LlmApplicationTransformer.ts +224 -184
  116. package/src/features/llm/LlmCoerceTransformer.ts +95 -0
  117. package/src/features/llm/LlmControllerTransformer.ts +81 -76
  118. package/src/features/llm/LlmCreateCoerceTransformer.ts +84 -0
  119. package/src/features/llm/LlmCreateParseTransformer.ts +84 -0
  120. package/src/features/llm/LlmParametersTransformer.ts +76 -88
  121. package/src/features/llm/LlmParseTransformer.ts +95 -0
  122. package/src/features/llm/LlmSchemaTransformer.ts +87 -133
  123. package/src/features/misc/MiscAssertCloneTransformer.ts +13 -13
  124. package/src/features/misc/MiscAssertPruneTransformer.ts +13 -13
  125. package/src/features/misc/MiscCloneTransformer.ts +13 -13
  126. package/src/features/misc/MiscCreateAssertCloneTransformer.ts +13 -13
  127. package/src/features/misc/MiscCreateAssertPruneTransformer.ts +13 -13
  128. package/src/features/misc/MiscCreateCloneTransformer.ts +13 -13
  129. package/src/features/misc/MiscCreateIsCloneTransformer.ts +13 -13
  130. package/src/features/misc/MiscCreateIsPruneTransformer.ts +13 -13
  131. package/src/features/misc/MiscCreatePruneTransformer.ts +13 -13
  132. package/src/features/misc/MiscCreateValidateCloneTransformer.ts +13 -13
  133. package/src/features/misc/MiscCreateValidatePruneTransformer.ts +13 -13
  134. package/src/features/misc/MiscIsCloneTransformer.ts +13 -13
  135. package/src/features/misc/MiscIsPruneTransformer.ts +13 -13
  136. package/src/features/misc/MiscLiteralsTransformer.ts +34 -34
  137. package/src/features/misc/MiscPruneTransformer.ts +13 -13
  138. package/src/features/misc/MiscValidateCloneTransformer.ts +13 -13
  139. package/src/features/misc/MiscValidatePruneTransformer.ts +13 -13
  140. package/src/features/notations/NotationAssertGeneralTransformer.ts +19 -19
  141. package/src/features/notations/NotationCreateAssertGeneralTransformer.ts +19 -19
  142. package/src/features/notations/NotationCreateGeneralTransformer.ts +19 -19
  143. package/src/features/notations/NotationCreateIsGeneralTransformer.ts +19 -19
  144. package/src/features/notations/NotationCreateValidateGeneralTransformer.ts +19 -19
  145. package/src/features/notations/NotationGeneralTransformer.ts +18 -18
  146. package/src/features/notations/NotationIsGeneralTransformer.ts +19 -19
  147. package/src/features/notations/NotationValidateGeneralTransformer.ts +19 -19
  148. package/src/features/protobuf/ProtobufAssertDecodeTransformer.ts +13 -13
  149. package/src/features/protobuf/ProtobufAssertEncodeTransformer.ts +13 -13
  150. package/src/features/protobuf/ProtobufCreateAssertDecodeTransformer.ts +13 -13
  151. package/src/features/protobuf/ProtobufCreateAssertEncodeTransformer.ts +13 -13
  152. package/src/features/protobuf/ProtobufCreateDecodeTransformer.ts +13 -13
  153. package/src/features/protobuf/ProtobufCreateEncodeTransformer.ts +13 -13
  154. package/src/features/protobuf/ProtobufCreateIsDecodeTransformer.ts +13 -13
  155. package/src/features/protobuf/ProtobufCreateIsEncodeTransformer.ts +13 -13
  156. package/src/features/protobuf/ProtobufCreateValidateDecodeTransformer.ts +13 -13
  157. package/src/features/protobuf/ProtobufCreateValidateEncodeTransformer.ts +13 -13
  158. package/src/features/protobuf/ProtobufDecodeTransformer.ts +13 -13
  159. package/src/features/protobuf/ProtobufEncodeTransformer.ts +13 -13
  160. package/src/features/protobuf/ProtobufIsDecodeTransformer.ts +13 -13
  161. package/src/features/protobuf/ProtobufIsEncodeTransformer.ts +13 -13
  162. package/src/features/protobuf/ProtobufMessageTransformer.ts +34 -34
  163. package/src/features/protobuf/ProtobufValidateDecodeTransformer.ts +13 -13
  164. package/src/features/protobuf/ProtobufValidateEncodeTransformer.ts +13 -13
  165. package/src/features/reflect/ReflectMetadataTransformer.ts +69 -69
  166. package/src/features/reflect/ReflectNameTransformer.ts +81 -81
  167. package/src/features/reflect/ReflectSchemaTransformer.ts +65 -65
  168. package/src/features/reflect/ReflectSchemasTransformer.ts +69 -69
  169. package/src/index.ts +7 -7
  170. package/src/internal/GenericTransformer.ts +101 -101
  171. package/src/transform.ts +68 -68
@@ -1,132 +1,120 @@
1
- import {
2
- JsonSchemaProgrammer,
3
- JsonSchemasProgrammer,
4
- LiteralFactory,
5
- MetadataCollection,
6
- MetadataFactory,
7
- MetadataSchema,
8
- } from "@typia/core";
9
- import { IJsonSchemaUnit, ValidationPipe } from "@typia/interface";
10
- import ts from "typescript";
11
-
12
- import { ITransformProps } from "../../ITransformProps";
13
- import { TransformerError } from "../../TransformerError";
14
-
15
- export namespace JsonSchemaTransformer {
16
- export const transform = (
17
- props: Pick<ITransformProps, "context" | "expression">,
18
- ): ts.Expression => {
19
- if (!props.expression.typeArguments?.length)
20
- throw new TransformerError({
21
- code: "typia.json.schema",
22
- message: "no generic argument.",
23
- });
24
-
25
- //----
26
- // GET ARGUMENTS
27
- //----
28
- // VALIDATE TUPLE ARGUMENTS
29
- const top: ts.Node | undefined = props.expression.typeArguments[0];
30
- if (top === undefined || ts.isTypeNode(top) === false)
31
- return props.expression;
32
-
33
- // GET TYPES
34
- const type: ts.Type = props.context.checker.getTypeFromTypeNode(top);
35
- if (type.isTypeParameter())
36
- throw new TransformerError({
37
- code: "typia.json.schema",
38
- message: "non-specified generic argument.",
39
- });
40
-
41
- // ADDITIONAL PARAMETERS
42
- const version: "3.0" | "3.1" = getParameter<"3.0" | "3.1">({
43
- checker: props.context.checker,
44
- name: "Version",
45
- is: (str) => str === "3.0" || str === "3.1",
46
- cast: (str) => str as "3.0" | "3.1",
47
- default: () => "3.1",
48
- })(props.expression.typeArguments[1]);
49
-
50
- //----
51
- // GENERATORS
52
- //----
53
- // METADATA
54
- const analyze = (validate: boolean): MetadataSchema => {
55
- const results: ValidationPipe<MetadataSchema, MetadataFactory.IError> =
56
- MetadataFactory.analyze({
57
- checker: props.context.checker,
58
- transformer: props.context.transformer,
59
- options: {
60
- absorb: validate,
61
- constant: true,
62
- escape: true,
63
- validate:
64
- validate === true ? JsonSchemasProgrammer.validate : undefined,
65
- },
66
- components: new MetadataCollection({
67
- replace: MetadataCollection.replace,
68
- }),
69
- type,
70
- });
71
- if (results.success === false)
72
- throw TransformerError.from({
73
- code: "typia.json.schema",
74
- errors: results.errors,
75
- });
76
- return results.data;
77
- };
78
- analyze(true);
79
-
80
- // APPLICATION
81
- const app: IJsonSchemaUnit<any> = JsonSchemaProgrammer.write({
82
- version,
83
- metadata: analyze(false),
84
- });
85
- return ts.factory.createAsExpression(
86
- LiteralFactory.write(app),
87
- props.context.importer.type({
88
- file: "typia",
89
- name: "IJsonSchemaUnit",
90
- arguments: [
91
- ts.factory.createLiteralTypeNode(
92
- ts.factory.createStringLiteral(version),
93
- ),
94
- ],
95
- }),
96
- );
97
- };
98
-
99
- const getParameter =
100
- <Value>(props: {
101
- checker: ts.TypeChecker;
102
- name: string;
103
- is: (value: string) => boolean;
104
- cast: (value: string) => Value;
105
- default: () => Value;
106
- }) =>
107
- (node: ts.TypeNode | undefined): Value => {
108
- if (!node) return props.default();
109
-
110
- // CHECK LITERAL TYPE
111
- const type: ts.Type = props.checker.getTypeFromTypeNode(node);
112
- if (
113
- !type.isLiteral() &&
114
- (type.getFlags() & ts.TypeFlags.BooleanLiteral) === 0
115
- )
116
- throw new TransformerError({
117
- code: "typia.json.schema",
118
- message: `generic argument "${props.name}" must be constant.`,
119
- });
120
-
121
- // GET VALUE AND VALIDATE IT
122
- const value = type.isLiteral()
123
- ? type.value
124
- : props.checker.typeToString(type);
125
- if (typeof value !== "string" || props.is(value) === false)
126
- throw new TransformerError({
127
- code: "typia.json.schema",
128
- message: `invalid value on generic argument "${props.name}".`,
129
- });
130
- return props.cast(value);
131
- };
132
- }
1
+ import {
2
+ JsonSchemaProgrammer,
3
+ JsonSchemasProgrammer,
4
+ MetadataCollection,
5
+ MetadataFactory,
6
+ MetadataSchema,
7
+ } from "@typia/core";
8
+ import { ValidationPipe } from "@typia/interface";
9
+ import ts from "typescript";
10
+
11
+ import { ITransformProps } from "../../ITransformProps";
12
+ import { TransformerError } from "../../TransformerError";
13
+
14
+ export namespace JsonSchemaTransformer {
15
+ export const transform = (
16
+ props: Pick<ITransformProps, "context" | "expression">,
17
+ ): ts.Expression => {
18
+ if (!props.expression.typeArguments?.length)
19
+ throw new TransformerError({
20
+ code: "typia.json.schema",
21
+ message: "no generic argument.",
22
+ });
23
+
24
+ //----
25
+ // GET ARGUMENTS
26
+ //----
27
+ // VALIDATE TUPLE ARGUMENTS
28
+ const top: ts.Node | undefined = props.expression.typeArguments[0];
29
+ if (top === undefined || ts.isTypeNode(top) === false)
30
+ return props.expression;
31
+
32
+ // GET TYPES
33
+ const type: ts.Type = props.context.checker.getTypeFromTypeNode(top);
34
+ if (type.isTypeParameter())
35
+ throw new TransformerError({
36
+ code: "typia.json.schema",
37
+ message: "non-specified generic argument.",
38
+ });
39
+
40
+ // ADDITIONAL PARAMETERS
41
+ const version: "3.0" | "3.1" = getParameter<"3.0" | "3.1">({
42
+ checker: props.context.checker,
43
+ name: "Version",
44
+ is: (str) => str === "3.0" || str === "3.1",
45
+ cast: (str) => str as "3.0" | "3.1",
46
+ default: () => "3.1",
47
+ })(props.expression.typeArguments[1]);
48
+
49
+ //----
50
+ // GENERATORS
51
+ //----
52
+ // METADATA
53
+ const analyze = (validate: boolean): MetadataSchema => {
54
+ const results: ValidationPipe<MetadataSchema, MetadataFactory.IError> =
55
+ MetadataFactory.analyze({
56
+ checker: props.context.checker,
57
+ transformer: props.context.transformer,
58
+ options: {
59
+ absorb: validate,
60
+ constant: true,
61
+ escape: true,
62
+ validate:
63
+ validate === true ? JsonSchemasProgrammer.validate : undefined,
64
+ },
65
+ components: new MetadataCollection({
66
+ replace: MetadataCollection.replace,
67
+ }),
68
+ type,
69
+ });
70
+ if (results.success === false)
71
+ throw TransformerError.from({
72
+ code: "typia.json.schema",
73
+ errors: results.errors,
74
+ });
75
+ return results.data;
76
+ };
77
+ analyze(true);
78
+
79
+ // APPLICATION
80
+ return JsonSchemaProgrammer.write({
81
+ context: props.context,
82
+ version,
83
+ metadata: analyze(false),
84
+ });
85
+ };
86
+
87
+ const getParameter =
88
+ <Value>(props: {
89
+ checker: ts.TypeChecker;
90
+ name: string;
91
+ is: (value: string) => boolean;
92
+ cast: (value: string) => Value;
93
+ default: () => Value;
94
+ }) =>
95
+ (node: ts.TypeNode | undefined): Value => {
96
+ if (!node) return props.default();
97
+
98
+ // CHECK LITERAL TYPE
99
+ const type: ts.Type = props.checker.getTypeFromTypeNode(node);
100
+ if (
101
+ !type.isLiteral() &&
102
+ (type.getFlags() & ts.TypeFlags.BooleanLiteral) === 0
103
+ )
104
+ throw new TransformerError({
105
+ code: "typia.json.schema",
106
+ message: `generic argument "${props.name}" must be constant.`,
107
+ });
108
+
109
+ // GET VALUE AND VALIDATE IT
110
+ const value = type.isLiteral()
111
+ ? type.value
112
+ : props.checker.typeToString(type);
113
+ if (typeof value !== "string" || props.is(value) === false)
114
+ throw new TransformerError({
115
+ code: "typia.json.schema",
116
+ message: `invalid value on generic argument "${props.name}".`,
117
+ });
118
+ return props.cast(value);
119
+ };
120
+ }
@@ -1,142 +1,130 @@
1
- import {
2
- JsonSchemasProgrammer,
3
- LiteralFactory,
4
- MetadataCollection,
5
- MetadataFactory,
6
- MetadataSchema,
7
- } from "@typia/core";
8
- import { IJsonSchemaCollection, ValidationPipe } from "@typia/interface";
9
- import ts from "typescript";
10
-
11
- import { ITransformProps } from "../../ITransformProps";
12
- import { TransformerError } from "../../TransformerError";
13
-
14
- export namespace JsonSchemasTransformer {
15
- export const transform = (
16
- props: Pick<ITransformProps, "context" | "expression">,
17
- ): ts.Expression => {
18
- if (!props.expression.typeArguments?.length)
19
- throw new TransformerError({
20
- code: "typia.json.schemas",
21
- message: "no generic argument.",
22
- });
23
-
24
- //----
25
- // GET ARGUMENTS
26
- //----
27
- // VALIDATE TUPLE ARGUMENTS
28
- const top: ts.Node = props.expression.typeArguments[0]!;
29
- if (!ts.isTupleTypeNode(top)) return props.expression;
30
- else if (top.elements.some((child) => !ts.isTypeNode(child)))
31
- return props.expression;
32
-
33
- // GET TYPES
34
- const types: ts.Type[] = top.elements.map((child) =>
35
- props.context.checker.getTypeFromTypeNode(child as ts.TypeNode),
36
- );
37
- if (types.some((t) => t.isTypeParameter()))
38
- throw new TransformerError({
39
- code: "typia.json.schemas",
40
- message: "non-specified generic argument(s).",
41
- });
42
-
43
- // ADDITIONAL PARAMETERS
44
- const version: "3.0" | "3.1" = getParameter<"3.0" | "3.1">({
45
- checker: props.context.checker,
46
- name: "Version",
47
- is: (str) => str === "3.0" || str === "3.1",
48
- cast: (str) => str as "3.0" | "3.1",
49
- default: () => "3.1",
50
- })(props.expression.typeArguments[1]);
51
-
52
- //----
53
- // GENERATORS
54
- //----
55
- // METADATA
56
- const analyze = (validate: boolean): MetadataSchema[] => {
57
- const results: ValidationPipe<MetadataSchema, MetadataFactory.IError>[] =
58
- types.map((type) =>
59
- MetadataFactory.analyze({
60
- checker: props.context.checker,
61
- transformer: props.context.transformer,
62
- options: {
63
- absorb: validate,
64
- constant: true,
65
- escape: true,
66
- validate:
67
- validate === true ? JsonSchemasProgrammer.validate : undefined,
68
- },
69
- components: new MetadataCollection({
70
- replace: MetadataCollection.replace,
71
- }),
72
- type,
73
- }),
74
- );
75
- const metadatas: MetadataSchema[] = [];
76
- const errors: MetadataFactory.IError[] = [];
77
- for (const r of results) {
78
- if (r.success === false) errors.push(...r.errors);
79
- else metadatas.push(r.data);
80
- }
81
- if (errors.length)
82
- throw TransformerError.from({
83
- code: "typia.json.schemas",
84
- errors,
85
- });
86
- return metadatas;
87
- };
88
- analyze(true);
89
-
90
- // APPLICATION
91
- const collection: IJsonSchemaCollection<any> = JsonSchemasProgrammer.write({
92
- version,
93
- metadatas: analyze(false),
94
- });
95
- return ts.factory.createAsExpression(
96
- LiteralFactory.write(collection),
97
- props.context.importer.type({
98
- file: "typia",
99
- name: "IJsonSchemaCollection",
100
- arguments: [
101
- ts.factory.createLiteralTypeNode(
102
- ts.factory.createStringLiteral(version),
103
- ),
104
- ],
105
- }),
106
- );
107
- };
108
-
109
- const getParameter =
110
- <Value>(props: {
111
- checker: ts.TypeChecker;
112
- name: string;
113
- is: (value: string) => boolean;
114
- cast: (value: string) => Value;
115
- default: () => Value;
116
- }) =>
117
- (node: ts.TypeNode | undefined): Value => {
118
- if (!node) return props.default();
119
-
120
- // CHECK LITERAL TYPE
121
- const type: ts.Type = props.checker.getTypeFromTypeNode(node);
122
- if (
123
- !type.isLiteral() &&
124
- (type.getFlags() & ts.TypeFlags.BooleanLiteral) === 0
125
- )
126
- throw new TransformerError({
127
- code: "typia.json.schemas",
128
- message: `generic argument "${props.name}" must be constant.`,
129
- });
130
-
131
- // GET VALUE AND VALIDATE IT
132
- const value = type.isLiteral()
133
- ? type.value
134
- : props.checker.typeToString(type);
135
- if (typeof value !== "string" || props.is(value) === false)
136
- throw new TransformerError({
137
- code: "typia.json.schemas",
138
- message: `invalid value on generic argument "${props.name}".`,
139
- });
140
- return props.cast(value);
141
- };
142
- }
1
+ import {
2
+ JsonSchemasProgrammer,
3
+ MetadataCollection,
4
+ MetadataFactory,
5
+ MetadataSchema,
6
+ } from "@typia/core";
7
+ import { ValidationPipe } from "@typia/interface";
8
+ import ts from "typescript";
9
+
10
+ import { ITransformProps } from "../../ITransformProps";
11
+ import { TransformerError } from "../../TransformerError";
12
+
13
+ export namespace JsonSchemasTransformer {
14
+ export const transform = (
15
+ props: Pick<ITransformProps, "context" | "expression">,
16
+ ): ts.Expression => {
17
+ if (!props.expression.typeArguments?.length)
18
+ throw new TransformerError({
19
+ code: "typia.json.schemas",
20
+ message: "no generic argument.",
21
+ });
22
+
23
+ //----
24
+ // GET ARGUMENTS
25
+ //----
26
+ // VALIDATE TUPLE ARGUMENTS
27
+ const top: ts.Node = props.expression.typeArguments[0]!;
28
+ if (!ts.isTupleTypeNode(top)) return props.expression;
29
+ else if (top.elements.some((child) => !ts.isTypeNode(child)))
30
+ return props.expression;
31
+
32
+ // GET TYPES
33
+ const types: ts.Type[] = top.elements.map((child) =>
34
+ props.context.checker.getTypeFromTypeNode(child as ts.TypeNode),
35
+ );
36
+ if (types.some((t) => t.isTypeParameter()))
37
+ throw new TransformerError({
38
+ code: "typia.json.schemas",
39
+ message: "non-specified generic argument(s).",
40
+ });
41
+
42
+ // ADDITIONAL PARAMETERS
43
+ const version: "3.0" | "3.1" = getParameter<"3.0" | "3.1">({
44
+ checker: props.context.checker,
45
+ name: "Version",
46
+ is: (str) => str === "3.0" || str === "3.1",
47
+ cast: (str) => str as "3.0" | "3.1",
48
+ default: () => "3.1",
49
+ })(props.expression.typeArguments[1]);
50
+
51
+ //----
52
+ // GENERATORS
53
+ //----
54
+ // METADATA
55
+ const analyze = (validate: boolean): MetadataSchema[] => {
56
+ const results: ValidationPipe<MetadataSchema, MetadataFactory.IError>[] =
57
+ types.map((type) =>
58
+ MetadataFactory.analyze({
59
+ checker: props.context.checker,
60
+ transformer: props.context.transformer,
61
+ options: {
62
+ absorb: validate,
63
+ constant: true,
64
+ escape: true,
65
+ validate:
66
+ validate === true ? JsonSchemasProgrammer.validate : undefined,
67
+ },
68
+ components: new MetadataCollection({
69
+ replace: MetadataCollection.replace,
70
+ }),
71
+ type,
72
+ }),
73
+ );
74
+ const metadatas: MetadataSchema[] = [];
75
+ const errors: MetadataFactory.IError[] = [];
76
+ for (const r of results) {
77
+ if (r.success === false) errors.push(...r.errors);
78
+ else metadatas.push(r.data);
79
+ }
80
+ if (errors.length)
81
+ throw TransformerError.from({
82
+ code: "typia.json.schemas",
83
+ errors,
84
+ });
85
+ return metadatas;
86
+ };
87
+ analyze(true);
88
+
89
+ // APPLICATION
90
+ return JsonSchemasProgrammer.write({
91
+ context: props.context,
92
+ version,
93
+ metadatas: analyze(false),
94
+ });
95
+ };
96
+
97
+ const getParameter =
98
+ <Value>(props: {
99
+ checker: ts.TypeChecker;
100
+ name: string;
101
+ is: (value: string) => boolean;
102
+ cast: (value: string) => Value;
103
+ default: () => Value;
104
+ }) =>
105
+ (node: ts.TypeNode | undefined): Value => {
106
+ if (!node) return props.default();
107
+
108
+ // CHECK LITERAL TYPE
109
+ const type: ts.Type = props.checker.getTypeFromTypeNode(node);
110
+ if (
111
+ !type.isLiteral() &&
112
+ (type.getFlags() & ts.TypeFlags.BooleanLiteral) === 0
113
+ )
114
+ throw new TransformerError({
115
+ code: "typia.json.schemas",
116
+ message: `generic argument "${props.name}" must be constant.`,
117
+ });
118
+
119
+ // GET VALUE AND VALIDATE IT
120
+ const value = type.isLiteral()
121
+ ? type.value
122
+ : props.checker.typeToString(type);
123
+ if (typeof value !== "string" || props.is(value) === false)
124
+ throw new TransformerError({
125
+ code: "typia.json.schemas",
126
+ message: `invalid value on generic argument "${props.name}".`,
127
+ });
128
+ return props.cast(value);
129
+ };
130
+ }
@@ -1,13 +1,13 @@
1
- import { JsonStringifyProgrammer } from "@typia/core";
2
-
3
- import { ITransformProps } from "../../ITransformProps";
4
- import { GenericTransformer } from "../../internal/GenericTransformer";
5
-
6
- export namespace JsonStringifyTransformer {
7
- export const transform = (props: ITransformProps) =>
8
- GenericTransformer.scalar({
9
- ...props,
10
- method: "json.stringify",
11
- write: JsonStringifyProgrammer.write,
12
- });
13
- }
1
+ import { JsonStringifyProgrammer } from "@typia/core";
2
+
3
+ import { ITransformProps } from "../../ITransformProps";
4
+ import { GenericTransformer } from "../../internal/GenericTransformer";
5
+
6
+ export namespace JsonStringifyTransformer {
7
+ export const transform = (props: ITransformProps) =>
8
+ GenericTransformer.scalar({
9
+ ...props,
10
+ method: "json.stringify",
11
+ write: JsonStringifyProgrammer.write,
12
+ });
13
+ }
@@ -1,13 +1,13 @@
1
- import { JsonValidateParseProgrammer } from "@typia/core";
2
-
3
- import { ITransformProps } from "../../ITransformProps";
4
- import { GenericTransformer } from "../../internal/GenericTransformer";
5
-
6
- export namespace JsonValidateParseTransformer {
7
- export const transform = (props: ITransformProps) =>
8
- GenericTransformer.scalar({
9
- ...props,
10
- method: "json.validateParse",
11
- write: JsonValidateParseProgrammer.write,
12
- });
13
- }
1
+ import { JsonValidateParseProgrammer } from "@typia/core";
2
+
3
+ import { ITransformProps } from "../../ITransformProps";
4
+ import { GenericTransformer } from "../../internal/GenericTransformer";
5
+
6
+ export namespace JsonValidateParseTransformer {
7
+ export const transform = (props: ITransformProps) =>
8
+ GenericTransformer.scalar({
9
+ ...props,
10
+ method: "json.validateParse",
11
+ write: JsonValidateParseProgrammer.write,
12
+ });
13
+ }
@@ -1,13 +1,13 @@
1
- import { JsonValidateStringifyProgrammer } from "@typia/core";
2
-
3
- import { ITransformProps } from "../../ITransformProps";
4
- import { GenericTransformer } from "../../internal/GenericTransformer";
5
-
6
- export namespace JsonValidateStringifyTransformer {
7
- export const transform = (props: ITransformProps) =>
8
- GenericTransformer.scalar({
9
- ...props,
10
- method: "json.validateStringify",
11
- write: JsonValidateStringifyProgrammer.write,
12
- });
13
- }
1
+ import { JsonValidateStringifyProgrammer } from "@typia/core";
2
+
3
+ import { ITransformProps } from "../../ITransformProps";
4
+ import { GenericTransformer } from "../../internal/GenericTransformer";
5
+
6
+ export namespace JsonValidateStringifyTransformer {
7
+ export const transform = (props: ITransformProps) =>
8
+ GenericTransformer.scalar({
9
+ ...props,
10
+ method: "json.validateStringify",
11
+ write: JsonValidateStringifyProgrammer.write,
12
+ });
13
+ }