@tsed/schema 8.19.2 → 8.19.4
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/lib/esm/components/open-spec/nullableMapper.js +6 -2
- package/lib/esm/constants/OperationVerbs.js +35 -0
- package/lib/esm/constants/VendorKeys.js +29 -0
- package/lib/esm/constants/httpStatusMessages.js +59 -0
- package/lib/esm/constants/jsonSchemaProperties.js +55 -0
- package/lib/esm/decorators/class/children.js +93 -5
- package/lib/esm/decorators/class/discriminatorValue.js +124 -20
- package/lib/esm/decorators/collections/collectionContains.js +91 -10
- package/lib/esm/decorators/collections/maxItems.js +97 -27
- package/lib/esm/decorators/collections/maxProperties.js +96 -30
- package/lib/esm/decorators/collections/minItems.js +122 -27
- package/lib/esm/decorators/collections/minProperties.js +132 -31
- package/lib/esm/decorators/common/additionalProperties.js +59 -4
- package/lib/esm/decorators/common/allOf.js +61 -6
- package/lib/esm/decorators/common/allow.js +56 -12
- package/lib/esm/decorators/common/any.js +58 -18
- package/lib/esm/decorators/common/anyOf.js +65 -9
- package/lib/esm/decorators/common/const.js +72 -30
- package/lib/esm/decorators/common/customKey.js +132 -12
- package/lib/esm/decorators/common/default.js +85 -19
- package/lib/esm/decorators/common/discriminatorKey.js +123 -0
- package/lib/esm/decorators/common/errorMsg.js +48 -2
- package/lib/esm/decorators/common/example.js +68 -6
- package/lib/esm/decorators/common/examples.js +99 -6
- package/lib/esm/decorators/common/format.js +86 -52
- package/lib/esm/decorators/common/nullable.js +77 -11
- package/lib/esm/decorators/common/oneOf.js +93 -9
- package/lib/esm/decorators/common/optional.js +93 -4
- package/lib/esm/decorators/common/property.js +42 -6
- package/lib/esm/decorators/common/readOnly.js +106 -5
- package/lib/esm/decorators/common/required.js +57 -20
- package/lib/esm/decorators/common/writeOnly.js +117 -5
- package/lib/esm/domain/JsonClassStore.js +75 -0
- package/lib/esm/domain/JsonDiscriminator.js +57 -0
- package/lib/esm/domain/JsonEntityStore.js +46 -0
- package/lib/esm/domain/JsonFormatTypes.js +39 -0
- package/lib/esm/domain/JsonLazyRef.js +33 -0
- package/lib/esm/domain/JsonMap.js +32 -0
- package/lib/esm/domain/JsonMedia.js +28 -0
- package/lib/esm/domain/JsonMethodStore.js +56 -0
- package/lib/esm/domain/JsonOperation.js +43 -0
- package/lib/esm/domain/JsonOperationPathsMap.js +38 -0
- package/lib/esm/domain/JsonOperationRoute.js +54 -0
- package/lib/esm/domain/JsonParameter.js +35 -0
- package/lib/esm/domain/JsonParameterStore.js +71 -0
- package/lib/esm/domain/JsonParameterTypes.js +26 -0
- package/lib/esm/domain/JsonPropertyStore.js +44 -0
- package/lib/esm/domain/JsonRequestBody.js +26 -0
- package/lib/esm/domain/JsonResponse.js +29 -0
- package/lib/esm/domain/JsonSchema.js +365 -16
- package/lib/esm/domain/SpecTypes.js +26 -0
- package/lib/esm/fn/boolean.js +39 -3
- package/lib/esm/fn/number.js +45 -3
- package/lib/esm/fn/string.js +43 -3
- package/lib/types/constants/OperationVerbs.d.ts +35 -0
- package/lib/types/constants/VendorKeys.d.ts +29 -0
- package/lib/types/constants/httpStatusMessages.d.ts +59 -0
- package/lib/types/constants/jsonSchemaProperties.d.ts +55 -0
- package/lib/types/decorators/class/children.d.ts +93 -5
- package/lib/types/decorators/class/discriminatorValue.d.ts +124 -20
- package/lib/types/decorators/collections/collectionContains.d.ts +91 -10
- package/lib/types/decorators/collections/maxItems.d.ts +97 -27
- package/lib/types/decorators/collections/maxProperties.d.ts +96 -30
- package/lib/types/decorators/collections/minItems.d.ts +122 -27
- package/lib/types/decorators/collections/minProperties.d.ts +132 -31
- package/lib/types/decorators/common/additionalProperties.d.ts +59 -4
- package/lib/types/decorators/common/allOf.d.ts +61 -6
- package/lib/types/decorators/common/allow.d.ts +56 -12
- package/lib/types/decorators/common/any.d.ts +58 -18
- package/lib/types/decorators/common/anyOf.d.ts +65 -9
- package/lib/types/decorators/common/const.d.ts +72 -30
- package/lib/types/decorators/common/customKey.d.ts +132 -12
- package/lib/types/decorators/common/default.d.ts +85 -19
- package/lib/types/decorators/common/discriminatorKey.d.ts +123 -0
- package/lib/types/decorators/common/errorMsg.d.ts +48 -2
- package/lib/types/decorators/common/example.d.ts +68 -6
- package/lib/types/decorators/common/examples.d.ts +99 -6
- package/lib/types/decorators/common/format.d.ts +86 -52
- package/lib/types/decorators/common/nullable.d.ts +77 -11
- package/lib/types/decorators/common/oneOf.d.ts +93 -9
- package/lib/types/decorators/common/optional.d.ts +93 -4
- package/lib/types/decorators/common/property.d.ts +42 -6
- package/lib/types/decorators/common/readOnly.d.ts +106 -5
- package/lib/types/decorators/common/required.d.ts +57 -20
- package/lib/types/decorators/common/writeOnly.d.ts +117 -5
- package/lib/types/domain/JsonClassStore.d.ts +75 -0
- package/lib/types/domain/JsonDiscriminator.d.ts +57 -0
- package/lib/types/domain/JsonEntityStore.d.ts +51 -0
- package/lib/types/domain/JsonFormatTypes.d.ts +39 -0
- package/lib/types/domain/JsonLazyRef.d.ts +33 -0
- package/lib/types/domain/JsonMap.d.ts +32 -0
- package/lib/types/domain/JsonMedia.d.ts +28 -0
- package/lib/types/domain/JsonMethodStore.d.ts +66 -0
- package/lib/types/domain/JsonOperation.d.ts +51 -0
- package/lib/types/domain/JsonOperationPathsMap.d.ts +38 -0
- package/lib/types/domain/JsonOperationRoute.d.ts +54 -0
- package/lib/types/domain/JsonParameter.d.ts +35 -0
- package/lib/types/domain/JsonParameterStore.d.ts +97 -0
- package/lib/types/domain/JsonParameterTypes.d.ts +26 -0
- package/lib/types/domain/JsonPropertyStore.d.ts +44 -0
- package/lib/types/domain/JsonRequestBody.d.ts +31 -0
- package/lib/types/domain/JsonResponse.d.ts +34 -0
- package/lib/types/domain/JsonSchema.d.ts +422 -19
- package/lib/types/domain/SpecTypes.d.ts +26 -0
- package/lib/types/domain/types.d.ts +90 -0
- package/lib/types/fn/boolean.d.ts +39 -3
- package/lib/types/fn/number.d.ts +45 -3
- package/lib/types/fn/string.d.ts +43 -3
- package/lib/types/interfaces/IgnoreCallback.d.ts +24 -0
- package/lib/types/interfaces/JsonHookContext.d.ts +31 -0
- package/lib/types/interfaces/JsonOpenSpec.d.ts +33 -0
- package/lib/types/interfaces/JsonSchemaOptions.d.ts +26 -0
- package/package.json +7 -7
|
@@ -13,6 +13,57 @@ import { JsonEntityStore } from "./JsonEntityStore.js";
|
|
|
13
13
|
function isEnum(type) {
|
|
14
14
|
return isObject(type) && !("toJSON" in type);
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Core class representing a JSON Schema with TypeScript type integration.
|
|
18
|
+
*
|
|
19
|
+
* JsonSchema is the central class in Ts.ED's schema system, providing a Map-based
|
|
20
|
+
* interface for building and manipulating JSON Schema definitions. It extends the
|
|
21
|
+
* standard JSON Schema 7 specification with Ts.ED-specific features including:
|
|
22
|
+
*
|
|
23
|
+
* - TypeScript class and type integration
|
|
24
|
+
* - Property aliasing and naming strategies
|
|
25
|
+
* - Group-based conditional schemas
|
|
26
|
+
* - Discriminator support for polymorphic types
|
|
27
|
+
* - Generic type parameter handling
|
|
28
|
+
* - Schema composition (oneOf, allOf, anyOf)
|
|
29
|
+
* - Hook-based transformation pipeline
|
|
30
|
+
* - Vendor extension support
|
|
31
|
+
*
|
|
32
|
+
* ### Usage
|
|
33
|
+
*
|
|
34
|
+
* ```typescript
|
|
35
|
+
* // Create a basic schema
|
|
36
|
+
* const schema = new JsonSchema({
|
|
37
|
+
* type: "string",
|
|
38
|
+
* minLength: 1,
|
|
39
|
+
* maxLength: 100
|
|
40
|
+
* });
|
|
41
|
+
*
|
|
42
|
+
* // Create schema from TypeScript class
|
|
43
|
+
* class User {
|
|
44
|
+
* name: string;
|
|
45
|
+
* email: string;
|
|
46
|
+
* }
|
|
47
|
+
*
|
|
48
|
+
* const userSchema = JsonSchema.from(User);
|
|
49
|
+
*
|
|
50
|
+
* // Generate JSON Schema output
|
|
51
|
+
* const jsonSchema = userSchema.toJSON();
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* ### Key Features
|
|
55
|
+
*
|
|
56
|
+
* - **Type Safety**: Generic type parameter `T` represents the TypeScript type
|
|
57
|
+
* - **Composition**: Support for allOf, oneOf, anyOf schema composition
|
|
58
|
+
* - **Validation**: Full JSON Schema validation keyword support
|
|
59
|
+
* - **Transformation**: Hook system for custom schema transformations
|
|
60
|
+
* - **Groups**: Conditional property inclusion based on groups
|
|
61
|
+
* - **Generics**: Support for generic type parameters in classes
|
|
62
|
+
*
|
|
63
|
+
* @typeParam T - The TypeScript type this schema represents
|
|
64
|
+
*
|
|
65
|
+
* @public
|
|
66
|
+
*/
|
|
16
67
|
export class JsonSchema extends Map {
|
|
17
68
|
/**
|
|
18
69
|
* All required fields are stored here
|
|
@@ -66,40 +117,77 @@ export class JsonSchema extends Map {
|
|
|
66
117
|
this.assign(obj);
|
|
67
118
|
}
|
|
68
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* Get the alias map for property name aliases.
|
|
122
|
+
*
|
|
123
|
+
* The alias map allows bidirectional property name mapping, useful for
|
|
124
|
+
* serialization/deserialization with different naming conventions.
|
|
125
|
+
*
|
|
126
|
+
* @returns The alias map containing property-to-alias and alias-to-property mappings
|
|
127
|
+
*/
|
|
69
128
|
get alias() {
|
|
70
129
|
return this.#alias;
|
|
71
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Check if this schema represents a TypeScript class (not a primitive or collection).
|
|
133
|
+
*
|
|
134
|
+
* @returns `true` if the schema represents a class, `false` otherwise
|
|
135
|
+
*/
|
|
72
136
|
get isClass() {
|
|
73
137
|
return isClass(this.class) && !this.isCollection;
|
|
74
138
|
}
|
|
75
139
|
/**
|
|
76
|
-
*
|
|
140
|
+
* Check if this schema represents a collection (array, Set, Map, etc.).
|
|
141
|
+
*
|
|
142
|
+
* Collections have an associated item schema that defines the type of elements
|
|
143
|
+
* they contain.
|
|
144
|
+
*
|
|
145
|
+
* @returns `true` if the schema is a collection, `false` otherwise
|
|
77
146
|
*/
|
|
78
147
|
get isCollection() {
|
|
79
148
|
return this.#isCollection;
|
|
80
149
|
}
|
|
81
150
|
/**
|
|
82
|
-
*
|
|
83
|
-
*
|
|
151
|
+
* Check if this is a local schema reference.
|
|
152
|
+
*
|
|
153
|
+
* A local schema acts as a reference to link a property or parameter to a class schema.
|
|
154
|
+
* This is used for type resolution and avoids schema duplication.
|
|
155
|
+
*
|
|
156
|
+
* @returns `true` if this is a local schema reference, `false` otherwise
|
|
84
157
|
*/
|
|
85
158
|
get isLocalSchema() {
|
|
86
159
|
return this.#isLocalSchema;
|
|
87
160
|
}
|
|
88
161
|
/**
|
|
89
|
-
*
|
|
162
|
+
* Check if this schema represents a generic type.
|
|
163
|
+
*
|
|
164
|
+
* Generic schemas have a generic label vendor key and are used for parameterized types.
|
|
165
|
+
*
|
|
166
|
+
* @returns `true` if the schema is a generic type, `false` otherwise
|
|
90
167
|
*/
|
|
91
168
|
get isGeneric() {
|
|
92
169
|
return this.has(VendorKeys.GENERIC_LABEL);
|
|
93
170
|
}
|
|
171
|
+
/**
|
|
172
|
+
* Get the ancestor schema that defines a discriminator.
|
|
173
|
+
*
|
|
174
|
+
* Searches through the class hierarchy to find a parent class with a discriminator
|
|
175
|
+
* configuration. This is used for polymorphic type resolution.
|
|
176
|
+
*
|
|
177
|
+
* @returns The ancestor schema with discriminator, or undefined if none found
|
|
178
|
+
*/
|
|
94
179
|
get discriminatorAncestor() {
|
|
95
180
|
const ancestors = ancestorsOf(this.#target);
|
|
96
181
|
const ancestor = ancestors.find((ancestor) => JsonEntityStore.from(ancestor).schema.isDiscriminator);
|
|
97
182
|
return ancestor && JsonEntityStore.from(ancestor).schema;
|
|
98
183
|
}
|
|
99
184
|
/**
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
185
|
+
* Get the computed TypeScript class or type for this schema.
|
|
186
|
+
*
|
|
187
|
+
* For local schemas, returns the item schema's target.
|
|
188
|
+
* For recursive models using functions, resolves the function to get the actual type.
|
|
189
|
+
*
|
|
190
|
+
* @returns The TypeScript class or type this schema represents
|
|
103
191
|
*/
|
|
104
192
|
get class() {
|
|
105
193
|
if (this.#isLocalSchema) {
|
|
@@ -108,26 +196,94 @@ export class JsonSchema extends Map {
|
|
|
108
196
|
return getComputedType(this.#target);
|
|
109
197
|
}
|
|
110
198
|
/**
|
|
111
|
-
*
|
|
199
|
+
* Get the TypeScript class for collection items.
|
|
200
|
+
*
|
|
201
|
+
* For collection schemas, returns the type of elements in the collection.
|
|
202
|
+
* For non-collection schemas, returns the schema's own class.
|
|
203
|
+
*
|
|
204
|
+
* @returns The item class for collections, or the schema's class otherwise
|
|
112
205
|
*/
|
|
113
206
|
get collectionClass() {
|
|
114
207
|
return this.isCollection && this.#itemSchema ? this.#itemSchema.class : this.class;
|
|
115
208
|
}
|
|
209
|
+
/**
|
|
210
|
+
* Check if this schema can use a $ref reference.
|
|
211
|
+
*
|
|
212
|
+
* Schemas with labels can be referenced by $ref to avoid duplication
|
|
213
|
+
* in generated OpenAPI specifications.
|
|
214
|
+
*
|
|
215
|
+
* @returns `true` if the schema can be referenced via $ref, `false` otherwise
|
|
216
|
+
*/
|
|
116
217
|
get canRef() {
|
|
117
218
|
return this.#useRefLabel;
|
|
118
219
|
}
|
|
220
|
+
/**
|
|
221
|
+
* Check if this schema allows null values.
|
|
222
|
+
*
|
|
223
|
+
* @returns `true` if the schema is nullable, `false` otherwise
|
|
224
|
+
*/
|
|
119
225
|
get isNullable() {
|
|
120
226
|
return !!this.get(VendorKeys.NULLABLE);
|
|
121
227
|
}
|
|
228
|
+
/**
|
|
229
|
+
* Check if this property is read-only.
|
|
230
|
+
*
|
|
231
|
+
* Read-only properties are included in responses but not accepted in requests.
|
|
232
|
+
*
|
|
233
|
+
* @returns `true` if the property is read-only, `false` otherwise
|
|
234
|
+
*/
|
|
122
235
|
get isReadOnly() {
|
|
123
236
|
return this.get("readOnly");
|
|
124
237
|
}
|
|
238
|
+
/**
|
|
239
|
+
* Check if this property is write-only.
|
|
240
|
+
*
|
|
241
|
+
* Write-only properties are accepted in requests but not included in responses.
|
|
242
|
+
* Useful for sensitive data like passwords.
|
|
243
|
+
*
|
|
244
|
+
* @returns `true` if the property is write-only, `false` otherwise
|
|
245
|
+
*/
|
|
125
246
|
get isWriteOnly() {
|
|
126
247
|
return this.get("writeOnly");
|
|
127
248
|
}
|
|
249
|
+
/**
|
|
250
|
+
* Check if this schema has a discriminator configuration.
|
|
251
|
+
*
|
|
252
|
+
* Discriminators enable polymorphic type resolution based on a property value.
|
|
253
|
+
*
|
|
254
|
+
* @returns `true` if a discriminator is configured, `false` otherwise
|
|
255
|
+
*/
|
|
128
256
|
get hasDiscriminator() {
|
|
129
257
|
return !!this.#discriminator;
|
|
130
258
|
}
|
|
259
|
+
/**
|
|
260
|
+
* Create a JsonSchema from various input types.
|
|
261
|
+
*
|
|
262
|
+
* This factory method intelligently converts different input types into JsonSchema instances:
|
|
263
|
+
* - Existing JsonSchema: returned as-is
|
|
264
|
+
* - TypeScript classes: retrieves the schema from JsonEntityStore
|
|
265
|
+
* - Primitive classes (String, Number, etc.) and Date: creates type schema
|
|
266
|
+
* - Plain objects: creates schema from JSON Schema definition
|
|
267
|
+
*
|
|
268
|
+
* ### Usage
|
|
269
|
+
*
|
|
270
|
+
* ```typescript
|
|
271
|
+
* // From a class
|
|
272
|
+
* const schema1 = JsonSchema.from(User);
|
|
273
|
+
*
|
|
274
|
+
* // From a primitive
|
|
275
|
+
* const schema2 = JsonSchema.from(String);
|
|
276
|
+
*
|
|
277
|
+
* // From a schema object
|
|
278
|
+
* const schema3 = JsonSchema.from({type: "string", minLength: 1});
|
|
279
|
+
*
|
|
280
|
+
* // From existing schema (no-op)
|
|
281
|
+
* const schema4 = JsonSchema.from(schema1);
|
|
282
|
+
* ```
|
|
283
|
+
*
|
|
284
|
+
* @param item - The input to convert to a JsonSchema
|
|
285
|
+
* @returns A JsonSchema instance representing the input
|
|
286
|
+
*/
|
|
131
287
|
static from(item) {
|
|
132
288
|
if (item instanceof JsonSchema) {
|
|
133
289
|
return item;
|
|
@@ -141,8 +297,15 @@ export class JsonSchema extends Map {
|
|
|
141
297
|
return new JsonSchema(item);
|
|
142
298
|
}
|
|
143
299
|
/**
|
|
144
|
-
* Check if the schema
|
|
145
|
-
*
|
|
300
|
+
* Check if the schema has a property.
|
|
301
|
+
*
|
|
302
|
+
* Checks multiple locations in order:
|
|
303
|
+
* 1. Standard Map keys
|
|
304
|
+
* 2. Internal keys (prefixed with #)
|
|
305
|
+
* 3. Vendor extension keys
|
|
306
|
+
*
|
|
307
|
+
* @param key - The property key to check
|
|
308
|
+
* @returns `true` if the property exists, `false` otherwise
|
|
146
309
|
*/
|
|
147
310
|
has(key) {
|
|
148
311
|
if (super.has(key)) {
|
|
@@ -165,10 +328,35 @@ export class JsonSchema extends Map {
|
|
|
165
328
|
}
|
|
166
329
|
return defaultValue;
|
|
167
330
|
}
|
|
331
|
+
/**
|
|
332
|
+
* Enable group forwarding to nested properties.
|
|
333
|
+
*
|
|
334
|
+
* When enabled, groups specified at the parent level are forwarded to nested
|
|
335
|
+
* object properties, allowing hierarchical group filtering.
|
|
336
|
+
*
|
|
337
|
+
* @param bool - Whether to enable group forwarding (default: true)
|
|
338
|
+
* @returns This schema instance for method chaining
|
|
339
|
+
*/
|
|
168
340
|
forwardGroups(bool = true) {
|
|
169
341
|
this.vendorKey(VendorKeys.FORWARD_GROUPS, bool);
|
|
170
342
|
return this;
|
|
171
343
|
}
|
|
344
|
+
/**
|
|
345
|
+
* Assign groups to this property or schema.
|
|
346
|
+
*
|
|
347
|
+
* Groups enable conditional inclusion of properties based on serialization context.
|
|
348
|
+
* For example, you might have "admin" and "public" groups to control visibility.
|
|
349
|
+
*
|
|
350
|
+
* ### Usage
|
|
351
|
+
*
|
|
352
|
+
* ```typescript
|
|
353
|
+
* schema.groups(["admin", "internal"]);
|
|
354
|
+
* ```
|
|
355
|
+
*
|
|
356
|
+
* @param groups - Array of group names this property belongs to
|
|
357
|
+
* @param isProperty - Whether this is a property schema (enables group matching hooks)
|
|
358
|
+
* @returns This schema instance for method chaining
|
|
359
|
+
*/
|
|
172
360
|
groups(groups, isProperty) {
|
|
173
361
|
if (groups) {
|
|
174
362
|
this.vendorKey(VendorKeys.GROUPS, groups);
|
|
@@ -186,29 +374,75 @@ export class JsonSchema extends Map {
|
|
|
186
374
|
}
|
|
187
375
|
return this;
|
|
188
376
|
}
|
|
377
|
+
/**
|
|
378
|
+
* Set a custom name suffix for group-specific schema variations.
|
|
379
|
+
*
|
|
380
|
+
* This is used to generate unique schema names when the same model is used
|
|
381
|
+
* with different groups, avoiding conflicts in OpenAPI components.
|
|
382
|
+
*
|
|
383
|
+
* @param groupsName - The groups name suffix
|
|
384
|
+
* @returns This schema instance for method chaining
|
|
385
|
+
*/
|
|
189
386
|
groupsName(groupsName) {
|
|
190
387
|
groupsName && this.vendorKey(VendorKeys.GROUPS_NAME, groupsName);
|
|
191
388
|
return this;
|
|
192
389
|
}
|
|
390
|
+
/**
|
|
391
|
+
* Specify which groups are allowed access to this schema.
|
|
392
|
+
*
|
|
393
|
+
* @param groups - Array of group names allowed to access this schema
|
|
394
|
+
* @returns This schema instance for method chaining
|
|
395
|
+
*/
|
|
193
396
|
allowedGroups(groups) {
|
|
194
397
|
groups && this.vendorKey(VendorKeys.ALLOWED_GROUPS, groups);
|
|
195
398
|
return this;
|
|
196
399
|
}
|
|
197
400
|
/**
|
|
198
|
-
* Assign
|
|
199
|
-
*
|
|
401
|
+
* Assign generic type parameter labels to a class or schema.
|
|
402
|
+
*
|
|
403
|
+
* Used for classes with generic type parameters (e.g., `Paginated<T>`)
|
|
404
|
+
* to track the parameter names for proper schema generation.
|
|
405
|
+
*
|
|
406
|
+
* ### Usage
|
|
407
|
+
*
|
|
408
|
+
* ```typescript
|
|
409
|
+
* schema.genericLabels(["T", "U"]);
|
|
410
|
+
* ```
|
|
411
|
+
*
|
|
412
|
+
* @param genericLabels - Array of generic type parameter names
|
|
413
|
+
* @returns This schema instance for method chaining
|
|
200
414
|
*/
|
|
201
415
|
genericLabels(genericLabels) {
|
|
202
416
|
this.vendorKey(VendorKeys.GENERIC_LABELS, genericLabels);
|
|
203
417
|
return this;
|
|
204
418
|
}
|
|
419
|
+
/**
|
|
420
|
+
* Assign a single generic type label to this schema.
|
|
421
|
+
*
|
|
422
|
+
* Marks this schema as representing a generic type parameter.
|
|
423
|
+
*
|
|
424
|
+
* @param genericLabel - The generic type parameter name
|
|
425
|
+
* @returns This schema instance for method chaining
|
|
426
|
+
*/
|
|
205
427
|
genericLabel(genericLabel) {
|
|
206
428
|
this.vendorKey(VendorKeys.GENERIC_LABEL, genericLabel);
|
|
207
429
|
return this;
|
|
208
430
|
}
|
|
209
431
|
/**
|
|
210
|
-
*
|
|
211
|
-
*
|
|
432
|
+
* Specify the concrete types for generic parameters.
|
|
433
|
+
*
|
|
434
|
+
* When using a generic class, this method assigns the actual types that should
|
|
435
|
+
* replace the generic parameters for this specific instance.
|
|
436
|
+
*
|
|
437
|
+
* ### Usage
|
|
438
|
+
*
|
|
439
|
+
* ```typescript
|
|
440
|
+
* // For Paginated<User>
|
|
441
|
+
* schema.genericOf([User]);
|
|
442
|
+
* ```
|
|
443
|
+
*
|
|
444
|
+
* @param generics - Arrays of generic type values to apply
|
|
445
|
+
* @returns This schema instance for method chaining
|
|
212
446
|
*/
|
|
213
447
|
genericOf(...generics) {
|
|
214
448
|
const mapped = this.mapGenerics(this.#itemSchema || this.mapToJsonSchema(this.getTarget()), generics);
|
|
@@ -221,6 +455,27 @@ export class JsonSchema extends Map {
|
|
|
221
455
|
}
|
|
222
456
|
return this;
|
|
223
457
|
}
|
|
458
|
+
/**
|
|
459
|
+
* Get or create the item schema for collections.
|
|
460
|
+
*
|
|
461
|
+
* For array/collection types, this defines the schema for individual items.
|
|
462
|
+
* If the schema already has an item schema, it returns the existing one;
|
|
463
|
+
* otherwise, it creates a new one from the provided definition.
|
|
464
|
+
*
|
|
465
|
+
* ### Usage
|
|
466
|
+
*
|
|
467
|
+
* ```typescript
|
|
468
|
+
* // Define array items
|
|
469
|
+
* const arraySchema = new JsonSchema({type: "array"});
|
|
470
|
+
* arraySchema.itemSchema({type: "string", minLength: 1});
|
|
471
|
+
*
|
|
472
|
+
* // For class-based items
|
|
473
|
+
* arraySchema.itemSchema(User);
|
|
474
|
+
* ```
|
|
475
|
+
*
|
|
476
|
+
* @param obj - Schema definition for items
|
|
477
|
+
* @returns The item schema instance
|
|
478
|
+
*/
|
|
224
479
|
itemSchema(obj = {}) {
|
|
225
480
|
this.#itemSchema = this.#itemSchema || this.mapToJsonSchema(obj);
|
|
226
481
|
if (isPlainObject(obj)) {
|
|
@@ -231,45 +486,139 @@ export class JsonSchema extends Map {
|
|
|
231
486
|
}
|
|
232
487
|
return this.#itemSchema;
|
|
233
488
|
}
|
|
489
|
+
/**
|
|
490
|
+
* Get the alias for a property name.
|
|
491
|
+
*
|
|
492
|
+
* @param property - The property name to look up
|
|
493
|
+
* @returns The alias for the property, or undefined if none exists
|
|
494
|
+
*/
|
|
234
495
|
getAliasOf(property) {
|
|
235
496
|
return this.#alias.get(property);
|
|
236
497
|
}
|
|
498
|
+
/**
|
|
499
|
+
* Add a bidirectional alias mapping between property names.
|
|
500
|
+
*
|
|
501
|
+
* Aliases allow properties to be accessed by alternative names, useful for
|
|
502
|
+
* supporting different naming conventions (camelCase vs snake_case, etc.).
|
|
503
|
+
*
|
|
504
|
+
* ### Usage
|
|
505
|
+
*
|
|
506
|
+
* ```typescript
|
|
507
|
+
* schema.addAlias("userId", "user_id");
|
|
508
|
+
* // Both "userId" and "user_id" now map to each other
|
|
509
|
+
* ```
|
|
510
|
+
*
|
|
511
|
+
* @param property - The original property name
|
|
512
|
+
* @param alias - The alias name
|
|
513
|
+
* @returns This schema instance for method chaining
|
|
514
|
+
*/
|
|
237
515
|
addAlias(property, alias) {
|
|
238
516
|
this.#alias.set(property, alias);
|
|
239
517
|
this.#alias.set(alias, property);
|
|
240
518
|
return this;
|
|
241
519
|
}
|
|
520
|
+
/**
|
|
521
|
+
* Remove an alias mapping for a property.
|
|
522
|
+
*
|
|
523
|
+
* @param property - The property name to remove aliases for
|
|
524
|
+
* @returns This schema instance for method chaining
|
|
525
|
+
*/
|
|
242
526
|
removeAlias(property) {
|
|
243
527
|
const alias = this.#alias.get(property);
|
|
244
528
|
alias && this.#alias.delete(alias);
|
|
245
529
|
this.#alias.delete(property);
|
|
246
530
|
return this;
|
|
247
531
|
}
|
|
532
|
+
/**
|
|
533
|
+
* Set the `$id` keyword for this schema.
|
|
534
|
+
*
|
|
535
|
+
* The `$id` keyword is used to identify the schema and establish a base URI
|
|
536
|
+
* for resolving relative references.
|
|
537
|
+
*
|
|
538
|
+
* @param $id - The schema identifier URI
|
|
539
|
+
* @returns This schema instance for method chaining
|
|
540
|
+
*/
|
|
248
541
|
$id($id) {
|
|
249
542
|
super.set("$id", $id);
|
|
250
543
|
return this;
|
|
251
544
|
}
|
|
545
|
+
/**
|
|
546
|
+
* Set a `$ref` reference to another schema.
|
|
547
|
+
*
|
|
548
|
+
* The `$ref` keyword creates a reference to a schema defined elsewhere,
|
|
549
|
+
* enabling schema reuse and avoiding duplication.
|
|
550
|
+
*
|
|
551
|
+
* @param $ref - The reference URI (e.g., "#/components/schemas/User")
|
|
552
|
+
* @returns This schema instance for method chaining
|
|
553
|
+
*/
|
|
252
554
|
$ref($ref) {
|
|
253
555
|
super.set("$ref", $ref);
|
|
254
556
|
return this;
|
|
255
557
|
}
|
|
558
|
+
/**
|
|
559
|
+
* Set the JSON Schema version.
|
|
560
|
+
*
|
|
561
|
+
* @param $schema - The JSON Schema version URI
|
|
562
|
+
* @returns This schema instance for method chaining
|
|
563
|
+
*/
|
|
256
564
|
$schema($schema) {
|
|
257
565
|
super.set("$schema", $schema);
|
|
258
566
|
return this;
|
|
259
567
|
}
|
|
260
568
|
/**
|
|
261
|
-
*
|
|
262
|
-
*
|
|
569
|
+
* Assign a label to enable schema referencing.
|
|
570
|
+
*
|
|
571
|
+
* Labeled schemas can be referenced using `$ref` in OpenAPI specifications,
|
|
572
|
+
* reducing duplication by sharing schema definitions across endpoints.
|
|
573
|
+
*
|
|
574
|
+
* ### Usage
|
|
575
|
+
*
|
|
576
|
+
* ```typescript
|
|
577
|
+
* schema.label("User");
|
|
578
|
+
* // Can now be referenced as #/components/schemas/User
|
|
579
|
+
* ```
|
|
580
|
+
*
|
|
581
|
+
* @param name - The label name for the schema
|
|
582
|
+
* @returns This schema instance for method chaining
|
|
263
583
|
*/
|
|
264
584
|
label(name) {
|
|
265
585
|
this.#useRefLabel = true;
|
|
266
586
|
super.set("name", name);
|
|
267
587
|
return this;
|
|
268
588
|
}
|
|
589
|
+
/**
|
|
590
|
+
* Set the name of this schema.
|
|
591
|
+
*
|
|
592
|
+
* Unlike `label()`, this sets the name without enabling referencing.
|
|
593
|
+
*
|
|
594
|
+
* @param name - The schema name
|
|
595
|
+
* @returns This schema instance for method chaining
|
|
596
|
+
*/
|
|
269
597
|
name(name) {
|
|
270
598
|
super.set("name", name);
|
|
271
599
|
return this;
|
|
272
600
|
}
|
|
601
|
+
/**
|
|
602
|
+
* Mark this property as ignored during serialization.
|
|
603
|
+
*
|
|
604
|
+
* Ignored properties are excluded from generated schemas and serialization.
|
|
605
|
+
* Can be a boolean or a callback function for conditional ignoring.
|
|
606
|
+
*
|
|
607
|
+
* ### Usage
|
|
608
|
+
*
|
|
609
|
+
* ```typescript
|
|
610
|
+
* // Always ignore
|
|
611
|
+
* schema.ignore(true);
|
|
612
|
+
*
|
|
613
|
+
* // Conditionally ignore based on context
|
|
614
|
+
* schema.ignore((value, ctx) => {
|
|
615
|
+
* return ctx.groups?.includes("public");
|
|
616
|
+
* });
|
|
617
|
+
* ```
|
|
618
|
+
*
|
|
619
|
+
* @param cb - Boolean flag or callback function to determine if property should be ignored
|
|
620
|
+
* @returns This schema instance for method chaining
|
|
621
|
+
*/
|
|
273
622
|
ignore(cb) {
|
|
274
623
|
if (typeof cb !== "boolean") {
|
|
275
624
|
this.$hooks.on("ignore", cb);
|
|
@@ -1,3 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Specification types supported by Ts.ED's schema system.
|
|
3
|
+
*
|
|
4
|
+
* This enum defines the different API specification formats that can be generated
|
|
5
|
+
* from Ts.ED schemas. Each type affects how schemas are serialized and which
|
|
6
|
+
* features are available.
|
|
7
|
+
*
|
|
8
|
+
* ### Specification Types
|
|
9
|
+
*
|
|
10
|
+
* - **JSON**: Pure JSON Schema (no OpenAPI-specific features)
|
|
11
|
+
* - **SWAGGER**: Swagger 2.0 / OpenAPI 2.0 specification
|
|
12
|
+
* - **OPENAPI**: OpenAPI 3.x specification (recommended)
|
|
13
|
+
* - **ASYNCAPI**: AsyncAPI 2.x specification (for async/event-driven APIs)
|
|
14
|
+
*
|
|
15
|
+
* ### Usage
|
|
16
|
+
*
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import {SpecTypes} from "@tsed/schema";
|
|
19
|
+
*
|
|
20
|
+
* const schema = getJsonSchema(MyModel, {
|
|
21
|
+
* specType: SpecTypes.OPENAPI
|
|
22
|
+
* });
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
1
27
|
export var SpecTypes;
|
|
2
28
|
(function (SpecTypes) {
|
|
3
29
|
SpecTypes["JSON"] = "jsonschema";
|
package/lib/esm/fn/boolean.js
CHANGED
|
@@ -1,10 +1,46 @@
|
|
|
1
1
|
import { from } from "./from.js";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Create a boolean schema programmatically.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
5
|
+
* This function provides a fluent API for building boolean schemas without decorators.
|
|
6
|
+
* It returns a JsonSchema instance configured for boolean type, which can be further
|
|
7
|
+
* customized with descriptions, defaults, and other JSON Schema properties.
|
|
6
8
|
*
|
|
7
|
-
*
|
|
9
|
+
* ### Usage
|
|
10
|
+
*
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import {boolean} from "@tsed/schema";
|
|
13
|
+
*
|
|
14
|
+
* // Basic boolean schema
|
|
15
|
+
* const activeSchema = boolean();
|
|
16
|
+
*
|
|
17
|
+
* // Boolean with description and default
|
|
18
|
+
* const isVerifiedSchema = boolean()
|
|
19
|
+
* .description("Whether the user is verified")
|
|
20
|
+
* .default(false);
|
|
21
|
+
*
|
|
22
|
+
* // Required boolean
|
|
23
|
+
* const termsAcceptedSchema = boolean()
|
|
24
|
+
* .description("User accepted terms and conditions")
|
|
25
|
+
* .required();
|
|
26
|
+
*
|
|
27
|
+
* // Nullable boolean
|
|
28
|
+
* const optionalFlagSchema = boolean()
|
|
29
|
+
* .nullable()
|
|
30
|
+
* .description("Optional feature flag");
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* ### Available Methods
|
|
34
|
+
*
|
|
35
|
+
* The returned JsonSchema provides many chainable methods:
|
|
36
|
+
* - `description(text)` - Add description
|
|
37
|
+
* - `default(value)` - Set default value (true or false)
|
|
38
|
+
* - `nullable()` - Allow null values
|
|
39
|
+
* - `const(value)` - Restrict to a single value
|
|
40
|
+
*
|
|
41
|
+
* @returns A new JsonSchema configured as a boolean type
|
|
42
|
+
*
|
|
43
|
+
* @public
|
|
8
44
|
*/
|
|
9
45
|
export function boolean() {
|
|
10
46
|
return from(Boolean);
|
package/lib/esm/fn/number.js
CHANGED
|
@@ -1,10 +1,52 @@
|
|
|
1
1
|
import { from } from "./from.js";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Create a number schema programmatically.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
5
|
+
* This function provides a fluent API for building number schemas without decorators.
|
|
6
|
+
* It returns a JsonSchema instance configured for number type, which can be further
|
|
7
|
+
* customized with validation constraints and other JSON Schema properties.
|
|
6
8
|
*
|
|
7
|
-
*
|
|
9
|
+
* ### Usage
|
|
10
|
+
*
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import {number} from "@tsed/schema";
|
|
13
|
+
*
|
|
14
|
+
* // Basic number schema
|
|
15
|
+
* const ageSchema = number();
|
|
16
|
+
*
|
|
17
|
+
* // Number with range constraints
|
|
18
|
+
* const priceSchema = number()
|
|
19
|
+
* .minimum(0)
|
|
20
|
+
* .maximum(999999.99)
|
|
21
|
+
* .description("Product price in USD");
|
|
22
|
+
*
|
|
23
|
+
* // Integer with exclusive bounds
|
|
24
|
+
* const scoreSchema = number()
|
|
25
|
+
* .integer()
|
|
26
|
+
* .exclusiveMinimum(0)
|
|
27
|
+
* .exclusiveMaximum(100);
|
|
28
|
+
*
|
|
29
|
+
* // Multiple of constraint
|
|
30
|
+
* const quantitySchema = number()
|
|
31
|
+
* .multipleOf(5)
|
|
32
|
+
* .minimum(0)
|
|
33
|
+
* .description("Quantity in packs of 5");
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* ### Available Methods
|
|
37
|
+
*
|
|
38
|
+
* The returned JsonSchema provides many chainable methods:
|
|
39
|
+
* - `minimum(n)` / `maximum(n)` - Range constraints (inclusive)
|
|
40
|
+
* - `exclusiveMinimum(n)` / `exclusiveMaximum(n)` - Range constraints (exclusive)
|
|
41
|
+
* - `multipleOf(n)` - Value must be multiple of n
|
|
42
|
+
* - `integer()` - Restrict to integers only
|
|
43
|
+
* - `description(text)` - Add description
|
|
44
|
+
* - `default(value)` - Set default value
|
|
45
|
+
* - `nullable()` - Allow null values
|
|
46
|
+
*
|
|
47
|
+
* @returns A new JsonSchema configured as a number type
|
|
48
|
+
*
|
|
49
|
+
* @public
|
|
8
50
|
*/
|
|
9
51
|
export function number() {
|
|
10
52
|
return from(Number);
|