@tsed/schema 8.19.1 → 8.19.3
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/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
|
@@ -1,26 +1,146 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Adds a custom key-value pair to the JSON Schema that is not part of the official spec.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* The `@CustomKey()` decorator allows adding vendor-specific extensions or custom metadata
|
|
5
|
+
* to schemas. These custom keys are only included in the generated schema when explicitly
|
|
6
|
+
* requested with the `customKeys: true` option in `getJsonSchema()`.
|
|
7
|
+
*
|
|
8
|
+
* ### Basic Usage
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* class UserModel {
|
|
12
|
+
* @CustomKey("x-internal-id", "USER_001")
|
|
13
|
+
* @Property()
|
|
14
|
+
* username: string;
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* ### Vendor Extensions
|
|
19
|
+
*
|
|
20
|
+
* Following OpenAPI conventions, use `x-` prefix for custom extensions:
|
|
21
|
+
*
|
|
22
|
+
* ```typescript
|
|
23
|
+
* class ApiModel {
|
|
24
|
+
* @CustomKey("x-visibility", "internal")
|
|
25
|
+
* @CustomKey("x-deprecated-by", "newField")
|
|
26
|
+
* @Property()
|
|
27
|
+
* oldField: string;
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* ### Documentation Metadata
|
|
32
|
+
*
|
|
33
|
+
* ```typescript
|
|
34
|
+
* class ProductModel {
|
|
35
|
+
* @CustomKey("x-example-source", "marketing-team")
|
|
36
|
+
* @CustomKey("x-last-reviewed", "2024-01-15")
|
|
37
|
+
* @Property()
|
|
38
|
+
* description: string;
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* ### Integration Hints
|
|
43
|
+
*
|
|
44
|
+
* ```typescript
|
|
45
|
+
* class DataModel {
|
|
46
|
+
* @CustomKey("x-database-column", "user_full_name")
|
|
47
|
+
* @CustomKey("x-searchable", true)
|
|
48
|
+
* @Property()
|
|
49
|
+
* fullName: string;
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* ### Retrieving Custom Keys
|
|
54
|
+
*
|
|
55
|
+
* Custom keys must be explicitly requested:
|
|
56
|
+
*
|
|
57
|
+
* ```typescript
|
|
58
|
+
* import { getJsonSchema } from "@tsed/schema";
|
|
59
|
+
*
|
|
60
|
+
* const schema = getJsonSchema(UserModel, { customKeys: true });
|
|
61
|
+
* // Schema will include x-internal-id and other custom keys
|
|
62
|
+
*
|
|
63
|
+
* const standardSchema = getJsonSchema(UserModel);
|
|
64
|
+
* // Custom keys are omitted by default
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* ### Use Cases
|
|
68
|
+
*
|
|
69
|
+
* - **OpenAPI Extensions**: Add vendor-specific OpenAPI extensions
|
|
70
|
+
* - **Tool Integration**: Metadata for code generators or documentation tools
|
|
71
|
+
* - **Internal Tracking**: Development or operational metadata
|
|
72
|
+
* - **Feature Flags**: Schema-level feature flag information
|
|
73
|
+
* - **Database Mapping**: ORM or database column hints
|
|
74
|
+
*
|
|
75
|
+
* @param key - The custom key name (use `x-` prefix for vendor extensions)
|
|
76
|
+
* @param value - The value to associate with the key
|
|
5
77
|
*
|
|
6
|
-
* @returns {Function}
|
|
7
78
|
* @decorator
|
|
8
79
|
* @validation
|
|
9
|
-
* @
|
|
10
|
-
* @parameter
|
|
11
|
-
* @schema
|
|
80
|
+
* @public
|
|
12
81
|
*/
|
|
13
82
|
export declare function CustomKey(key: string, value: any): (...args: any[]) => any;
|
|
14
83
|
/**
|
|
15
|
-
*
|
|
84
|
+
* Adds multiple custom key-value pairs to the JSON Schema in one decorator.
|
|
85
|
+
*
|
|
86
|
+
* The `@CustomKeys()` decorator is a convenience function for adding multiple custom
|
|
87
|
+
* keys at once. Like `@CustomKey()`, these keys are only included when explicitly
|
|
88
|
+
* requested with `customKeys: true`.
|
|
89
|
+
*
|
|
90
|
+
* ### Basic Usage
|
|
91
|
+
*
|
|
92
|
+
* ```typescript
|
|
93
|
+
* class ProductModel {
|
|
94
|
+
* @CustomKeys({
|
|
95
|
+
* "x-internal-code": "PROD-123",
|
|
96
|
+
* "x-category": "electronics",
|
|
97
|
+
* "x-priority": "high"
|
|
98
|
+
* })
|
|
99
|
+
* @Property()
|
|
100
|
+
* name: string;
|
|
101
|
+
* }
|
|
102
|
+
* ```
|
|
103
|
+
*
|
|
104
|
+
* ### Multiple Vendor Extensions
|
|
105
|
+
*
|
|
106
|
+
* ```typescript
|
|
107
|
+
* class ApiEndpoint {
|
|
108
|
+
* @CustomKeys({
|
|
109
|
+
* "x-rate-limit": 100,
|
|
110
|
+
* "x-requires-auth": true,
|
|
111
|
+
* "x-cache-ttl": 3600,
|
|
112
|
+
* "x-deprecated": false
|
|
113
|
+
* })
|
|
114
|
+
* @Property()
|
|
115
|
+
* endpoint: string;
|
|
116
|
+
* }
|
|
117
|
+
* ```
|
|
118
|
+
*
|
|
119
|
+
* ### Equivalent to Multiple @CustomKey
|
|
120
|
+
*
|
|
121
|
+
* These are equivalent:
|
|
122
|
+
*
|
|
123
|
+
* ```typescript
|
|
124
|
+
* // Using @CustomKeys
|
|
125
|
+
* @CustomKeys({ "x-foo": "bar", "x-baz": "qux" })
|
|
126
|
+
* field: string;
|
|
127
|
+
*
|
|
128
|
+
* // Using multiple @CustomKey
|
|
129
|
+
* @CustomKey("x-foo", "bar")
|
|
130
|
+
* @CustomKey("x-baz", "qux")
|
|
131
|
+
* field: string;
|
|
132
|
+
* ```
|
|
133
|
+
*
|
|
134
|
+
* ### Use Cases
|
|
135
|
+
*
|
|
136
|
+
* - **Bulk Metadata**: Add multiple related custom properties efficiently
|
|
137
|
+
* - **Configuration**: Group related custom configuration values
|
|
138
|
+
* - **Tool Directives**: Multiple hints for code generation or documentation
|
|
16
139
|
*
|
|
17
|
-
*
|
|
140
|
+
* @param obj - Object containing custom key-value pairs
|
|
18
141
|
*
|
|
19
|
-
* @returns {Function}
|
|
20
142
|
* @decorator
|
|
21
143
|
* @validation
|
|
22
|
-
* @
|
|
23
|
-
* @parameter
|
|
24
|
-
* @schema
|
|
144
|
+
* @public
|
|
25
145
|
*/
|
|
26
146
|
export declare function CustomKeys(obj: Record<string, any>): (...args: any[]) => any;
|
|
@@ -1,42 +1,108 @@
|
|
|
1
1
|
import type { JSONSchema7Type } from "json-schema";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Specifies a default value for a property in the JSON Schema.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* The `@Default()` decorator sets the default value that should be used when a property
|
|
6
|
+
* is not provided in the input. This is primarily used for documentation in OpenAPI specs
|
|
7
|
+
* and can be used by validation libraries that support default value insertion.
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
* It is RECOMMENDED that a default value be valid against the associated schema.
|
|
11
|
-
*
|
|
12
|
-
* ## Example
|
|
9
|
+
* ### Basic Usage
|
|
13
10
|
*
|
|
14
11
|
* ```typescript
|
|
15
|
-
* class
|
|
16
|
-
*
|
|
17
|
-
*
|
|
12
|
+
* class UserModel {
|
|
13
|
+
* @Default("guest")
|
|
14
|
+
* role: string = "guest";
|
|
15
|
+
*
|
|
16
|
+
* @Default(0)
|
|
17
|
+
* loginCount: number = 0;
|
|
18
|
+
*
|
|
19
|
+
* @Default(true)
|
|
20
|
+
* active: boolean = true;
|
|
18
21
|
* }
|
|
19
22
|
* ```
|
|
20
23
|
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
24
|
+
* Generated schema:
|
|
23
25
|
* ```json
|
|
24
26
|
* {
|
|
25
27
|
* "type": "object",
|
|
26
28
|
* "properties": {
|
|
27
|
-
* "
|
|
29
|
+
* "role": {
|
|
28
30
|
* "type": "string",
|
|
29
|
-
* "default": "
|
|
31
|
+
* "default": "guest"
|
|
32
|
+
* },
|
|
33
|
+
* "loginCount": {
|
|
34
|
+
* "type": "integer",
|
|
35
|
+
* "default": 0
|
|
36
|
+
* },
|
|
37
|
+
* "active": {
|
|
38
|
+
* "type": "boolean",
|
|
39
|
+
* "default": true
|
|
30
40
|
* }
|
|
31
41
|
* }
|
|
32
42
|
* }
|
|
33
43
|
* ```
|
|
34
44
|
*
|
|
35
|
-
*
|
|
45
|
+
* ### With Objects
|
|
46
|
+
*
|
|
47
|
+
* ```typescript
|
|
48
|
+
* class ConfigModel {
|
|
49
|
+
* @Default({ theme: "light", lang: "en" })
|
|
50
|
+
* preferences: object = { theme: "light", lang: "en" };
|
|
51
|
+
* }
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* ### With Arrays
|
|
55
|
+
*
|
|
56
|
+
* ```typescript
|
|
57
|
+
* class ListModel {
|
|
58
|
+
* @Default([])
|
|
59
|
+
* items: string[] = [];
|
|
60
|
+
* }
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* ### Dynamic Defaults with Functions
|
|
64
|
+
*
|
|
65
|
+
* ```typescript
|
|
66
|
+
* class TimestampModel {
|
|
67
|
+
* @Default(() => new Date().toISOString())
|
|
68
|
+
* createdAt: string;
|
|
69
|
+
* // Default value computed at schema generation time
|
|
70
|
+
* }
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* ### Use Cases
|
|
74
|
+
*
|
|
75
|
+
* - **Optional Configuration**: Provide sensible defaults for config properties
|
|
76
|
+
* - **API Documentation**: Show default values in OpenAPI/Swagger docs
|
|
77
|
+
* - **Form Generation**: Pre-populate form fields with default values
|
|
78
|
+
* - **Database Models**: Document default column values
|
|
79
|
+
*
|
|
80
|
+
* ### Important Notes
|
|
81
|
+
*
|
|
82
|
+
* - The decorator adds default info to the schema but doesn't set TypeScript property initializers
|
|
83
|
+
* - Recommended to match TypeScript initializer with decorator value for consistency
|
|
84
|
+
* - Default value should be valid according to the property's schema
|
|
85
|
+
* - Not all validation libraries automatically apply defaults
|
|
86
|
+
*
|
|
87
|
+
* ### With Validation
|
|
88
|
+
*
|
|
89
|
+
* ```typescript
|
|
90
|
+
* class PaginationParams {
|
|
91
|
+
* @Default(10)
|
|
92
|
+
* @Minimum(1)
|
|
93
|
+
* @Maximum(100)
|
|
94
|
+
* limit: number = 10;
|
|
95
|
+
*
|
|
96
|
+
* @Default(0)
|
|
97
|
+
* @Minimum(0)
|
|
98
|
+
* offset: number = 0;
|
|
99
|
+
* }
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
102
|
+
* @param defaultValue - The default value (or function returning the value)
|
|
103
|
+
*
|
|
36
104
|
* @decorator
|
|
37
105
|
* @validation
|
|
38
|
-
* @
|
|
39
|
-
* @schema
|
|
40
|
-
* @input
|
|
106
|
+
* @public
|
|
41
107
|
*/
|
|
42
108
|
export declare function Default(defaultValue: JSONSchema7Type | undefined | (() => JSONSchema7Type)): (...args: any[]) => any;
|
|
@@ -1 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Marks a property as the discriminator key for polymorphic type resolution.
|
|
3
|
+
*
|
|
4
|
+
* The `@DiscriminatorKey()` decorator identifies which property in a base class should
|
|
5
|
+
* be used to determine the concrete type in inheritance hierarchies. This is essential
|
|
6
|
+
* for proper serialization/deserialization of polymorphic objects and for generating
|
|
7
|
+
* accurate OpenAPI schemas with discriminators.
|
|
8
|
+
*
|
|
9
|
+
* ### Basic Usage
|
|
10
|
+
*
|
|
11
|
+
* ```typescript
|
|
12
|
+
* abstract class Animal {
|
|
13
|
+
* @DiscriminatorKey()
|
|
14
|
+
* type: string;
|
|
15
|
+
*
|
|
16
|
+
* @Property()
|
|
17
|
+
* name: string;
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* @DiscriminatorValue("dog")
|
|
21
|
+
* class Dog extends Animal {
|
|
22
|
+
* @Property()
|
|
23
|
+
* breed: string;
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* @DiscriminatorValue("cat")
|
|
27
|
+
* class Cat extends Animal {
|
|
28
|
+
* @Property()
|
|
29
|
+
* indoor: boolean;
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* ### How It Works
|
|
34
|
+
*
|
|
35
|
+
* When deserializing, the framework:
|
|
36
|
+
* 1. Reads the discriminator key property value
|
|
37
|
+
* 2. Maps it to the correct concrete class using `@DiscriminatorValue()`
|
|
38
|
+
* 3. Instantiates and validates the appropriate subclass
|
|
39
|
+
*
|
|
40
|
+
* ### With API Endpoints
|
|
41
|
+
*
|
|
42
|
+
* ```typescript
|
|
43
|
+
* @Controller("/animals")
|
|
44
|
+
* class AnimalController {
|
|
45
|
+
* @Post("/")
|
|
46
|
+
* async create(@BodyParams() animal: Animal) {
|
|
47
|
+
* // animal is automatically deserialized to Dog or Cat
|
|
48
|
+
* // based on the 'type' field in the request body
|
|
49
|
+
* }
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* ### OpenAPI Generation
|
|
54
|
+
*
|
|
55
|
+
* Generates discriminator object in OpenAPI spec:
|
|
56
|
+
*
|
|
57
|
+
* ```json
|
|
58
|
+
* {
|
|
59
|
+
* "discriminator": {
|
|
60
|
+
* "propertyName": "type",
|
|
61
|
+
* "mapping": {
|
|
62
|
+
* "dog": "#/components/schemas/Dog",
|
|
63
|
+
* "cat": "#/components/schemas/Cat"
|
|
64
|
+
* }
|
|
65
|
+
* }
|
|
66
|
+
* }
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* ### Payment Method Example
|
|
70
|
+
*
|
|
71
|
+
* ```typescript
|
|
72
|
+
* abstract class PaymentMethod {
|
|
73
|
+
* @DiscriminatorKey()
|
|
74
|
+
* method: string;
|
|
75
|
+
* }
|
|
76
|
+
*
|
|
77
|
+
* @DiscriminatorValue("credit_card")
|
|
78
|
+
* class CreditCard extends PaymentMethod {
|
|
79
|
+
* @Property()
|
|
80
|
+
* cardNumber: string;
|
|
81
|
+
*
|
|
82
|
+
* @Property()
|
|
83
|
+
* expiryDate: string;
|
|
84
|
+
* }
|
|
85
|
+
*
|
|
86
|
+
* @DiscriminatorValue("bank_transfer")
|
|
87
|
+
* class BankTransfer extends PaymentMethod {
|
|
88
|
+
* @Property()
|
|
89
|
+
* iban: string;
|
|
90
|
+
*
|
|
91
|
+
* @Property()
|
|
92
|
+
* bic: string;
|
|
93
|
+
* }
|
|
94
|
+
* ```
|
|
95
|
+
*
|
|
96
|
+
* ### Use Cases
|
|
97
|
+
*
|
|
98
|
+
* - **Polymorphic APIs**: Endpoints accepting multiple related types
|
|
99
|
+
* - **Event Systems**: Different event types with shared base structure
|
|
100
|
+
* - **Strategy Pattern**: Multiple implementation variants
|
|
101
|
+
* - **Type-Safe Deserialization**: Automatic instantiation of correct subclass
|
|
102
|
+
* - **OpenAPI Documentation**: Clear polymorphic type documentation
|
|
103
|
+
*
|
|
104
|
+
* ### Best Practices
|
|
105
|
+
*
|
|
106
|
+
* - Use on abstract base classes
|
|
107
|
+
* - Discriminator property should be required
|
|
108
|
+
* - Combine with `@DiscriminatorValue()` on subclasses
|
|
109
|
+
* - Use meaningful, stable discriminator values
|
|
110
|
+
* - Document discriminator values in API documentation
|
|
111
|
+
*
|
|
112
|
+
* ### Important Notes
|
|
113
|
+
*
|
|
114
|
+
* - Only one discriminator key per class hierarchy
|
|
115
|
+
* - The property is automatically marked with `@Property()`
|
|
116
|
+
* - All subclasses must have distinct `@DiscriminatorValue()` decorators
|
|
117
|
+
* - Discriminator values should be unique within the hierarchy
|
|
118
|
+
*
|
|
119
|
+
* @decorator
|
|
120
|
+
* @property
|
|
121
|
+
* @public
|
|
122
|
+
* @see DiscriminatorValue
|
|
123
|
+
*/
|
|
1
124
|
export declare function DiscriminatorKey(): PropertyDecorator;
|
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Adds custom error messages for validation failures using ajv-errors.
|
|
3
|
+
*
|
|
4
|
+
* The `@ErrorMsg()` decorator allows specifying custom error messages that override
|
|
5
|
+
* the default validation error messages. This requires the ajv-errors plugin to be
|
|
6
|
+
* configured with your AJV validator.
|
|
7
|
+
*
|
|
8
|
+
* ### Basic Usage
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* class UserModel {
|
|
12
|
+
* @ErrorMsg({ required: "Username is required" })
|
|
13
|
+
* @Required()
|
|
14
|
+
* username: string;
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* ### Multiple Validation Errors
|
|
19
|
+
*
|
|
20
|
+
* ```typescript
|
|
21
|
+
* class PasswordModel {
|
|
22
|
+
* @ErrorMsg({
|
|
23
|
+
* required: "Password is required",
|
|
24
|
+
* minLength: "Password must be at least 8 characters",
|
|
25
|
+
* pattern: "Password must contain letters and numbers"
|
|
26
|
+
* })
|
|
27
|
+
* @Required()
|
|
28
|
+
* @MinLength(8)
|
|
29
|
+
* @Pattern(/^(?=.*[A-Za-z])(?=.*\d)/)
|
|
30
|
+
* password: string;
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* ### Use Cases
|
|
35
|
+
*
|
|
36
|
+
* - **User-Friendly Messages**: Replace technical validation errors with clear messages
|
|
37
|
+
* - **Localization**: Provide translated error messages
|
|
38
|
+
* - **Business Context**: Add domain-specific error descriptions
|
|
39
|
+
* - **API Consistency**: Standardize error message formats
|
|
40
|
+
*
|
|
41
|
+
* ### Important Notes
|
|
42
|
+
*
|
|
43
|
+
* - Requires ajv-errors plugin to be installed and configured
|
|
44
|
+
* - Keys should match the validation keywords (required, minLength, etc.)
|
|
45
|
+
* - Messages are stored as custom schema keys
|
|
46
|
+
*
|
|
47
|
+
* @param obj - Object mapping validation keywords to custom error messages
|
|
48
|
+
*
|
|
3
49
|
* @decorator
|
|
4
|
-
* @
|
|
50
|
+
* @public
|
|
5
51
|
*/
|
|
6
52
|
export declare function ErrorMsg(obj: Record<string, string>): (...args: any[]) => any;
|
|
@@ -1,11 +1,73 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Adds example values to the schema for documentation purposes.
|
|
3
|
+
*
|
|
4
|
+
* The `@Example()` decorator provides sample values that appear in OpenAPI/Swagger
|
|
5
|
+
* documentation, helping API consumers understand expected data formats. Multiple
|
|
6
|
+
* examples can be provided, and they're displayed in API documentation tools.
|
|
7
|
+
*
|
|
8
|
+
* ### On Properties
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* class UserModel {
|
|
12
|
+
* @Example("john.doe@example.com", "jane.smith@example.com")
|
|
13
|
+
* email: string;
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* ### On Classes
|
|
18
|
+
*
|
|
19
|
+
* ```typescript
|
|
20
|
+
* @Example({ name: "John", age: 30 })
|
|
21
|
+
* @Example({ name: "Jane", age: 25 })
|
|
22
|
+
* class PersonModel {
|
|
23
|
+
* @Property()
|
|
24
|
+
* name: string;
|
|
25
|
+
*
|
|
26
|
+
* @Property()
|
|
27
|
+
* age: number;
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* ### Complex Objects
|
|
32
|
+
*
|
|
33
|
+
* ```typescript
|
|
34
|
+
* class OrderModel {
|
|
35
|
+
* @Example(
|
|
36
|
+
* { id: "ORD-123", total: 99.99, items: ["item1", "item2"] },
|
|
37
|
+
* { id: "ORD-456", total: 149.99, items: ["item3"] }
|
|
38
|
+
* )
|
|
39
|
+
* orderData: object;
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* ### With Validation
|
|
44
|
+
*
|
|
45
|
+
* ```typescript
|
|
46
|
+
* class ProductModel {
|
|
47
|
+
* @Example("Electronics", "Clothing", "Books")
|
|
48
|
+
* @MinLength(3)
|
|
49
|
+
* @MaxLength(50)
|
|
50
|
+
* category: string;
|
|
51
|
+
* }
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* ### Use Cases
|
|
55
|
+
*
|
|
56
|
+
* - **API Documentation**: Show realistic example values
|
|
57
|
+
* - **Interactive Docs**: Populate try-it-out forms with examples
|
|
58
|
+
* - **Code Generation**: Provide samples for client SDK generation
|
|
59
|
+
* - **Testing**: Reference values for test cases
|
|
60
|
+
*
|
|
61
|
+
* ### Important Notes
|
|
62
|
+
*
|
|
63
|
+
* - Examples are for documentation only, not validation
|
|
64
|
+
* - Multiple examples can be provided
|
|
65
|
+
* - Examples should match the property's type and constraints
|
|
66
|
+
* - Appears in OpenAPI spec under `examples` field
|
|
67
|
+
*
|
|
68
|
+
* @param examples - One or more example values
|
|
3
69
|
*
|
|
4
70
|
* @decorator
|
|
5
|
-
* @
|
|
6
|
-
* @schema
|
|
7
|
-
* @input
|
|
8
|
-
* @methodDecorator
|
|
9
|
-
* @classDecorator
|
|
71
|
+
* @public
|
|
10
72
|
*/
|
|
11
73
|
export declare function Example(...examples: any[]): Function;
|
|
@@ -1,12 +1,105 @@
|
|
|
1
1
|
import { OpenSpecHash, OpenSpecRef, OS3Example } from "@tsed/openspec";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Adds named examples for parameters in OpenAPI 3.0 format.
|
|
4
|
+
*
|
|
5
|
+
* The `@Examples()` decorator provides named examples specifically for parameters,
|
|
6
|
+
* following the OpenAPI 3.0 specification format. Unlike `@Example()`, this accepts
|
|
7
|
+
* an object with named example entries, each with a value and optional description.
|
|
8
|
+
*
|
|
9
|
+
* ### Basic Usage
|
|
10
|
+
*
|
|
11
|
+
* ```typescript
|
|
12
|
+
* @Controller("/users")
|
|
13
|
+
* class UserController {
|
|
14
|
+
* @Get("/:id")
|
|
15
|
+
* async getUser(
|
|
16
|
+
* @Examples({
|
|
17
|
+
* admin: {
|
|
18
|
+
* value: "admin-123",
|
|
19
|
+
* summary: "Admin user ID"
|
|
20
|
+
* },
|
|
21
|
+
* regular: {
|
|
22
|
+
* value: "user-456",
|
|
23
|
+
* summary: "Regular user ID"
|
|
24
|
+
* }
|
|
25
|
+
* })
|
|
26
|
+
* @PathParams("id")
|
|
27
|
+
* id: string
|
|
28
|
+
* ) {}
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* ### Query Parameters
|
|
33
|
+
*
|
|
34
|
+
* ```typescript
|
|
35
|
+
* @Get("/search")
|
|
36
|
+
* async search(
|
|
37
|
+
* @Examples({
|
|
38
|
+
* simple: {
|
|
39
|
+
* value: "typescript",
|
|
40
|
+
* summary: "Simple keyword search"
|
|
41
|
+
* },
|
|
42
|
+
* complex: {
|
|
43
|
+
* value: "typescript AND nodejs",
|
|
44
|
+
* summary: "Boolean search query"
|
|
45
|
+
* }
|
|
46
|
+
* })
|
|
47
|
+
* @QueryParams("q")
|
|
48
|
+
* query: string
|
|
49
|
+
* ) {}
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* ### With Descriptions
|
|
53
|
+
*
|
|
54
|
+
* ```typescript
|
|
55
|
+
* @Post("/filter")
|
|
56
|
+
* async filter(
|
|
57
|
+
* @Examples({
|
|
58
|
+
* basic: {
|
|
59
|
+
* value: { status: "active" },
|
|
60
|
+
* summary: "Filter by status",
|
|
61
|
+
* description: "Returns only active records"
|
|
62
|
+
* },
|
|
63
|
+
* advanced: {
|
|
64
|
+
* value: { status: "active", role: "admin" },
|
|
65
|
+
* summary: "Complex filter",
|
|
66
|
+
* description: "Combines multiple filter criteria"
|
|
67
|
+
* }
|
|
68
|
+
* })
|
|
69
|
+
* @BodyParams()
|
|
70
|
+
* filters: object
|
|
71
|
+
* ) {}
|
|
72
|
+
* ```
|
|
73
|
+
*
|
|
74
|
+
* ### Use Cases
|
|
75
|
+
*
|
|
76
|
+
* - **Interactive Docs**: Provide multiple example scenarios in Swagger UI
|
|
77
|
+
* - **API Education**: Show different use cases with descriptions
|
|
78
|
+
* - **Testing**: Reference examples for integration tests
|
|
79
|
+
* - **Client Generation**: Provide varied examples for SDK documentation
|
|
80
|
+
*
|
|
81
|
+
* ### OpenAPI 3.0 Format
|
|
82
|
+
*
|
|
83
|
+
* Generates:
|
|
84
|
+
* ```json
|
|
85
|
+
* {
|
|
86
|
+
* "examples": {
|
|
87
|
+
* "admin": {
|
|
88
|
+
* "value": "admin-123",
|
|
89
|
+
* "summary": "Admin user ID"
|
|
90
|
+
* }
|
|
91
|
+
* }
|
|
92
|
+
* }
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* ### vs @Example
|
|
96
|
+
*
|
|
97
|
+
* - `@Examples()`: Named examples with summaries/descriptions (OpenAPI 3.0)
|
|
98
|
+
* - `@Example()`: Simple array of values
|
|
99
|
+
*
|
|
100
|
+
* @param examples - Object mapping names to example definitions
|
|
4
101
|
*
|
|
5
102
|
* @decorator
|
|
6
|
-
* @
|
|
7
|
-
* @schema
|
|
8
|
-
* @input
|
|
9
|
-
* @methodDecorator
|
|
10
|
-
* @classDecorator
|
|
103
|
+
* @public
|
|
11
104
|
*/
|
|
12
105
|
export declare function Examples(examples: OpenSpecHash<OS3Example | OpenSpecRef>): ParameterDecorator;
|