@tsed/schema 8.19.2 → 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,3 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP and WebSocket operation verbs supported by Ts.ED.
|
|
3
|
+
*
|
|
4
|
+
* This enum defines all supported operation verbs for HTTP endpoints,
|
|
5
|
+
* WebSocket operations, and special routing cases. These verbs are used
|
|
6
|
+
* to define route handlers and generate OpenAPI specifications.
|
|
7
|
+
*
|
|
8
|
+
* ### HTTP Verbs
|
|
9
|
+
* Standard HTTP methods (GET, POST, PUT, PATCH, DELETE, etc.) are supported
|
|
10
|
+
* for RESTful API operations.
|
|
11
|
+
*
|
|
12
|
+
* ### WebSocket Verbs
|
|
13
|
+
* PUBLISH and SUBSCRIBE are used for WebSocket operations and message patterns.
|
|
14
|
+
*
|
|
15
|
+
* ### Special Verbs
|
|
16
|
+
* - ALL: Matches all HTTP methods for a route
|
|
17
|
+
* - CUSTOM: Allows custom HTTP methods
|
|
18
|
+
*
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
1
21
|
export var OperationVerbs;
|
|
2
22
|
(function (OperationVerbs) {
|
|
3
23
|
OperationVerbs["ALL"] = "ALL";
|
|
@@ -13,6 +33,14 @@ export var OperationVerbs;
|
|
|
13
33
|
OperationVerbs["SUBSCRIBE"] = "SUBSCRIBE";
|
|
14
34
|
OperationVerbs["CUSTOM"] = "CUSTOM";
|
|
15
35
|
})(OperationVerbs || (OperationVerbs = {}));
|
|
36
|
+
/**
|
|
37
|
+
* List of HTTP operation verbs including the special ALL and CUSTOM verbs.
|
|
38
|
+
*
|
|
39
|
+
* This array contains all HTTP methods supported for REST API operations,
|
|
40
|
+
* excluding WebSocket-specific verbs (PUBLISH, SUBSCRIBE).
|
|
41
|
+
*
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
16
44
|
export const OPERATION_HTTP_VERBS = [
|
|
17
45
|
OperationVerbs.ALL,
|
|
18
46
|
OperationVerbs.GET,
|
|
@@ -25,4 +53,11 @@ export const OPERATION_HTTP_VERBS = [
|
|
|
25
53
|
OperationVerbs.TRACE,
|
|
26
54
|
OperationVerbs.CUSTOM
|
|
27
55
|
];
|
|
56
|
+
/**
|
|
57
|
+
* List of WebSocket operation verbs for pub/sub messaging patterns.
|
|
58
|
+
*
|
|
59
|
+
* These verbs are used to define WebSocket message handlers in Ts.ED applications.
|
|
60
|
+
*
|
|
61
|
+
* @public
|
|
62
|
+
*/
|
|
28
63
|
export const OPERATION_WS_VERBS = [OperationVerbs.PUBLISH, OperationVerbs.SUBSCRIBE];
|
|
@@ -1,3 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vendor extension keys used in JSON schemas and OpenAPI specifications.
|
|
3
|
+
*
|
|
4
|
+
* These keys are used as custom extension properties (prefixed with `x-` in OpenAPI)
|
|
5
|
+
* to store Ts.ED-specific metadata in generated schemas. Vendor extensions enable
|
|
6
|
+
* advanced features like generic type handling, group-based serialization, and
|
|
7
|
+
* nullable type support.
|
|
8
|
+
*
|
|
9
|
+
* ### Usage
|
|
10
|
+
*
|
|
11
|
+
* Vendor keys are automatically added to schemas by decorators and mappers.
|
|
12
|
+
* They preserve TypeScript type information and enable runtime schema transformations.
|
|
13
|
+
*
|
|
14
|
+
* ### Generic Type Keys
|
|
15
|
+
* - GENERIC_LABELS: Labels for generic type parameters
|
|
16
|
+
* - GENERIC_LABEL: Single generic type label
|
|
17
|
+
* - GENERIC_OF: Generic type reference
|
|
18
|
+
*
|
|
19
|
+
* ### Group Keys
|
|
20
|
+
* - GROUPS: Property group memberships
|
|
21
|
+
* - GROUPS_NAME: Named group identifier
|
|
22
|
+
* - FORWARD_GROUPS: Groups forwarded to nested properties
|
|
23
|
+
* - ALLOWED_GROUPS: Groups allowed for access
|
|
24
|
+
*
|
|
25
|
+
* ### Type Keys
|
|
26
|
+
* - NULLABLE: Marks a type as nullable
|
|
27
|
+
*
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
1
30
|
export var VendorKeys;
|
|
2
31
|
(function (VendorKeys) {
|
|
3
32
|
VendorKeys["GENERIC_LABELS"] = "genericLabels";
|
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
import { constantCase } from "change-case";
|
|
2
2
|
import statuses from "statuses";
|
|
3
|
+
/**
|
|
4
|
+
* Map of HTTP status codes to their human-readable labels and constant names.
|
|
5
|
+
*
|
|
6
|
+
* This constant provides a standardized lookup table for all HTTP status codes,
|
|
7
|
+
* mapping each code to both a descriptive label and a constant-case identifier.
|
|
8
|
+
* The mapping is generated from the standard HTTP status code definitions.
|
|
9
|
+
*
|
|
10
|
+
* ### Structure
|
|
11
|
+
*
|
|
12
|
+
* Each entry contains:
|
|
13
|
+
* - `label`: Human-readable status message (e.g., "Not Found")
|
|
14
|
+
* - `code`: Constant-case identifier (e.g., "NOT_FOUND")
|
|
15
|
+
*
|
|
16
|
+
* ### Usage
|
|
17
|
+
*
|
|
18
|
+
* ```typescript
|
|
19
|
+
* // Access status information
|
|
20
|
+
* const status404 = HTTP_STATUS_MESSAGES["404"];
|
|
21
|
+
* // { label: "Not Found", code: "NOT_FOUND" }
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
3
26
|
export const HTTP_STATUS_MESSAGES = statuses.codes.reduce((map, code) => {
|
|
4
27
|
const message = String(statuses(code));
|
|
5
28
|
return {
|
|
@@ -9,9 +32,45 @@ export const HTTP_STATUS_MESSAGES = statuses.codes.reduce((map, code) => {
|
|
|
9
32
|
}, {});
|
|
10
33
|
HTTP_STATUS_MESSAGES["200"].label = "Success";
|
|
11
34
|
HTTP_STATUS_MESSAGES["200"].code = "SUCCESS";
|
|
35
|
+
/**
|
|
36
|
+
* Get the constant-case identifier for an HTTP status code.
|
|
37
|
+
*
|
|
38
|
+
* Converts a status code to its constant representation (e.g., 404 → "NOT_FOUND").
|
|
39
|
+
* Useful for generating TypeScript constants and error codes.
|
|
40
|
+
*
|
|
41
|
+
* ### Usage
|
|
42
|
+
*
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const constant = getStatusConstant(404);
|
|
45
|
+
* // Returns: "NOT_FOUND"
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @param status - The HTTP status code (number or string)
|
|
49
|
+
* @returns The constant-case identifier, or undefined if not found
|
|
50
|
+
*
|
|
51
|
+
* @public
|
|
52
|
+
*/
|
|
12
53
|
export function getStatusConstant(status) {
|
|
13
54
|
return HTTP_STATUS_MESSAGES[status]?.code;
|
|
14
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Get the human-readable message for an HTTP status code.
|
|
58
|
+
*
|
|
59
|
+
* Returns the descriptive label associated with a status code
|
|
60
|
+
* (e.g., 404 → "Not Found").
|
|
61
|
+
*
|
|
62
|
+
* ### Usage
|
|
63
|
+
*
|
|
64
|
+
* ```typescript
|
|
65
|
+
* const message = getStatusMessage(404);
|
|
66
|
+
* // Returns: "Not Found"
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* @param status - The HTTP status code (number or string)
|
|
70
|
+
* @returns The human-readable status message, or undefined if not found
|
|
71
|
+
*
|
|
72
|
+
* @public
|
|
73
|
+
*/
|
|
15
74
|
export function getStatusMessage(status) {
|
|
16
75
|
return HTTP_STATUS_MESSAGES[status]?.label;
|
|
17
76
|
}
|
|
@@ -1,8 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON Schema properties for composition keywords (oneOf, allOf, anyOf).
|
|
3
|
+
*
|
|
4
|
+
* These properties define schema composition, allowing multiple schemas to be
|
|
5
|
+
* combined using different logical operators.
|
|
6
|
+
*
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
1
9
|
export const MANY_OF_PROPERTIES = ["oneOf", "allOf", "anyOf"];
|
|
10
|
+
/**
|
|
11
|
+
* JSON Schema properties specific to string type validation.
|
|
12
|
+
*
|
|
13
|
+
* These properties control string length constraints, pattern matching,
|
|
14
|
+
* and format validation (e.g., email, uri, date-time).
|
|
15
|
+
*
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
2
18
|
export const STRING_PROPERTIES = ["minLength", "maxLength", "pattern", "format"];
|
|
19
|
+
/**
|
|
20
|
+
* JSON Schema properties specific to boolean type validation.
|
|
21
|
+
*
|
|
22
|
+
* Currently empty as boolean types have no specific validation properties
|
|
23
|
+
* beyond the type itself.
|
|
24
|
+
*
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
3
27
|
export const BOOLEAN_PROPERTIES = [];
|
|
28
|
+
/**
|
|
29
|
+
* JSON Schema properties specific to number and integer type validation.
|
|
30
|
+
*
|
|
31
|
+
* These properties control numeric range constraints and divisibility rules.
|
|
32
|
+
*
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
4
35
|
export const NUMBER_PROPERTIES = ["minimum", "maximum", "exclusiveMinimum", "exclusiveMaximum", "multipleOf"];
|
|
36
|
+
/**
|
|
37
|
+
* JSON Schema properties specific to array type validation.
|
|
38
|
+
*
|
|
39
|
+
* These properties control array size constraints, uniqueness requirements,
|
|
40
|
+
* item schemas, and containment rules.
|
|
41
|
+
*
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
5
44
|
export const ARRAY_PROPERTIES = ["maxItems", "minItems", "uniqueItems", "items", "contains", "maxContains", "minContains"];
|
|
45
|
+
/**
|
|
46
|
+
* JSON Schema properties specific to object type validation.
|
|
47
|
+
*
|
|
48
|
+
* These properties control object property constraints, pattern-based properties,
|
|
49
|
+
* and property dependencies.
|
|
50
|
+
*
|
|
51
|
+
* @public
|
|
52
|
+
*/
|
|
6
53
|
export const OBJECT_PROPERTIES = [
|
|
7
54
|
"maxItems",
|
|
8
55
|
"minItems",
|
|
@@ -14,4 +61,12 @@ export const OBJECT_PROPERTIES = [
|
|
|
14
61
|
"patternProperties",
|
|
15
62
|
"dependencies"
|
|
16
63
|
];
|
|
64
|
+
/**
|
|
65
|
+
* JSON Schema properties common to all types.
|
|
66
|
+
*
|
|
67
|
+
* These properties can be applied to any schema regardless of its type,
|
|
68
|
+
* defining constant values or enumerated options.
|
|
69
|
+
*
|
|
70
|
+
* @public
|
|
71
|
+
*/
|
|
17
72
|
export const COMMON_PROPERTIES = ["const", "enum"];
|
|
@@ -1,13 +1,101 @@
|
|
|
1
1
|
import { Store } from "@tsed/core";
|
|
2
2
|
import { JsonEntityFn } from "../common/jsonEntityFn.js";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Declares child controller classes for route composition and hierarchy.
|
|
5
|
+
*
|
|
6
|
+
* The `@Children()` decorator establishes parent-child relationships between controllers,
|
|
7
|
+
* allowing you to compose complex routing hierarchies and organize related endpoints into
|
|
8
|
+
* logical groups. Child controllers inherit the parent's base path.
|
|
9
|
+
*
|
|
10
|
+
* ### Basic Usage
|
|
11
|
+
*
|
|
12
|
+
* ```typescript
|
|
13
|
+
* @Controller("/users")
|
|
14
|
+
* @Children(UserProfileController, UserSettingsController)
|
|
15
|
+
* class UserController {
|
|
16
|
+
* @Get("/")
|
|
17
|
+
* list() {
|
|
18
|
+
* // GET /users/
|
|
19
|
+
* }
|
|
20
|
+
* }
|
|
21
|
+
*
|
|
22
|
+
* @Controller("/profile")
|
|
23
|
+
* class UserProfileController {
|
|
24
|
+
* @Get("/")
|
|
25
|
+
* getProfile() {
|
|
26
|
+
* // GET /users/profile/
|
|
27
|
+
* }
|
|
28
|
+
* }
|
|
29
|
+
*
|
|
30
|
+
* @Controller("/settings")
|
|
31
|
+
* class UserSettingsController {
|
|
32
|
+
* @Get("/")
|
|
33
|
+
* getSettings() {
|
|
34
|
+
* // GET /users/settings/
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* ### Multi-Level Hierarchy
|
|
40
|
+
*
|
|
41
|
+
* ```typescript
|
|
42
|
+
* @Controller("/api")
|
|
43
|
+
* @Children(UsersController, ProductsController)
|
|
44
|
+
* class ApiController {}
|
|
45
|
+
*
|
|
46
|
+
* @Controller("/users")
|
|
47
|
+
* @Children(UserProfileController, UserOrdersController)
|
|
48
|
+
* class UsersController {}
|
|
49
|
+
*
|
|
50
|
+
* @Controller("/profile")
|
|
51
|
+
* class UserProfileController {
|
|
52
|
+
* @Get("/")
|
|
53
|
+
* get() {
|
|
54
|
+
* // GET /api/users/profile/
|
|
55
|
+
* }
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* ### Resource Organization
|
|
60
|
+
*
|
|
61
|
+
* ```typescript
|
|
62
|
+
* @Controller("/admin")
|
|
63
|
+
* @Children(
|
|
64
|
+
* AdminUsersController,
|
|
65
|
+
* AdminProductsController,
|
|
66
|
+
* AdminReportsController
|
|
67
|
+
* )
|
|
68
|
+
* class AdminController {
|
|
69
|
+
* // Parent controller for all admin routes
|
|
70
|
+
* }
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* ### Use Cases
|
|
74
|
+
*
|
|
75
|
+
* - **Route Organization**: Group related endpoints logically
|
|
76
|
+
* - **API Versioning**: Organize versions with child controllers
|
|
77
|
+
* - **Module Structure**: Mirror application module hierarchy in routes
|
|
78
|
+
* - **Access Control**: Apply middleware at parent level for all children
|
|
79
|
+
* - **Documentation**: Generate organized OpenAPI specs with grouped endpoints
|
|
80
|
+
*
|
|
81
|
+
* ### Benefits
|
|
82
|
+
*
|
|
83
|
+
* - **Maintainability**: Keep related routes together
|
|
84
|
+
* - **Scalability**: Easy to add new endpoint groups
|
|
85
|
+
* - **Clarity**: Clear route hierarchy in codebase
|
|
86
|
+
* - **Reusability**: Share middleware and configuration across children
|
|
87
|
+
*
|
|
88
|
+
* ### Important Notes
|
|
89
|
+
*
|
|
90
|
+
* - Children automatically inherit parent's base path
|
|
91
|
+
* - Parent-child relationship is stored in metadata
|
|
92
|
+
* - Useful for generating structured API documentation
|
|
93
|
+
* - Middleware on parent applies to all children
|
|
94
|
+
*
|
|
95
|
+
* @param children - Controller classes to register as children
|
|
5
96
|
*
|
|
6
97
|
* @decorator
|
|
7
|
-
* @
|
|
8
|
-
* @schema
|
|
9
|
-
* @classDecorator
|
|
10
|
-
* @param children
|
|
98
|
+
* @public
|
|
11
99
|
*/
|
|
12
100
|
export function Children(...children) {
|
|
13
101
|
return JsonEntityFn((store) => {
|
|
@@ -1,45 +1,149 @@
|
|
|
1
1
|
import { snakeCase } from "change-case";
|
|
2
2
|
import { JsonEntityFn } from "../common/jsonEntityFn.js";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Declares the discriminator value for a subclass in a polymorphic hierarchy.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
6
|
+
* The `@DiscriminatorValue()` decorator specifies the value that should be present in the
|
|
7
|
+
* discriminator key property to identify this specific subclass. This is used together with
|
|
8
|
+
* `@DiscriminatorKey()` on the base class for proper polymorphic type resolution during
|
|
9
|
+
* serialization and deserialization.
|
|
10
|
+
*
|
|
11
|
+
* ### Basic Usage
|
|
12
|
+
*
|
|
13
|
+
* ```typescript
|
|
14
|
+
* abstract class Event {
|
|
15
|
+
* @DiscriminatorKey()
|
|
9
16
|
* type: string;
|
|
10
17
|
*
|
|
11
18
|
* @Property()
|
|
12
|
-
*
|
|
19
|
+
* timestamp: Date;
|
|
13
20
|
* }
|
|
14
21
|
*
|
|
15
22
|
* @DiscriminatorValue("page_view")
|
|
16
|
-
*
|
|
17
|
-
* @
|
|
23
|
+
* class PageView extends Event {
|
|
24
|
+
* @Property()
|
|
18
25
|
* url: string;
|
|
26
|
+
*
|
|
27
|
+
* @Property()
|
|
28
|
+
* referrer: string;
|
|
19
29
|
* }
|
|
20
30
|
*
|
|
21
31
|
* @DiscriminatorValue("action")
|
|
22
|
-
*
|
|
23
|
-
* @
|
|
24
|
-
*
|
|
32
|
+
* class Action extends Event {
|
|
33
|
+
* @Property()
|
|
34
|
+
* actionName: string;
|
|
35
|
+
*
|
|
36
|
+
* @Property()
|
|
37
|
+
* metadata: object;
|
|
25
38
|
* }
|
|
39
|
+
* ```
|
|
26
40
|
*
|
|
27
|
-
*
|
|
28
|
-
* export class CustomAction extends Event {
|
|
29
|
-
* @Required()
|
|
30
|
-
* event: string;
|
|
41
|
+
* ### Auto-Generated Values
|
|
31
42
|
*
|
|
43
|
+
* If no value is provided, it auto-generates from the class name in snake_case:
|
|
44
|
+
*
|
|
45
|
+
* ```typescript
|
|
46
|
+
* @DiscriminatorValue() // Generates "custom_action"
|
|
47
|
+
* class CustomAction extends Event {
|
|
32
48
|
* @Property()
|
|
33
|
-
*
|
|
49
|
+
* customField: string;
|
|
34
50
|
* }
|
|
35
51
|
* ```
|
|
36
52
|
*
|
|
53
|
+
* ### Multiple Values (Aliases)
|
|
54
|
+
*
|
|
55
|
+
* Support multiple discriminator values for the same class:
|
|
56
|
+
*
|
|
57
|
+
* ```typescript
|
|
58
|
+
* @DiscriminatorValue("click", "tap", "press")
|
|
59
|
+
* class ClickEvent extends Event {
|
|
60
|
+
* // Matches when type is "click", "tap", OR "press"
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* ### Payment Methods Example
|
|
65
|
+
*
|
|
66
|
+
* ```typescript
|
|
67
|
+
* abstract class PaymentMethod {
|
|
68
|
+
* @DiscriminatorKey()
|
|
69
|
+
* type: string;
|
|
70
|
+
* }
|
|
71
|
+
*
|
|
72
|
+
* @DiscriminatorValue("credit_card")
|
|
73
|
+
* class CreditCardPayment extends PaymentMethod {
|
|
74
|
+
* @Property()
|
|
75
|
+
* cardNumber: string;
|
|
76
|
+
*
|
|
77
|
+
* @Property()
|
|
78
|
+
* cvv: string;
|
|
79
|
+
* }
|
|
80
|
+
*
|
|
81
|
+
* @DiscriminatorValue("paypal")
|
|
82
|
+
* class PayPalPayment extends PaymentMethod {
|
|
83
|
+
* @Property()
|
|
84
|
+
* email: string;
|
|
85
|
+
* }
|
|
86
|
+
*
|
|
87
|
+
* @DiscriminatorValue("bank_transfer")
|
|
88
|
+
* class BankTransfer extends PaymentMethod {
|
|
89
|
+
* @Property()
|
|
90
|
+
* iban: string;
|
|
91
|
+
* }
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
94
|
+
* ### With API Endpoints
|
|
95
|
+
*
|
|
96
|
+
* ```typescript
|
|
97
|
+
* @Post("/events")
|
|
98
|
+
* async trackEvent(@BodyParams() event: Event) {
|
|
99
|
+
* // Framework automatically deserializes to correct subclass
|
|
100
|
+
* // based on the 'type' field value in request body
|
|
101
|
+
* if (event instanceof PageView) {
|
|
102
|
+
* // Handle page view
|
|
103
|
+
* } else if (event instanceof Action) {
|
|
104
|
+
* // Handle action
|
|
105
|
+
* }
|
|
106
|
+
* }
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* ### OpenAPI Generation
|
|
110
|
+
*
|
|
111
|
+
* Generates proper discriminator mapping:
|
|
112
|
+
*
|
|
113
|
+
* ```json
|
|
114
|
+
* {
|
|
115
|
+
* "discriminator": {
|
|
116
|
+
* "propertyName": "type",
|
|
117
|
+
* "mapping": {
|
|
118
|
+
* "page_view": "#/components/schemas/PageView",
|
|
119
|
+
* "action": "#/components/schemas/Action"
|
|
120
|
+
* }
|
|
121
|
+
* }
|
|
122
|
+
* }
|
|
123
|
+
* ```
|
|
124
|
+
*
|
|
125
|
+
* ### Use Cases
|
|
126
|
+
*
|
|
127
|
+
* - **Event Systems**: Different event types with shared structure
|
|
128
|
+
* - **Payment Processing**: Multiple payment method types
|
|
129
|
+
* - **Notification Systems**: Various notification channels
|
|
130
|
+
* - **Content Types**: Polymorphic content (articles, videos, images)
|
|
131
|
+
* - **API Responses**: Different response formats based on type
|
|
132
|
+
*
|
|
133
|
+
* ### Important Notes
|
|
134
|
+
*
|
|
135
|
+
* - Must be used on subclasses that extend a class with `@DiscriminatorKey()`
|
|
136
|
+
* - Each subclass should have a unique discriminator value
|
|
137
|
+
* - If omitted, value is auto-generated from class name (snake_case)
|
|
138
|
+
* - Multiple values create aliases for backward compatibility
|
|
139
|
+
* - Works with OpenAPI discriminator specification
|
|
140
|
+
*
|
|
141
|
+
* @param value - The discriminator value (auto-generated if omitted)
|
|
142
|
+
* @param values - Additional alias values for this subclass
|
|
143
|
+
*
|
|
37
144
|
* @decorator
|
|
38
|
-
* @
|
|
39
|
-
* @
|
|
40
|
-
* @classDecorator
|
|
41
|
-
* @param value
|
|
42
|
-
* @param values
|
|
145
|
+
* @public
|
|
146
|
+
* @see DiscriminatorKey
|
|
43
147
|
*/
|
|
44
148
|
export function DiscriminatorValue(value, ...values) {
|
|
45
149
|
return JsonEntityFn((store) => {
|
|
@@ -1,23 +1,104 @@
|
|
|
1
1
|
import { CollectionOf } from "./collectionOf.js";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Validates that an array contains at least one element matching the specified type/schema.
|
|
4
4
|
*
|
|
5
|
-
* The
|
|
5
|
+
* The `@CollectionContains()` decorator uses JSON Schema's `contains` keyword to ensure
|
|
6
|
+
* that at least one item in the array is valid against the given schema. This is useful
|
|
7
|
+
* for validation where you need to guarantee certain elements exist in a collection
|
|
8
|
+
* without requiring all elements to match.
|
|
9
|
+
*
|
|
10
|
+
* ### Basic Usage
|
|
11
|
+
*
|
|
12
|
+
* ```typescript
|
|
13
|
+
* class SearchResults {
|
|
14
|
+
* @CollectionContains(String)
|
|
15
|
+
* tags: string[];
|
|
16
|
+
* // Array must contain at least one string
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* ### With String Constraints
|
|
21
|
+
*
|
|
22
|
+
* ```typescript
|
|
23
|
+
* class ValidationModel {
|
|
24
|
+
* @CollectionContains(String).MinLength(5).MaxLength(20)
|
|
25
|
+
* keywords: string[];
|
|
26
|
+
* // Must contain at least one string between 5-20 characters
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* ### With Custom Classes
|
|
6
31
|
*
|
|
7
32
|
* ```typescript
|
|
8
|
-
* class
|
|
9
|
-
*
|
|
10
|
-
*
|
|
33
|
+
* class AdminUser {
|
|
34
|
+
* @Property()
|
|
35
|
+
* role: string;
|
|
36
|
+
*
|
|
37
|
+
* @Property()
|
|
38
|
+
* permissions: string[];
|
|
39
|
+
* }
|
|
40
|
+
*
|
|
41
|
+
* class Team {
|
|
42
|
+
* @CollectionContains(AdminUser)
|
|
43
|
+
* members: User[];
|
|
44
|
+
* // Team must contain at least one AdminUser
|
|
11
45
|
* }
|
|
12
46
|
* ```
|
|
13
47
|
*
|
|
14
|
-
*
|
|
48
|
+
* ### Mixed Type Arrays
|
|
49
|
+
*
|
|
50
|
+
* ```typescript
|
|
51
|
+
* class DataSet {
|
|
52
|
+
* @CollectionContains(Number).Minimum(100)
|
|
53
|
+
* values: (number | string)[];
|
|
54
|
+
* // Must contain at least one number >= 100
|
|
55
|
+
* }
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* ### Validation Behavior
|
|
59
|
+
*
|
|
60
|
+
* ```typescript
|
|
61
|
+
* @CollectionContains(String).MinLength(3)
|
|
62
|
+
* items: string[];
|
|
63
|
+
*
|
|
64
|
+
* // Valid:
|
|
65
|
+
* ["a", "abc", "b"] // Contains "abc" with length >= 3
|
|
66
|
+
* ["hello", "world"] // Both match, but only one needed
|
|
67
|
+
*
|
|
68
|
+
* // Invalid:
|
|
69
|
+
* ["a", "b", "c"] // No string with length >= 3
|
|
70
|
+
* [] // Empty array has no matching elements
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* ### Use Cases
|
|
74
|
+
*
|
|
75
|
+
* - **Required Elements**: Ensure specific items exist in collections
|
|
76
|
+
* - **Mixed Arrays**: Validate presence of certain types in heterogeneous arrays
|
|
77
|
+
* - **Quality Checks**: Guarantee minimum quality standards for at least one item
|
|
78
|
+
* - **Search Results**: Ensure relevant results exist in search responses
|
|
79
|
+
*
|
|
80
|
+
* ### Chaining with Other Validators
|
|
81
|
+
*
|
|
82
|
+
* ```typescript
|
|
83
|
+
* class ProductList {
|
|
84
|
+
* @CollectionContains(String).MinLength(3)
|
|
85
|
+
* @MinItems(1)
|
|
86
|
+
* @MaxItems(10)
|
|
87
|
+
* names: string[];
|
|
88
|
+
* // 1-10 items, with at least one name having 3+ characters
|
|
89
|
+
* }
|
|
90
|
+
* ```
|
|
91
|
+
*
|
|
92
|
+
* ### vs CollectionOf
|
|
93
|
+
*
|
|
94
|
+
* - `@CollectionContains()`: At least **one** element must match the schema
|
|
95
|
+
* - `@CollectionOf()`: **All** elements must match the schema
|
|
96
|
+
*
|
|
97
|
+
* @param type - The type or class that at least one array element must match
|
|
98
|
+
*
|
|
15
99
|
* @decorator
|
|
16
100
|
* @validation
|
|
17
|
-
* @
|
|
18
|
-
* @schema
|
|
19
|
-
* @input
|
|
20
|
-
* @collections
|
|
101
|
+
* @public
|
|
21
102
|
*/
|
|
22
103
|
export function CollectionContains(type) {
|
|
23
104
|
return CollectionOf(type).Contains();
|