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.
- package/LICENSE +21 -21
- package/README.md +153 -153
- package/lib/factories/ProtobufFactory.js +1 -1
- package/lib/factories/ProtobufFactory.mjs +1 -1
- package/lib/programmers/internal/json_schema_station.d.mts +2 -2
- package/lib/programmers/internal/json_schema_station.d.ts +2 -2
- package/lib/programmers/llm/LlmApplicationProgrammer.js +5 -1
- package/lib/programmers/llm/LlmApplicationProgrammer.js.map +1 -1
- package/lib/programmers/llm/LlmApplicationProgrammer.mjs +5 -1
- package/lib/programmers/llm/LlmSchemaProgrammer.js +1 -4
- package/lib/programmers/llm/LlmSchemaProgrammer.js.map +1 -1
- package/lib/programmers/llm/LlmSchemaProgrammer.mjs +1 -35
- package/package.json +121 -121
- package/src/AssertionGuard.ts +41 -41
- package/src/CamelCase.ts +75 -75
- package/src/IRandomGenerator.ts +337 -337
- package/src/IReadableURLSearchParams.ts +9 -9
- package/src/PascalCase.ts +71 -71
- package/src/Primitive.ts +90 -90
- package/src/Resolved.ts +72 -72
- package/src/SnakeCase.ts +127 -127
- package/src/TypeGuardError.ts +216 -216
- package/src/factories/MetadataCollection.ts +270 -270
- package/src/factories/MetadataCommentTagFactory.ts +632 -632
- package/src/factories/MetadataFactory.ts +402 -402
- package/src/factories/ProtobufFactory.ts +873 -873
- package/src/functional.ts +705 -705
- package/src/http.ts +972 -972
- package/src/internal/_ProtobufReader.ts +188 -188
- package/src/internal/_ProtobufSizer.ts +137 -137
- package/src/internal/_ProtobufWriter.ts +135 -135
- package/src/internal/_jsonStringifyString.ts +42 -42
- package/src/json.ts +643 -643
- package/src/llm.ts +615 -615
- package/src/misc.ts +594 -594
- package/src/module.ts +889 -889
- package/src/notations.ts +751 -751
- package/src/programmers/FeatureProgrammer.ts +605 -605
- package/src/programmers/ImportProgrammer.ts +179 -179
- package/src/programmers/RandomProgrammer.ts +1195 -1195
- package/src/programmers/helpers/ProtobufWire.ts +34 -34
- package/src/programmers/internal/check_array_length.ts +43 -43
- package/src/programmers/internal/check_bigint.ts +46 -46
- package/src/programmers/internal/check_dynamic_key.ts +197 -197
- package/src/programmers/internal/check_dynamic_properties.ts +231 -231
- package/src/programmers/internal/check_everything.ts +21 -21
- package/src/programmers/internal/check_native.ts +23 -23
- package/src/programmers/internal/check_number.ts +108 -108
- package/src/programmers/internal/check_object.ts +72 -72
- package/src/programmers/internal/check_string.ts +46 -46
- package/src/programmers/internal/check_template.ts +46 -46
- package/src/programmers/internal/check_union_array_like.ts +331 -331
- package/src/programmers/internal/decode_union_object.ts +110 -110
- package/src/programmers/internal/feature_object_entries.ts +59 -59
- package/src/programmers/internal/json_schema_escaped.ts +78 -78
- package/src/programmers/internal/json_schema_object.ts +150 -150
- package/src/programmers/internal/json_schema_station.ts +2 -2
- package/src/programmers/internal/metadata_to_pattern.ts +40 -40
- package/src/programmers/internal/postfix_of_tuple.ts +3 -3
- package/src/programmers/internal/prune_object_properties.ts +69 -69
- package/src/programmers/internal/stringify_dynamic_properties.ts +158 -158
- package/src/programmers/internal/stringify_native.ts +5 -5
- package/src/programmers/internal/stringify_regular_properties.ts +77 -77
- package/src/programmers/internal/template_to_pattern.ts +21 -21
- package/src/programmers/internal/wrap_metadata_rest_tuple.ts +21 -21
- package/src/programmers/json/JsonStringifyProgrammer.ts +1124 -1124
- package/src/programmers/llm/LlmApplicationProgrammer.ts +10 -1
- package/src/programmers/llm/LlmSchemaProgrammer.ts +2 -7
- package/src/protobuf.ts +820 -820
- package/src/reflect.ts +46 -46
- package/src/schemas/json/IJsonApplication.ts +77 -77
- package/src/schemas/json/IJsonSchemaCollection.ts +212 -212
- package/src/schemas/json/IJsonSchemaUnit.ts +263 -263
- package/src/schemas/metadata/IMetadataTypeTag.ts +14 -14
- package/src/schemas/metadata/Metadata.ts +669 -669
- package/src/schemas/metadata/MetadataAliasType.ts +57 -57
- package/src/schemas/metadata/MetadataApplication.ts +40 -40
- package/src/schemas/metadata/MetadataArray.ts +47 -47
- package/src/schemas/metadata/MetadataArrayType.ts +51 -51
- package/src/schemas/metadata/MetadataAtomic.ts +85 -85
- package/src/schemas/metadata/MetadataEscaped.ts +45 -45
- package/src/schemas/metadata/MetadataFunction.ts +45 -45
- package/src/schemas/metadata/MetadataObject.ts +46 -46
- package/src/schemas/metadata/MetadataObjectType.ts +137 -137
- package/src/schemas/metadata/MetadataParameter.ts +52 -52
- package/src/schemas/metadata/MetadataProperty.ts +53 -53
- package/src/schemas/metadata/MetadataTemplate.ts +78 -78
- package/src/schemas/metadata/MetadataTuple.ts +28 -28
- package/src/schemas/metadata/MetadataTupleType.ts +61 -61
- package/src/tags/Constant.ts +47 -47
- package/src/tags/ContentMediaType.ts +27 -27
- package/src/tags/Default.ts +52 -52
- package/src/tags/Example.ts +56 -56
- package/src/tags/Examples.ts +56 -56
- package/src/tags/ExclusiveMaximum.ts +44 -44
- package/src/tags/ExclusiveMinimum.ts +44 -44
- package/src/tags/Format.ts +78 -78
- package/src/tags/JsonSchemaPlugin.ts +36 -36
- package/src/tags/MaxItems.ts +31 -31
- package/src/tags/MaxLength.ts +25 -25
- package/src/tags/Maximum.ts +39 -39
- package/src/tags/MinItems.ts +31 -31
- package/src/tags/MinLength.ts +25 -25
- package/src/tags/Minimum.ts +39 -39
- package/src/tags/MultipleOf.ts +42 -42
- package/src/tags/Pattern.ts +49 -49
- package/src/tags/Sequence.ts +37 -37
- package/src/tags/TagBase.ts +102 -102
- package/src/tags/Type.ts +64 -64
- package/src/tags/UniqueItems.ts +34 -34
- package/src/tags/internal/FormatCheatSheet.ts +71 -71
- package/src/transformers/ITransformOptions.ts +70 -70
- package/src/transformers/ImportTransformer.ts +253 -253
- package/src/transformers/NoTransformConfigurationError.ts +16 -16
- package/src/transformers/features/llm/LlmApplicationTransformer.ts +224 -224
- package/src/typings/Equal.ts +18 -18
|
@@ -1,402 +1,402 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
|
|
3
|
-
import { Metadata } from "../schemas/metadata/Metadata";
|
|
4
|
-
import { MetadataAliasType } from "../schemas/metadata/MetadataAliasType";
|
|
5
|
-
import { MetadataArrayType } from "../schemas/metadata/MetadataArrayType";
|
|
6
|
-
import { MetadataConstant } from "../schemas/metadata/MetadataConstant";
|
|
7
|
-
import { MetadataFunction } from "../schemas/metadata/MetadataFunction";
|
|
8
|
-
import { MetadataObject } from "../schemas/metadata/MetadataObject";
|
|
9
|
-
import { MetadataObjectType } from "../schemas/metadata/MetadataObjectType";
|
|
10
|
-
import { MetadataTupleType } from "../schemas/metadata/MetadataTupleType";
|
|
11
|
-
import { explore_metadata } from "./internal/metadata/explore_metadata";
|
|
12
|
-
import { iterate_metadata_collection } from "./internal/metadata/iterate_metadata_collection";
|
|
13
|
-
import { iterate_metadata_sort } from "./internal/metadata/iterate_metadata_sort";
|
|
14
|
-
|
|
15
|
-
import { ValidationPipe } from "../typings/ValidationPipe";
|
|
16
|
-
|
|
17
|
-
import { ExpressionFactory } from "./ExpressionFactory";
|
|
18
|
-
import { MetadataCollection } from "./MetadataCollection";
|
|
19
|
-
|
|
20
|
-
export namespace MetadataFactory {
|
|
21
|
-
export type Validator = (meta: Metadata, explore: IExplore) => string[];
|
|
22
|
-
|
|
23
|
-
export interface IProps {
|
|
24
|
-
checker: ts.TypeChecker;
|
|
25
|
-
transformer: ts.TransformationContext | undefined;
|
|
26
|
-
options: IOptions;
|
|
27
|
-
collection: MetadataCollection;
|
|
28
|
-
type: ts.Type | null;
|
|
29
|
-
}
|
|
30
|
-
export interface IOptions {
|
|
31
|
-
escape: boolean;
|
|
32
|
-
constant: boolean;
|
|
33
|
-
absorb: boolean;
|
|
34
|
-
functional?: boolean;
|
|
35
|
-
validate?: Validator;
|
|
36
|
-
onError?: (node: ts.Node | undefined, message: string) => void;
|
|
37
|
-
}
|
|
38
|
-
export interface IExplore {
|
|
39
|
-
top: boolean;
|
|
40
|
-
object: MetadataObjectType | null;
|
|
41
|
-
property: string | object | null;
|
|
42
|
-
nested: null | MetadataAliasType | MetadataArrayType | MetadataTupleType;
|
|
43
|
-
parameter: string | null;
|
|
44
|
-
output: boolean;
|
|
45
|
-
escaped: boolean;
|
|
46
|
-
aliased: boolean;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export interface IError {
|
|
50
|
-
name: string;
|
|
51
|
-
explore: IExplore;
|
|
52
|
-
messages: string[];
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export const analyze = (props: IProps): ValidationPipe<Metadata, IError> => {
|
|
56
|
-
const errors: IError[] = [];
|
|
57
|
-
const metadata: Metadata = explore_metadata({
|
|
58
|
-
...props,
|
|
59
|
-
errors,
|
|
60
|
-
explore: {
|
|
61
|
-
top: true,
|
|
62
|
-
object: null,
|
|
63
|
-
property: null,
|
|
64
|
-
parameter: null,
|
|
65
|
-
nested: null,
|
|
66
|
-
aliased: false,
|
|
67
|
-
escaped: false,
|
|
68
|
-
output: false,
|
|
69
|
-
},
|
|
70
|
-
intersected: false,
|
|
71
|
-
});
|
|
72
|
-
iterate_metadata_collection({
|
|
73
|
-
errors,
|
|
74
|
-
collection: props.collection,
|
|
75
|
-
});
|
|
76
|
-
iterate_metadata_sort({
|
|
77
|
-
collection: props.collection,
|
|
78
|
-
metadata: metadata,
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
if (props.options.validate)
|
|
82
|
-
errors.push(
|
|
83
|
-
...validate({
|
|
84
|
-
transformer: props.transformer,
|
|
85
|
-
options: props.options,
|
|
86
|
-
functor: props.options.validate,
|
|
87
|
-
metadata,
|
|
88
|
-
}),
|
|
89
|
-
);
|
|
90
|
-
return errors.length
|
|
91
|
-
? {
|
|
92
|
-
success: false,
|
|
93
|
-
errors,
|
|
94
|
-
}
|
|
95
|
-
: {
|
|
96
|
-
success: true,
|
|
97
|
-
data: metadata,
|
|
98
|
-
};
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
/** @internal */
|
|
102
|
-
export const soleLiteral = (value: string): Metadata => {
|
|
103
|
-
const meta: Metadata = Metadata.initialize();
|
|
104
|
-
meta.constants.push(
|
|
105
|
-
MetadataConstant.from({
|
|
106
|
-
values: [
|
|
107
|
-
{
|
|
108
|
-
value,
|
|
109
|
-
tags: [],
|
|
110
|
-
},
|
|
111
|
-
],
|
|
112
|
-
type: "string",
|
|
113
|
-
}),
|
|
114
|
-
);
|
|
115
|
-
return meta;
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
export const validate = (props: {
|
|
119
|
-
transformer?: ts.TransformationContext;
|
|
120
|
-
options: IOptions;
|
|
121
|
-
functor: Validator;
|
|
122
|
-
metadata: Metadata;
|
|
123
|
-
}): IError[] => {
|
|
124
|
-
const visitor: IValidationVisitor = {
|
|
125
|
-
functor: props.functor,
|
|
126
|
-
errors: [],
|
|
127
|
-
objects: new Set(),
|
|
128
|
-
arrays: new Set(),
|
|
129
|
-
tuples: new Set(),
|
|
130
|
-
aliases: new Set(),
|
|
131
|
-
functions: new Set(),
|
|
132
|
-
};
|
|
133
|
-
validateMeta({
|
|
134
|
-
...props,
|
|
135
|
-
visitor,
|
|
136
|
-
explore: {
|
|
137
|
-
object: null,
|
|
138
|
-
property: null,
|
|
139
|
-
parameter: null,
|
|
140
|
-
nested: null,
|
|
141
|
-
top: true,
|
|
142
|
-
aliased: false,
|
|
143
|
-
escaped: false,
|
|
144
|
-
output: false,
|
|
145
|
-
},
|
|
146
|
-
});
|
|
147
|
-
return visitor.errors;
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
const validateMeta = (props: {
|
|
151
|
-
options: IOptions;
|
|
152
|
-
visitor: IValidationVisitor;
|
|
153
|
-
metadata: Metadata;
|
|
154
|
-
explore: IExplore;
|
|
155
|
-
}) => {
|
|
156
|
-
const result: string[] = [];
|
|
157
|
-
for (const atomic of props.metadata.atomics)
|
|
158
|
-
for (const row of atomic.tags)
|
|
159
|
-
for (const tag of row.filter(
|
|
160
|
-
(t) => t.validate !== undefined && t.predicate === undefined,
|
|
161
|
-
))
|
|
162
|
-
try {
|
|
163
|
-
tag.predicate = ExpressionFactory.transpile({
|
|
164
|
-
script: tag.validate!,
|
|
165
|
-
});
|
|
166
|
-
} catch {
|
|
167
|
-
result.push(
|
|
168
|
-
`Unable to transpile type tag script: ${JSON.stringify(
|
|
169
|
-
tag.validate,
|
|
170
|
-
)}`,
|
|
171
|
-
);
|
|
172
|
-
tag.predicate = () => ts.factory.createTrue();
|
|
173
|
-
}
|
|
174
|
-
result.push(...props.visitor.functor(props.metadata, props.explore));
|
|
175
|
-
if (result.length)
|
|
176
|
-
props.visitor.errors.push({
|
|
177
|
-
name: props.metadata.getName(),
|
|
178
|
-
explore: { ...props.explore },
|
|
179
|
-
messages: [...new Set(result)],
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
for (const alias of props.metadata.aliases)
|
|
183
|
-
validateAlias({
|
|
184
|
-
...props,
|
|
185
|
-
alias: alias.type,
|
|
186
|
-
});
|
|
187
|
-
for (const array of props.metadata.arrays)
|
|
188
|
-
validateArray({
|
|
189
|
-
...props,
|
|
190
|
-
array: array.type,
|
|
191
|
-
});
|
|
192
|
-
for (const tuple of props.metadata.tuples)
|
|
193
|
-
validateTuple({
|
|
194
|
-
...props,
|
|
195
|
-
tuple: tuple.type,
|
|
196
|
-
});
|
|
197
|
-
for (const object of props.metadata.objects)
|
|
198
|
-
validateObject({
|
|
199
|
-
...props,
|
|
200
|
-
object: object.type,
|
|
201
|
-
});
|
|
202
|
-
for (const func of props.metadata.functions)
|
|
203
|
-
validateFunction({
|
|
204
|
-
...props,
|
|
205
|
-
function: func,
|
|
206
|
-
});
|
|
207
|
-
for (const set of props.metadata.sets)
|
|
208
|
-
validateMeta({
|
|
209
|
-
...props,
|
|
210
|
-
metadata: set.value,
|
|
211
|
-
});
|
|
212
|
-
for (const map of props.metadata.maps) {
|
|
213
|
-
validateMeta({
|
|
214
|
-
...props,
|
|
215
|
-
metadata: map.key,
|
|
216
|
-
});
|
|
217
|
-
validateMeta({
|
|
218
|
-
...props,
|
|
219
|
-
metadata: map.value,
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
if (props.options.escape === true && props.metadata.escaped !== null)
|
|
224
|
-
validateMeta({
|
|
225
|
-
...props,
|
|
226
|
-
metadata: props.metadata.escaped.returns,
|
|
227
|
-
explore: {
|
|
228
|
-
...props.explore,
|
|
229
|
-
escaped: true,
|
|
230
|
-
},
|
|
231
|
-
});
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
const validateAlias = (props: {
|
|
235
|
-
transformer?: ts.TransformationContext;
|
|
236
|
-
options: IOptions;
|
|
237
|
-
visitor: IValidationVisitor;
|
|
238
|
-
alias: MetadataAliasType;
|
|
239
|
-
explore: IExplore;
|
|
240
|
-
}) => {
|
|
241
|
-
if (props.visitor.aliases.has(props.alias)) return;
|
|
242
|
-
props.visitor.aliases.add(props.alias);
|
|
243
|
-
|
|
244
|
-
validateMeta({
|
|
245
|
-
...props,
|
|
246
|
-
metadata: props.alias.value,
|
|
247
|
-
explore: {
|
|
248
|
-
...props.explore,
|
|
249
|
-
nested: props.alias,
|
|
250
|
-
aliased: true,
|
|
251
|
-
},
|
|
252
|
-
});
|
|
253
|
-
};
|
|
254
|
-
|
|
255
|
-
const validateArray = (props: {
|
|
256
|
-
transformer?: ts.TransformationContext;
|
|
257
|
-
options: IOptions;
|
|
258
|
-
visitor: IValidationVisitor;
|
|
259
|
-
array: MetadataArrayType;
|
|
260
|
-
explore: IExplore;
|
|
261
|
-
}) => {
|
|
262
|
-
if (props.visitor.arrays.has(props.array)) return;
|
|
263
|
-
props.visitor.arrays.add(props.array);
|
|
264
|
-
|
|
265
|
-
validateMeta({
|
|
266
|
-
...props,
|
|
267
|
-
metadata: props.array.value,
|
|
268
|
-
explore: {
|
|
269
|
-
...props.explore,
|
|
270
|
-
nested: props.array,
|
|
271
|
-
top: false,
|
|
272
|
-
},
|
|
273
|
-
});
|
|
274
|
-
};
|
|
275
|
-
|
|
276
|
-
const validateTuple = (props: {
|
|
277
|
-
transformer?: ts.TransformationContext;
|
|
278
|
-
options: IOptions;
|
|
279
|
-
visitor: IValidationVisitor;
|
|
280
|
-
tuple: MetadataTupleType;
|
|
281
|
-
explore: IExplore;
|
|
282
|
-
}) => {
|
|
283
|
-
if (props.visitor.tuples.has(props.tuple)) return;
|
|
284
|
-
props.visitor.tuples.add(props.tuple);
|
|
285
|
-
|
|
286
|
-
for (const elem of props.tuple.elements)
|
|
287
|
-
validateMeta({
|
|
288
|
-
...props,
|
|
289
|
-
metadata: elem,
|
|
290
|
-
explore: {
|
|
291
|
-
...props.explore,
|
|
292
|
-
nested: props.tuple,
|
|
293
|
-
top: false,
|
|
294
|
-
},
|
|
295
|
-
});
|
|
296
|
-
};
|
|
297
|
-
|
|
298
|
-
const validateObject = (props: {
|
|
299
|
-
transformer?: ts.TransformationContext;
|
|
300
|
-
options: IOptions;
|
|
301
|
-
visitor: IValidationVisitor;
|
|
302
|
-
object: MetadataObjectType;
|
|
303
|
-
}) => {
|
|
304
|
-
if (props.visitor.objects.has(props.object)) return;
|
|
305
|
-
props.visitor.objects.add(props.object);
|
|
306
|
-
|
|
307
|
-
if (props.options.validate) {
|
|
308
|
-
const explore: IExplore = {
|
|
309
|
-
object: props.object,
|
|
310
|
-
top: false,
|
|
311
|
-
property: null,
|
|
312
|
-
parameter: null,
|
|
313
|
-
nested: null,
|
|
314
|
-
aliased: false,
|
|
315
|
-
escaped: false,
|
|
316
|
-
output: false,
|
|
317
|
-
};
|
|
318
|
-
const errors: string[] = props.options.validate(
|
|
319
|
-
Metadata.create({
|
|
320
|
-
...Metadata.initialize(),
|
|
321
|
-
objects: [
|
|
322
|
-
MetadataObject.create({
|
|
323
|
-
type: props.object,
|
|
324
|
-
tags: [],
|
|
325
|
-
}),
|
|
326
|
-
],
|
|
327
|
-
}),
|
|
328
|
-
explore,
|
|
329
|
-
);
|
|
330
|
-
if (errors.length)
|
|
331
|
-
props.visitor.errors.push({
|
|
332
|
-
name: props.object.name,
|
|
333
|
-
explore,
|
|
334
|
-
messages: [...new Set(errors)],
|
|
335
|
-
});
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
for (const property of props.object.properties)
|
|
339
|
-
validateMeta({
|
|
340
|
-
...props,
|
|
341
|
-
metadata: property.value,
|
|
342
|
-
explore: {
|
|
343
|
-
object: props.object,
|
|
344
|
-
property: property.key.isSoleLiteral()
|
|
345
|
-
? property.key.getSoleLiteral()!
|
|
346
|
-
: {},
|
|
347
|
-
parameter: null,
|
|
348
|
-
nested: null,
|
|
349
|
-
top: false,
|
|
350
|
-
aliased: false,
|
|
351
|
-
escaped: false,
|
|
352
|
-
output: false,
|
|
353
|
-
},
|
|
354
|
-
});
|
|
355
|
-
};
|
|
356
|
-
|
|
357
|
-
const validateFunction = (props: {
|
|
358
|
-
transformer?: ts.TransformationContext;
|
|
359
|
-
options: IOptions;
|
|
360
|
-
visitor: IValidationVisitor;
|
|
361
|
-
function: MetadataFunction;
|
|
362
|
-
explore: IExplore;
|
|
363
|
-
}) => {
|
|
364
|
-
if (props.visitor.functions.has(props.function)) return;
|
|
365
|
-
props.visitor.functions.add(props.function);
|
|
366
|
-
|
|
367
|
-
for (const param of props.function.parameters)
|
|
368
|
-
validateMeta({
|
|
369
|
-
...props,
|
|
370
|
-
metadata: param.type,
|
|
371
|
-
explore: {
|
|
372
|
-
...props.explore,
|
|
373
|
-
parameter: param.name,
|
|
374
|
-
nested: null,
|
|
375
|
-
top: false,
|
|
376
|
-
output: false,
|
|
377
|
-
},
|
|
378
|
-
});
|
|
379
|
-
if (props.function.output)
|
|
380
|
-
validateMeta({
|
|
381
|
-
...props,
|
|
382
|
-
metadata: props.function.output,
|
|
383
|
-
explore: {
|
|
384
|
-
...props.explore,
|
|
385
|
-
parameter: null,
|
|
386
|
-
nested: null,
|
|
387
|
-
top: false,
|
|
388
|
-
output: true,
|
|
389
|
-
},
|
|
390
|
-
});
|
|
391
|
-
};
|
|
392
|
-
|
|
393
|
-
interface IValidationVisitor {
|
|
394
|
-
functor: Validator;
|
|
395
|
-
errors: IError[];
|
|
396
|
-
objects: Set<MetadataObjectType>;
|
|
397
|
-
arrays: Set<MetadataArrayType>;
|
|
398
|
-
tuples: Set<MetadataTupleType>;
|
|
399
|
-
aliases: Set<MetadataAliasType>;
|
|
400
|
-
functions: Set<MetadataFunction>;
|
|
401
|
-
}
|
|
402
|
-
}
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { Metadata } from "../schemas/metadata/Metadata";
|
|
4
|
+
import { MetadataAliasType } from "../schemas/metadata/MetadataAliasType";
|
|
5
|
+
import { MetadataArrayType } from "../schemas/metadata/MetadataArrayType";
|
|
6
|
+
import { MetadataConstant } from "../schemas/metadata/MetadataConstant";
|
|
7
|
+
import { MetadataFunction } from "../schemas/metadata/MetadataFunction";
|
|
8
|
+
import { MetadataObject } from "../schemas/metadata/MetadataObject";
|
|
9
|
+
import { MetadataObjectType } from "../schemas/metadata/MetadataObjectType";
|
|
10
|
+
import { MetadataTupleType } from "../schemas/metadata/MetadataTupleType";
|
|
11
|
+
import { explore_metadata } from "./internal/metadata/explore_metadata";
|
|
12
|
+
import { iterate_metadata_collection } from "./internal/metadata/iterate_metadata_collection";
|
|
13
|
+
import { iterate_metadata_sort } from "./internal/metadata/iterate_metadata_sort";
|
|
14
|
+
|
|
15
|
+
import { ValidationPipe } from "../typings/ValidationPipe";
|
|
16
|
+
|
|
17
|
+
import { ExpressionFactory } from "./ExpressionFactory";
|
|
18
|
+
import { MetadataCollection } from "./MetadataCollection";
|
|
19
|
+
|
|
20
|
+
export namespace MetadataFactory {
|
|
21
|
+
export type Validator = (meta: Metadata, explore: IExplore) => string[];
|
|
22
|
+
|
|
23
|
+
export interface IProps {
|
|
24
|
+
checker: ts.TypeChecker;
|
|
25
|
+
transformer: ts.TransformationContext | undefined;
|
|
26
|
+
options: IOptions;
|
|
27
|
+
collection: MetadataCollection;
|
|
28
|
+
type: ts.Type | null;
|
|
29
|
+
}
|
|
30
|
+
export interface IOptions {
|
|
31
|
+
escape: boolean;
|
|
32
|
+
constant: boolean;
|
|
33
|
+
absorb: boolean;
|
|
34
|
+
functional?: boolean;
|
|
35
|
+
validate?: Validator;
|
|
36
|
+
onError?: (node: ts.Node | undefined, message: string) => void;
|
|
37
|
+
}
|
|
38
|
+
export interface IExplore {
|
|
39
|
+
top: boolean;
|
|
40
|
+
object: MetadataObjectType | null;
|
|
41
|
+
property: string | object | null;
|
|
42
|
+
nested: null | MetadataAliasType | MetadataArrayType | MetadataTupleType;
|
|
43
|
+
parameter: string | null;
|
|
44
|
+
output: boolean;
|
|
45
|
+
escaped: boolean;
|
|
46
|
+
aliased: boolean;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface IError {
|
|
50
|
+
name: string;
|
|
51
|
+
explore: IExplore;
|
|
52
|
+
messages: string[];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const analyze = (props: IProps): ValidationPipe<Metadata, IError> => {
|
|
56
|
+
const errors: IError[] = [];
|
|
57
|
+
const metadata: Metadata = explore_metadata({
|
|
58
|
+
...props,
|
|
59
|
+
errors,
|
|
60
|
+
explore: {
|
|
61
|
+
top: true,
|
|
62
|
+
object: null,
|
|
63
|
+
property: null,
|
|
64
|
+
parameter: null,
|
|
65
|
+
nested: null,
|
|
66
|
+
aliased: false,
|
|
67
|
+
escaped: false,
|
|
68
|
+
output: false,
|
|
69
|
+
},
|
|
70
|
+
intersected: false,
|
|
71
|
+
});
|
|
72
|
+
iterate_metadata_collection({
|
|
73
|
+
errors,
|
|
74
|
+
collection: props.collection,
|
|
75
|
+
});
|
|
76
|
+
iterate_metadata_sort({
|
|
77
|
+
collection: props.collection,
|
|
78
|
+
metadata: metadata,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
if (props.options.validate)
|
|
82
|
+
errors.push(
|
|
83
|
+
...validate({
|
|
84
|
+
transformer: props.transformer,
|
|
85
|
+
options: props.options,
|
|
86
|
+
functor: props.options.validate,
|
|
87
|
+
metadata,
|
|
88
|
+
}),
|
|
89
|
+
);
|
|
90
|
+
return errors.length
|
|
91
|
+
? {
|
|
92
|
+
success: false,
|
|
93
|
+
errors,
|
|
94
|
+
}
|
|
95
|
+
: {
|
|
96
|
+
success: true,
|
|
97
|
+
data: metadata,
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/** @internal */
|
|
102
|
+
export const soleLiteral = (value: string): Metadata => {
|
|
103
|
+
const meta: Metadata = Metadata.initialize();
|
|
104
|
+
meta.constants.push(
|
|
105
|
+
MetadataConstant.from({
|
|
106
|
+
values: [
|
|
107
|
+
{
|
|
108
|
+
value,
|
|
109
|
+
tags: [],
|
|
110
|
+
},
|
|
111
|
+
],
|
|
112
|
+
type: "string",
|
|
113
|
+
}),
|
|
114
|
+
);
|
|
115
|
+
return meta;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
export const validate = (props: {
|
|
119
|
+
transformer?: ts.TransformationContext;
|
|
120
|
+
options: IOptions;
|
|
121
|
+
functor: Validator;
|
|
122
|
+
metadata: Metadata;
|
|
123
|
+
}): IError[] => {
|
|
124
|
+
const visitor: IValidationVisitor = {
|
|
125
|
+
functor: props.functor,
|
|
126
|
+
errors: [],
|
|
127
|
+
objects: new Set(),
|
|
128
|
+
arrays: new Set(),
|
|
129
|
+
tuples: new Set(),
|
|
130
|
+
aliases: new Set(),
|
|
131
|
+
functions: new Set(),
|
|
132
|
+
};
|
|
133
|
+
validateMeta({
|
|
134
|
+
...props,
|
|
135
|
+
visitor,
|
|
136
|
+
explore: {
|
|
137
|
+
object: null,
|
|
138
|
+
property: null,
|
|
139
|
+
parameter: null,
|
|
140
|
+
nested: null,
|
|
141
|
+
top: true,
|
|
142
|
+
aliased: false,
|
|
143
|
+
escaped: false,
|
|
144
|
+
output: false,
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
return visitor.errors;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const validateMeta = (props: {
|
|
151
|
+
options: IOptions;
|
|
152
|
+
visitor: IValidationVisitor;
|
|
153
|
+
metadata: Metadata;
|
|
154
|
+
explore: IExplore;
|
|
155
|
+
}) => {
|
|
156
|
+
const result: string[] = [];
|
|
157
|
+
for (const atomic of props.metadata.atomics)
|
|
158
|
+
for (const row of atomic.tags)
|
|
159
|
+
for (const tag of row.filter(
|
|
160
|
+
(t) => t.validate !== undefined && t.predicate === undefined,
|
|
161
|
+
))
|
|
162
|
+
try {
|
|
163
|
+
tag.predicate = ExpressionFactory.transpile({
|
|
164
|
+
script: tag.validate!,
|
|
165
|
+
});
|
|
166
|
+
} catch {
|
|
167
|
+
result.push(
|
|
168
|
+
`Unable to transpile type tag script: ${JSON.stringify(
|
|
169
|
+
tag.validate,
|
|
170
|
+
)}`,
|
|
171
|
+
);
|
|
172
|
+
tag.predicate = () => ts.factory.createTrue();
|
|
173
|
+
}
|
|
174
|
+
result.push(...props.visitor.functor(props.metadata, props.explore));
|
|
175
|
+
if (result.length)
|
|
176
|
+
props.visitor.errors.push({
|
|
177
|
+
name: props.metadata.getName(),
|
|
178
|
+
explore: { ...props.explore },
|
|
179
|
+
messages: [...new Set(result)],
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
for (const alias of props.metadata.aliases)
|
|
183
|
+
validateAlias({
|
|
184
|
+
...props,
|
|
185
|
+
alias: alias.type,
|
|
186
|
+
});
|
|
187
|
+
for (const array of props.metadata.arrays)
|
|
188
|
+
validateArray({
|
|
189
|
+
...props,
|
|
190
|
+
array: array.type,
|
|
191
|
+
});
|
|
192
|
+
for (const tuple of props.metadata.tuples)
|
|
193
|
+
validateTuple({
|
|
194
|
+
...props,
|
|
195
|
+
tuple: tuple.type,
|
|
196
|
+
});
|
|
197
|
+
for (const object of props.metadata.objects)
|
|
198
|
+
validateObject({
|
|
199
|
+
...props,
|
|
200
|
+
object: object.type,
|
|
201
|
+
});
|
|
202
|
+
for (const func of props.metadata.functions)
|
|
203
|
+
validateFunction({
|
|
204
|
+
...props,
|
|
205
|
+
function: func,
|
|
206
|
+
});
|
|
207
|
+
for (const set of props.metadata.sets)
|
|
208
|
+
validateMeta({
|
|
209
|
+
...props,
|
|
210
|
+
metadata: set.value,
|
|
211
|
+
});
|
|
212
|
+
for (const map of props.metadata.maps) {
|
|
213
|
+
validateMeta({
|
|
214
|
+
...props,
|
|
215
|
+
metadata: map.key,
|
|
216
|
+
});
|
|
217
|
+
validateMeta({
|
|
218
|
+
...props,
|
|
219
|
+
metadata: map.value,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (props.options.escape === true && props.metadata.escaped !== null)
|
|
224
|
+
validateMeta({
|
|
225
|
+
...props,
|
|
226
|
+
metadata: props.metadata.escaped.returns,
|
|
227
|
+
explore: {
|
|
228
|
+
...props.explore,
|
|
229
|
+
escaped: true,
|
|
230
|
+
},
|
|
231
|
+
});
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const validateAlias = (props: {
|
|
235
|
+
transformer?: ts.TransformationContext;
|
|
236
|
+
options: IOptions;
|
|
237
|
+
visitor: IValidationVisitor;
|
|
238
|
+
alias: MetadataAliasType;
|
|
239
|
+
explore: IExplore;
|
|
240
|
+
}) => {
|
|
241
|
+
if (props.visitor.aliases.has(props.alias)) return;
|
|
242
|
+
props.visitor.aliases.add(props.alias);
|
|
243
|
+
|
|
244
|
+
validateMeta({
|
|
245
|
+
...props,
|
|
246
|
+
metadata: props.alias.value,
|
|
247
|
+
explore: {
|
|
248
|
+
...props.explore,
|
|
249
|
+
nested: props.alias,
|
|
250
|
+
aliased: true,
|
|
251
|
+
},
|
|
252
|
+
});
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
const validateArray = (props: {
|
|
256
|
+
transformer?: ts.TransformationContext;
|
|
257
|
+
options: IOptions;
|
|
258
|
+
visitor: IValidationVisitor;
|
|
259
|
+
array: MetadataArrayType;
|
|
260
|
+
explore: IExplore;
|
|
261
|
+
}) => {
|
|
262
|
+
if (props.visitor.arrays.has(props.array)) return;
|
|
263
|
+
props.visitor.arrays.add(props.array);
|
|
264
|
+
|
|
265
|
+
validateMeta({
|
|
266
|
+
...props,
|
|
267
|
+
metadata: props.array.value,
|
|
268
|
+
explore: {
|
|
269
|
+
...props.explore,
|
|
270
|
+
nested: props.array,
|
|
271
|
+
top: false,
|
|
272
|
+
},
|
|
273
|
+
});
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
const validateTuple = (props: {
|
|
277
|
+
transformer?: ts.TransformationContext;
|
|
278
|
+
options: IOptions;
|
|
279
|
+
visitor: IValidationVisitor;
|
|
280
|
+
tuple: MetadataTupleType;
|
|
281
|
+
explore: IExplore;
|
|
282
|
+
}) => {
|
|
283
|
+
if (props.visitor.tuples.has(props.tuple)) return;
|
|
284
|
+
props.visitor.tuples.add(props.tuple);
|
|
285
|
+
|
|
286
|
+
for (const elem of props.tuple.elements)
|
|
287
|
+
validateMeta({
|
|
288
|
+
...props,
|
|
289
|
+
metadata: elem,
|
|
290
|
+
explore: {
|
|
291
|
+
...props.explore,
|
|
292
|
+
nested: props.tuple,
|
|
293
|
+
top: false,
|
|
294
|
+
},
|
|
295
|
+
});
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
const validateObject = (props: {
|
|
299
|
+
transformer?: ts.TransformationContext;
|
|
300
|
+
options: IOptions;
|
|
301
|
+
visitor: IValidationVisitor;
|
|
302
|
+
object: MetadataObjectType;
|
|
303
|
+
}) => {
|
|
304
|
+
if (props.visitor.objects.has(props.object)) return;
|
|
305
|
+
props.visitor.objects.add(props.object);
|
|
306
|
+
|
|
307
|
+
if (props.options.validate) {
|
|
308
|
+
const explore: IExplore = {
|
|
309
|
+
object: props.object,
|
|
310
|
+
top: false,
|
|
311
|
+
property: null,
|
|
312
|
+
parameter: null,
|
|
313
|
+
nested: null,
|
|
314
|
+
aliased: false,
|
|
315
|
+
escaped: false,
|
|
316
|
+
output: false,
|
|
317
|
+
};
|
|
318
|
+
const errors: string[] = props.options.validate(
|
|
319
|
+
Metadata.create({
|
|
320
|
+
...Metadata.initialize(),
|
|
321
|
+
objects: [
|
|
322
|
+
MetadataObject.create({
|
|
323
|
+
type: props.object,
|
|
324
|
+
tags: [],
|
|
325
|
+
}),
|
|
326
|
+
],
|
|
327
|
+
}),
|
|
328
|
+
explore,
|
|
329
|
+
);
|
|
330
|
+
if (errors.length)
|
|
331
|
+
props.visitor.errors.push({
|
|
332
|
+
name: props.object.name,
|
|
333
|
+
explore,
|
|
334
|
+
messages: [...new Set(errors)],
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
for (const property of props.object.properties)
|
|
339
|
+
validateMeta({
|
|
340
|
+
...props,
|
|
341
|
+
metadata: property.value,
|
|
342
|
+
explore: {
|
|
343
|
+
object: props.object,
|
|
344
|
+
property: property.key.isSoleLiteral()
|
|
345
|
+
? property.key.getSoleLiteral()!
|
|
346
|
+
: {},
|
|
347
|
+
parameter: null,
|
|
348
|
+
nested: null,
|
|
349
|
+
top: false,
|
|
350
|
+
aliased: false,
|
|
351
|
+
escaped: false,
|
|
352
|
+
output: false,
|
|
353
|
+
},
|
|
354
|
+
});
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
const validateFunction = (props: {
|
|
358
|
+
transformer?: ts.TransformationContext;
|
|
359
|
+
options: IOptions;
|
|
360
|
+
visitor: IValidationVisitor;
|
|
361
|
+
function: MetadataFunction;
|
|
362
|
+
explore: IExplore;
|
|
363
|
+
}) => {
|
|
364
|
+
if (props.visitor.functions.has(props.function)) return;
|
|
365
|
+
props.visitor.functions.add(props.function);
|
|
366
|
+
|
|
367
|
+
for (const param of props.function.parameters)
|
|
368
|
+
validateMeta({
|
|
369
|
+
...props,
|
|
370
|
+
metadata: param.type,
|
|
371
|
+
explore: {
|
|
372
|
+
...props.explore,
|
|
373
|
+
parameter: param.name,
|
|
374
|
+
nested: null,
|
|
375
|
+
top: false,
|
|
376
|
+
output: false,
|
|
377
|
+
},
|
|
378
|
+
});
|
|
379
|
+
if (props.function.output)
|
|
380
|
+
validateMeta({
|
|
381
|
+
...props,
|
|
382
|
+
metadata: props.function.output,
|
|
383
|
+
explore: {
|
|
384
|
+
...props.explore,
|
|
385
|
+
parameter: null,
|
|
386
|
+
nested: null,
|
|
387
|
+
top: false,
|
|
388
|
+
output: true,
|
|
389
|
+
},
|
|
390
|
+
});
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
interface IValidationVisitor {
|
|
394
|
+
functor: Validator;
|
|
395
|
+
errors: IError[];
|
|
396
|
+
objects: Set<MetadataObjectType>;
|
|
397
|
+
arrays: Set<MetadataArrayType>;
|
|
398
|
+
tuples: Set<MetadataTupleType>;
|
|
399
|
+
aliases: Set<MetadataAliasType>;
|
|
400
|
+
functions: Set<MetadataFunction>;
|
|
401
|
+
}
|
|
402
|
+
}
|