typia 5.0.0-dev.20230829-4 → 5.0.0-dev.20230830

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.
@@ -1,214 +1,214 @@
1
- import ts from "typescript";
2
-
3
- import { Metadata } from "../schemas/metadata/Metadata";
4
- import { MetadataAlias } from "../schemas/metadata/MetadataAlias";
5
- import { MetadataArrayType } from "../schemas/metadata/MetadataArrayType";
6
- import { MetadataObject } from "../schemas/metadata/MetadataObject";
7
- import { MetadataTupleType } from "../schemas/metadata/MetadataTupleType";
8
- import { explore_metadata } from "./internal/metadata/explore_metadata";
9
- import { iterate_metadata_collection } from "./internal/metadata/iterate_metadata_collection";
10
- import { iterate_metadata_sort } from "./internal/metadata/iterate_metadata_sort";
11
-
12
- import { ValidationPipe } from "../typings/ValidationPipe";
13
-
14
- import { MetadataCollection } from "./MetadataCollection";
15
-
16
- export namespace MetadataFactory {
17
- export type Validator = (meta: Metadata, explore: IExplore) => string[];
18
-
19
- export interface IOptions {
20
- escape: boolean;
21
- constant: boolean;
22
- absorb: boolean;
23
- validate?: Validator;
24
- onError?: (node: ts.Node | undefined, message: string) => void;
25
- }
26
-
27
- export interface IExplore {
28
- top: boolean;
29
- object: MetadataObject | null;
30
- property: string | object | null;
31
- nested: null | MetadataAlias | MetadataArrayType | MetadataTupleType;
32
- escaped: boolean;
33
- aliased: boolean;
34
- }
35
-
36
- export interface IError {
37
- name: string;
38
- explore: IExplore;
39
- messages: string[];
40
- }
41
-
42
- export const analyze =
43
- (checker: ts.TypeChecker) =>
44
- (options: IOptions) =>
45
- (collection: MetadataCollection) =>
46
- (type: ts.Type | null): ValidationPipe<Metadata, IError> => {
47
- const errors: IError[] = [];
48
- const meta: Metadata = explore_metadata(checker)(options)(
49
- collection,
50
- )(errors)(type, {
51
- top: true,
52
- object: null,
53
- property: null,
54
- nested: null,
55
- escaped: false,
56
- aliased: false,
57
- });
58
- iterate_metadata_collection(errors)(collection);
59
- iterate_metadata_sort(collection)(meta);
60
-
61
- if (options.validate)
62
- errors.push(...validate(options)(options.validate)(meta));
63
- return errors.length
64
- ? {
65
- success: false,
66
- errors,
67
- }
68
- : {
69
- success: true,
70
- data: meta,
71
- };
72
- };
73
-
74
- /**
75
- * @internal
76
- */
77
- export const soleLiteral = (value: string): Metadata => {
78
- const meta: Metadata = Metadata.initialize();
79
- meta.constants.push({
80
- values: [value],
81
- type: "string",
82
- });
83
- return meta;
84
- };
85
-
86
- const validate =
87
- (options: IOptions) =>
88
- (functor: Validator) =>
89
- (meta: Metadata): IError[] => {
90
- const visitor: IValidationVisitor = {
91
- functor,
92
- errors: [],
93
- objects: new Set(),
94
- arrays: new Set(),
95
- tuples: new Set(),
96
- aliases: new Set(),
97
- };
98
- validateMeta(options)(visitor)(meta, {
99
- object: null,
100
- property: null,
101
- nested: null,
102
- top: true,
103
- aliased: false,
104
- escaped: false,
105
- });
106
- return visitor.errors;
107
- };
108
-
109
- const validateMeta =
110
- (options: IOptions) =>
111
- (visitor: IValidationVisitor) =>
112
- (meta: Metadata, explore: IExplore) => {
113
- const result: Set<string> = new Set(visitor.functor(meta, explore));
114
- if (result.size)
115
- visitor.errors.push({
116
- name: meta.getName(),
117
- explore: { ...explore },
118
- messages: [...result],
119
- });
120
-
121
- for (const alias of meta.aliases)
122
- validateAlias(options)(visitor)(alias, explore);
123
- for (const array of meta.arrays)
124
- validateArray(options)(visitor)(array.type, explore);
125
- for (const tuple of meta.tuples)
126
- validateTuple(options)(visitor)(tuple.type, explore);
127
- for (const obj of meta.objects)
128
- validateObject(options)(visitor)(obj);
129
- for (const set of meta.sets)
130
- validateMeta(options)(visitor)(set, explore);
131
- for (const map of meta.maps) {
132
- validateMeta(options)(visitor)(map.key, explore);
133
- validateMeta(options)(visitor)(map.value, explore);
134
- }
135
-
136
- if (options.escape === true && meta.escaped !== null)
137
- validateMeta(options)(visitor)(meta.escaped.returns, {
138
- ...explore,
139
- escaped: true,
140
- });
141
- };
142
-
143
- const validateAlias =
144
- (options: IOptions) =>
145
- (visitor: IValidationVisitor) =>
146
- (alias: MetadataAlias, explore: IExplore) => {
147
- if (visitor.aliases.has(alias)) return;
148
- visitor.aliases.add(alias);
149
-
150
- validateMeta(options)(visitor)(alias.value, {
151
- ...explore,
152
- nested: alias,
153
- aliased: true,
154
- });
155
- };
156
-
157
- const validateArray =
158
- (options: IOptions) =>
159
- (visitor: IValidationVisitor) =>
160
- (array: MetadataArrayType, explore: IExplore) => {
161
- if (visitor.arrays.has(array)) return;
162
- visitor.arrays.add(array);
163
-
164
- validateMeta(options)(visitor)(array.value, {
165
- ...explore,
166
- nested: array,
167
- top: false,
168
- });
169
- };
170
-
171
- const validateTuple =
172
- (options: IOptions) =>
173
- (visitor: IValidationVisitor) =>
174
- (tuple: MetadataTupleType, explore: IExplore) => {
175
- if (visitor.tuples.has(tuple)) return;
176
- visitor.tuples.add(tuple);
177
-
178
- for (const elem of tuple.elements)
179
- validateMeta(options)(visitor)(elem, {
180
- ...explore,
181
- nested: tuple,
182
- top: false,
183
- });
184
- };
185
-
186
- const validateObject =
187
- (options: IOptions) =>
188
- (visitor: IValidationVisitor) =>
189
- (object: MetadataObject) => {
190
- if (visitor.objects.has(object)) return;
191
- visitor.objects.add(object);
192
-
193
- for (const property of object.properties)
194
- validateMeta(options)(visitor)(property.value, {
195
- object,
196
- property: property.value.isSoleLiteral()
197
- ? property.value.getSoleLiteral()!
198
- : {},
199
- nested: null,
200
- top: false,
201
- aliased: false,
202
- escaped: false,
203
- });
204
- };
205
-
206
- interface IValidationVisitor {
207
- functor: Validator;
208
- errors: IError[];
209
- objects: Set<MetadataObject>;
210
- arrays: Set<MetadataArrayType>;
211
- tuples: Set<MetadataTupleType>;
212
- aliases: Set<MetadataAlias>;
213
- }
214
- }
1
+ import ts from "typescript";
2
+
3
+ import { Metadata } from "../schemas/metadata/Metadata";
4
+ import { MetadataAlias } from "../schemas/metadata/MetadataAlias";
5
+ import { MetadataArrayType } from "../schemas/metadata/MetadataArrayType";
6
+ import { MetadataObject } from "../schemas/metadata/MetadataObject";
7
+ import { MetadataTupleType } from "../schemas/metadata/MetadataTupleType";
8
+ import { explore_metadata } from "./internal/metadata/explore_metadata";
9
+ import { iterate_metadata_collection } from "./internal/metadata/iterate_metadata_collection";
10
+ import { iterate_metadata_sort } from "./internal/metadata/iterate_metadata_sort";
11
+
12
+ import { ValidationPipe } from "../typings/ValidationPipe";
13
+
14
+ import { MetadataCollection } from "./MetadataCollection";
15
+
16
+ export namespace MetadataFactory {
17
+ export type Validator = (meta: Metadata, explore: IExplore) => string[];
18
+
19
+ export interface IOptions {
20
+ escape: boolean;
21
+ constant: boolean;
22
+ absorb: boolean;
23
+ validate?: Validator;
24
+ onError?: (node: ts.Node | undefined, message: string) => void;
25
+ }
26
+
27
+ export interface IExplore {
28
+ top: boolean;
29
+ object: MetadataObject | null;
30
+ property: string | object | null;
31
+ nested: null | MetadataAlias | MetadataArrayType | MetadataTupleType;
32
+ escaped: boolean;
33
+ aliased: boolean;
34
+ }
35
+
36
+ export interface IError {
37
+ name: string;
38
+ explore: IExplore;
39
+ messages: string[];
40
+ }
41
+
42
+ export const analyze =
43
+ (checker: ts.TypeChecker) =>
44
+ (options: IOptions) =>
45
+ (collection: MetadataCollection) =>
46
+ (type: ts.Type | null): ValidationPipe<Metadata, IError> => {
47
+ const errors: IError[] = [];
48
+ const meta: Metadata = explore_metadata(checker)(options)(
49
+ collection,
50
+ )(errors)(type, {
51
+ top: true,
52
+ object: null,
53
+ property: null,
54
+ nested: null,
55
+ escaped: false,
56
+ aliased: false,
57
+ });
58
+ iterate_metadata_collection(errors)(collection);
59
+ iterate_metadata_sort(collection)(meta);
60
+
61
+ if (options.validate)
62
+ errors.push(...validate(options)(options.validate)(meta));
63
+ return errors.length
64
+ ? {
65
+ success: false,
66
+ errors,
67
+ }
68
+ : {
69
+ success: true,
70
+ data: meta,
71
+ };
72
+ };
73
+
74
+ /**
75
+ * @internal
76
+ */
77
+ export const soleLiteral = (value: string): Metadata => {
78
+ const meta: Metadata = Metadata.initialize();
79
+ meta.constants.push({
80
+ values: [value],
81
+ type: "string",
82
+ });
83
+ return meta;
84
+ };
85
+
86
+ const validate =
87
+ (options: IOptions) =>
88
+ (functor: Validator) =>
89
+ (meta: Metadata): IError[] => {
90
+ const visitor: IValidationVisitor = {
91
+ functor,
92
+ errors: [],
93
+ objects: new Set(),
94
+ arrays: new Set(),
95
+ tuples: new Set(),
96
+ aliases: new Set(),
97
+ };
98
+ validateMeta(options)(visitor)(meta, {
99
+ object: null,
100
+ property: null,
101
+ nested: null,
102
+ top: true,
103
+ aliased: false,
104
+ escaped: false,
105
+ });
106
+ return visitor.errors;
107
+ };
108
+
109
+ const validateMeta =
110
+ (options: IOptions) =>
111
+ (visitor: IValidationVisitor) =>
112
+ (meta: Metadata, explore: IExplore) => {
113
+ const result: Set<string> = new Set(visitor.functor(meta, explore));
114
+ if (result.size)
115
+ visitor.errors.push({
116
+ name: meta.getName(),
117
+ explore: { ...explore },
118
+ messages: [...result],
119
+ });
120
+
121
+ for (const alias of meta.aliases)
122
+ validateAlias(options)(visitor)(alias, explore);
123
+ for (const array of meta.arrays)
124
+ validateArray(options)(visitor)(array.type, explore);
125
+ for (const tuple of meta.tuples)
126
+ validateTuple(options)(visitor)(tuple.type, explore);
127
+ for (const obj of meta.objects)
128
+ validateObject(options)(visitor)(obj);
129
+ for (const set of meta.sets)
130
+ validateMeta(options)(visitor)(set, explore);
131
+ for (const map of meta.maps) {
132
+ validateMeta(options)(visitor)(map.key, explore);
133
+ validateMeta(options)(visitor)(map.value, explore);
134
+ }
135
+
136
+ if (options.escape === true && meta.escaped !== null)
137
+ validateMeta(options)(visitor)(meta.escaped.returns, {
138
+ ...explore,
139
+ escaped: true,
140
+ });
141
+ };
142
+
143
+ const validateAlias =
144
+ (options: IOptions) =>
145
+ (visitor: IValidationVisitor) =>
146
+ (alias: MetadataAlias, explore: IExplore) => {
147
+ if (visitor.aliases.has(alias)) return;
148
+ visitor.aliases.add(alias);
149
+
150
+ validateMeta(options)(visitor)(alias.value, {
151
+ ...explore,
152
+ nested: alias,
153
+ aliased: true,
154
+ });
155
+ };
156
+
157
+ const validateArray =
158
+ (options: IOptions) =>
159
+ (visitor: IValidationVisitor) =>
160
+ (array: MetadataArrayType, explore: IExplore) => {
161
+ if (visitor.arrays.has(array)) return;
162
+ visitor.arrays.add(array);
163
+
164
+ validateMeta(options)(visitor)(array.value, {
165
+ ...explore,
166
+ nested: array,
167
+ top: false,
168
+ });
169
+ };
170
+
171
+ const validateTuple =
172
+ (options: IOptions) =>
173
+ (visitor: IValidationVisitor) =>
174
+ (tuple: MetadataTupleType, explore: IExplore) => {
175
+ if (visitor.tuples.has(tuple)) return;
176
+ visitor.tuples.add(tuple);
177
+
178
+ for (const elem of tuple.elements)
179
+ validateMeta(options)(visitor)(elem, {
180
+ ...explore,
181
+ nested: tuple,
182
+ top: false,
183
+ });
184
+ };
185
+
186
+ const validateObject =
187
+ (options: IOptions) =>
188
+ (visitor: IValidationVisitor) =>
189
+ (object: MetadataObject) => {
190
+ if (visitor.objects.has(object)) return;
191
+ visitor.objects.add(object);
192
+
193
+ for (const property of object.properties)
194
+ validateMeta(options)(visitor)(property.value, {
195
+ object,
196
+ property: property.key.isSoleLiteral()
197
+ ? property.key.getSoleLiteral()!
198
+ : {},
199
+ nested: null,
200
+ top: false,
201
+ aliased: false,
202
+ escaped: false,
203
+ });
204
+ };
205
+
206
+ interface IValidationVisitor {
207
+ functor: Validator;
208
+ errors: IError[];
209
+ objects: Set<MetadataObject>;
210
+ arrays: Set<MetadataArrayType>;
211
+ tuples: Set<MetadataTupleType>;
212
+ aliases: Set<MetadataAlias>;
213
+ }
214
+ }