@tsed/schema 8.0.1 → 8.0.2
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/package.json +11 -10
- package/src/components/async-api/channelsMapper.ts +81 -0
- package/src/components/async-api/generate.ts +24 -0
- package/src/components/async-api/messageMapper.ts +62 -0
- package/src/components/async-api/payloadMapper.ts +61 -0
- package/src/components/async-api/responseMapper.ts +70 -0
- package/src/components/default/anyMapper.spec.ts +7 -0
- package/src/components/default/anyMapper.ts +33 -0
- package/src/components/default/classMapper.ts +55 -0
- package/src/components/default/discriminatorMappingMapper.ts +36 -0
- package/src/components/default/enumsMapper.spec.ts +64 -0
- package/src/components/default/enumsMapper.ts +17 -0
- package/src/components/default/genericsMapper.ts +70 -0
- package/src/components/default/inheritedClassMapper.ts +24 -0
- package/src/components/default/itemMapper.ts +8 -0
- package/src/components/default/lazyRefMapper.ts +21 -0
- package/src/components/default/mapMapper.ts +25 -0
- package/src/components/default/nullableMapper.ts +55 -0
- package/src/components/default/objectMapper.ts +36 -0
- package/src/components/default/ofMapper.ts +16 -0
- package/src/components/default/propertiesMapper.ts +25 -0
- package/src/components/default/requiredMapper.ts +69 -0
- package/src/components/default/schemaMapper.ts +151 -0
- package/src/components/index.ts +36 -0
- package/src/components/open-spec/discriminatorMappingMapper.ts +7 -0
- package/src/components/open-spec/enumsMapper.ts +20 -0
- package/src/components/open-spec/generate.ts +29 -0
- package/src/components/open-spec/nullableMapper.ts +51 -0
- package/src/components/open-spec/operationInFilesMapper.ts +32 -0
- package/src/components/open-spec/operationInParameterMapper.ts +56 -0
- package/src/components/open-spec/operationInParametersMapper.ts +9 -0
- package/src/components/open-spec/operationInQueryMapper.ts +74 -0
- package/src/components/open-spec/operationMapper.ts +65 -0
- package/src/components/open-spec/operationMediaMapper.ts +11 -0
- package/src/components/open-spec/operationRequestBodyMapper.ts +82 -0
- package/src/components/open-spec/operationResponseMapper.ts +26 -0
- package/src/components/open-spec/pathsMapper.ts +132 -0
- package/src/constants/OperationVerbs.ts +29 -0
- package/src/constants/httpStatusMessages.ts +21 -0
- package/src/constants/jsonSchemaProperties.ts +17 -0
- package/src/decorators/class/children.ts +22 -0
- package/src/decorators/class/discriminatorValue.ts +53 -0
- package/src/decorators/collections/collectionContains.ts +25 -0
- package/src/decorators/collections/collectionOf.spec.ts +327 -0
- package/src/decorators/collections/collectionOf.ts +182 -0
- package/src/decorators/collections/maxItems.spec.ts +40 -0
- package/src/decorators/collections/maxItems.ts +59 -0
- package/src/decorators/collections/maxProperties.spec.ts +109 -0
- package/src/decorators/collections/maxProperties.ts +82 -0
- package/src/decorators/collections/minItems.spec.ts +40 -0
- package/src/decorators/collections/minItems.ts +63 -0
- package/src/decorators/collections/minProperties.spec.ts +109 -0
- package/src/decorators/collections/minProperties.ts +81 -0
- package/src/decorators/collections/uniqueItems.spec.ts +28 -0
- package/src/decorators/collections/uniqueItems.ts +59 -0
- package/src/decorators/common/__snapshots__/groups.spec.ts.snap +321 -0
- package/src/decorators/common/additionalProperties.spec.ts +133 -0
- package/src/decorators/common/additionalProperties.ts +17 -0
- package/src/decorators/common/allOf.spec.ts +276 -0
- package/src/decorators/common/allOf.ts +19 -0
- package/src/decorators/common/allow.spec.ts +130 -0
- package/src/decorators/common/allow.ts +50 -0
- package/src/decorators/common/any.spec.ts +109 -0
- package/src/decorators/common/any.ts +45 -0
- package/src/decorators/common/anyOf.spec.ts +270 -0
- package/src/decorators/common/anyOf.ts +23 -0
- package/src/decorators/common/const.spec.ts +98 -0
- package/src/decorators/common/const.ts +68 -0
- package/src/decorators/common/customKey.spec.ts +105 -0
- package/src/decorators/common/customKey.ts +39 -0
- package/src/decorators/common/default.spec.ts +25 -0
- package/src/decorators/common/default.ts +47 -0
- package/src/decorators/common/defaultMsg.spec.ts +22 -0
- package/src/decorators/common/defaultMsg.ts +38 -0
- package/src/decorators/common/description.spec.ts +166 -0
- package/src/decorators/common/description.ts +77 -0
- package/src/decorators/common/discriminatorKey.ts +14 -0
- package/src/decorators/common/enum.spec.ts +305 -0
- package/src/decorators/common/enum.ts +111 -0
- package/src/decorators/common/errorMsg.spec.ts +57 -0
- package/src/decorators/common/errorMsg.ts +14 -0
- package/src/decorators/common/example.spec.ts +93 -0
- package/src/decorators/common/example.ts +18 -0
- package/src/decorators/common/examples.spec.ts +101 -0
- package/src/decorators/common/examples.ts +20 -0
- package/src/decorators/common/exclusiveMaximum.spec.ts +47 -0
- package/src/decorators/common/exclusiveMaximum.ts +75 -0
- package/src/decorators/common/exclusiveMinimum.spec.ts +47 -0
- package/src/decorators/common/exclusiveMinimum.ts +75 -0
- package/src/decorators/common/format.spec.ts +149 -0
- package/src/decorators/common/format.ts +486 -0
- package/src/decorators/common/forwardGroups.spec.ts +270 -0
- package/src/decorators/common/forwardGroups.ts +16 -0
- package/src/decorators/common/groups.spec.ts +968 -0
- package/src/decorators/common/groups.ts +66 -0
- package/src/decorators/common/hidden.spec.ts +52 -0
- package/src/decorators/common/hidden.ts +36 -0
- package/src/decorators/common/ignore.spec.ts +88 -0
- package/src/decorators/common/ignore.ts +66 -0
- package/src/decorators/common/integer.spec.ts +110 -0
- package/src/decorators/common/integer.ts +69 -0
- package/src/decorators/common/jsonEntityFn.spec.ts +16 -0
- package/src/decorators/common/jsonEntityFn.ts +33 -0
- package/src/decorators/common/maxLength.spec.ts +61 -0
- package/src/decorators/common/maxLength.ts +88 -0
- package/src/decorators/common/maximum.spec.ts +73 -0
- package/src/decorators/common/maximum.ts +146 -0
- package/src/decorators/common/minLength.spec.ts +186 -0
- package/src/decorators/common/minLength.ts +89 -0
- package/src/decorators/common/minimum.spec.ts +74 -0
- package/src/decorators/common/minimum.ts +145 -0
- package/src/decorators/common/multipleOf.spec.ts +33 -0
- package/src/decorators/common/multipleOf.ts +80 -0
- package/src/decorators/common/name.spec.ts +56 -0
- package/src/decorators/common/name.ts +50 -0
- package/src/decorators/common/nullable.spec.ts +818 -0
- package/src/decorators/common/nullable.ts +32 -0
- package/src/decorators/common/oneOf.spec.ts +301 -0
- package/src/decorators/common/oneOf.ts +24 -0
- package/src/decorators/common/optional.spec.ts +43 -0
- package/src/decorators/common/optional.ts +26 -0
- package/src/decorators/common/pattern.spec.ts +27 -0
- package/src/decorators/common/pattern.ts +102 -0
- package/src/decorators/common/property.ts +24 -0
- package/src/decorators/common/readOnly.spec.ts +135 -0
- package/src/decorators/common/readOnly.ts +17 -0
- package/src/decorators/common/recordOf.spec.ts +41 -0
- package/src/decorators/common/recordOf.ts +59 -0
- package/src/decorators/common/required.spec.ts +188 -0
- package/src/decorators/common/required.ts +50 -0
- package/src/decorators/common/requiredGroups.spec.ts +127 -0
- package/src/decorators/common/requiredGroups.ts +23 -0
- package/src/decorators/common/schema.spec.ts +23 -0
- package/src/decorators/common/schema.ts +64 -0
- package/src/decorators/common/title.spec.ts +20 -0
- package/src/decorators/common/title.ts +45 -0
- package/src/decorators/common/typeError.spec.ts +22 -0
- package/src/decorators/common/typeError.ts +15 -0
- package/src/decorators/common/writeOnly.spec.ts +105 -0
- package/src/decorators/common/writeOnly.ts +17 -0
- package/src/decorators/config/jsonEntityComponent.ts +16 -0
- package/src/decorators/generics/genericOf.spec.ts +246 -0
- package/src/decorators/generics/genericOf.ts +112 -0
- package/src/decorators/generics/generics.ts +22 -0
- package/src/decorators/operations/acceptMime.spec.ts +39 -0
- package/src/decorators/operations/acceptMime.ts +25 -0
- package/src/decorators/operations/consumes.spec.ts +89 -0
- package/src/decorators/operations/consumes.ts +37 -0
- package/src/decorators/operations/contentType.spec.ts +40 -0
- package/src/decorators/operations/contentType.ts +24 -0
- package/src/decorators/operations/deprecated.spec.ts +223 -0
- package/src/decorators/operations/deprecated.ts +49 -0
- package/src/decorators/operations/header.spec.ts +214 -0
- package/src/decorators/operations/header.ts +62 -0
- package/src/decorators/operations/in.spec.ts +321 -0
- package/src/decorators/operations/in.ts +141 -0
- package/src/decorators/operations/inFile.spec.ts +209 -0
- package/src/decorators/operations/inFile.ts +55 -0
- package/src/decorators/operations/location.spec.ts +51 -0
- package/src/decorators/operations/location.ts +26 -0
- package/src/decorators/operations/operation.ts +119 -0
- package/src/decorators/operations/operationId.spec.ts +43 -0
- package/src/decorators/operations/operationId.ts +22 -0
- package/src/decorators/operations/operationPath.spec.ts +42 -0
- package/src/decorators/operations/operationPath.ts +33 -0
- package/src/decorators/operations/partial.spec.ts +249 -0
- package/src/decorators/operations/partial.ts +14 -0
- package/src/decorators/operations/path.spec.ts +47 -0
- package/src/decorators/operations/path.ts +27 -0
- package/src/decorators/operations/produces.spec.ts +89 -0
- package/src/decorators/operations/produces.ts +44 -0
- package/src/decorators/operations/publish.spec.ts +26 -0
- package/src/decorators/operations/publish.ts +6 -0
- package/src/decorators/operations/redirect.spec.ts +146 -0
- package/src/decorators/operations/redirect.ts +82 -0
- package/src/decorators/operations/returns.spec.ts +1393 -0
- package/src/decorators/operations/returns.ts +662 -0
- package/src/decorators/operations/route.spec.ts +211 -0
- package/src/decorators/operations/route.ts +154 -0
- package/src/decorators/operations/security.spec.ts +162 -0
- package/src/decorators/operations/security.ts +83 -0
- package/src/decorators/operations/status.spec.ts +582 -0
- package/src/decorators/operations/status.ts +130 -0
- package/src/decorators/operations/subscribe.spec.ts +32 -0
- package/src/decorators/operations/subscribe.ts +6 -0
- package/src/decorators/operations/summary.spec.ts +45 -0
- package/src/decorators/operations/summary.ts +32 -0
- package/src/decorators/operations/tags.spec.ts +113 -0
- package/src/decorators/operations/tags.ts +53 -0
- package/src/decorators/operations/view.spec.ts +16 -0
- package/src/decorators/operations/view.ts +27 -0
- package/src/domain/DecoratorContext.ts +116 -0
- package/src/domain/JsonAliasMap.ts +19 -0
- package/src/domain/JsonClassStore.ts +35 -0
- package/src/domain/JsonDiscriminator.spec.ts +65 -0
- package/src/domain/JsonDiscriminator.ts +52 -0
- package/src/domain/JsonEntityStore.spec.ts +277 -0
- package/src/domain/JsonEntityStore.ts +284 -0
- package/src/domain/JsonFormatTypes.ts +17 -0
- package/src/domain/JsonLazyRef.ts +21 -0
- package/src/domain/JsonMap.spec.ts +15 -0
- package/src/domain/JsonMap.ts +33 -0
- package/src/domain/JsonMedia.ts +24 -0
- package/src/domain/JsonMethodStore.spec.ts +301 -0
- package/src/domain/JsonMethodStore.ts +212 -0
- package/src/domain/JsonOperation.spec.ts +126 -0
- package/src/domain/JsonOperation.ts +219 -0
- package/src/domain/JsonOperationPathsMap.ts +16 -0
- package/src/domain/JsonOperationRoute.spec.ts +109 -0
- package/src/domain/JsonOperationRoute.ts +75 -0
- package/src/domain/JsonParameter.ts +70 -0
- package/src/domain/JsonParameterStore.spec.ts +212 -0
- package/src/domain/JsonParameterStore.ts +138 -0
- package/src/domain/JsonParameterTypes.spec.ts +12 -0
- package/src/domain/JsonParameterTypes.ts +20 -0
- package/src/domain/JsonPropertyStore.spec.ts +177 -0
- package/src/domain/JsonPropertyStore.ts +92 -0
- package/src/domain/JsonRequestBody.ts +47 -0
- package/src/domain/JsonResponse.spec.ts +33 -0
- package/src/domain/JsonResponse.ts +64 -0
- package/src/domain/JsonSchema.spec.ts +1470 -0
- package/src/domain/JsonSchema.ts +1049 -0
- package/src/domain/SpecTypes.ts +6 -0
- package/src/hooks/alterIgnore.ts +25 -0
- package/src/hooks/alterOneOf.ts +67 -0
- package/src/hooks/alterRequiredGroups.ts +12 -0
- package/src/index.ts +185 -0
- package/src/interfaces/IgnoreCallback.ts +5 -0
- package/src/interfaces/JsonHookContext.ts +18 -0
- package/src/interfaces/JsonOpenSpec.ts +7 -0
- package/src/interfaces/JsonSchemaOptions.ts +35 -0
- package/src/registries/JsonSchemaMapperContainer.ts +52 -0
- package/src/registries/enumRegistries.ts +3 -0
- package/src/utils/__snapshots__/generateSpec.spec.ts.snap +109 -0
- package/src/utils/buildPath.ts +8 -0
- package/src/utils/concatParameters.ts +12 -0
- package/src/utils/concatPath.spec.ts +23 -0
- package/src/utils/concatPath.ts +24 -0
- package/src/utils/defineStatusModel.ts +11 -0
- package/src/utils/from.spec.ts +243 -0
- package/src/utils/from.ts +281 -0
- package/src/utils/generateSpec.spec.ts +1262 -0
- package/src/utils/generateSpec.ts +54 -0
- package/src/utils/generics.ts +83 -0
- package/src/utils/getComputedType.spec.ts +45 -0
- package/src/utils/getComputedType.ts +39 -0
- package/src/utils/getInheritedStores.spec.ts +47 -0
- package/src/utils/getInheritedStores.ts +23 -0
- package/src/utils/getJsonEntityStore.ts +36 -0
- package/src/utils/getJsonPathParameters.spec.ts +126 -0
- package/src/utils/getJsonPathParameters.ts +97 -0
- package/src/utils/getJsonSchema.spec.ts +695 -0
- package/src/utils/getJsonSchema.ts +69 -0
- package/src/utils/getJsonType.spec.ts +28 -0
- package/src/utils/getJsonType.ts +46 -0
- package/src/utils/getOperationsRoutes.spec.ts +284 -0
- package/src/utils/getOperationsRoutes.ts +56 -0
- package/src/utils/getOperationsStore.spec.ts +47 -0
- package/src/utils/getOperationsStores.ts +37 -0
- package/src/utils/getPropertiesStores.spec.ts +138 -0
- package/src/utils/getPropertiesStores.ts +66 -0
- package/src/utils/getSpec.spec.ts +610 -0
- package/src/utils/getSpec.ts +117 -0
- package/src/utils/getSpecType.spec.ts +16 -0
- package/src/utils/getSpecType.ts +19 -0
- package/src/utils/isSuccessStatus.spec.ts +22 -0
- package/src/utils/isSuccessStatus.ts +2 -0
- package/src/utils/mapHeaders.spec.ts +57 -0
- package/src/utils/mapHeaders.ts +33 -0
- package/src/utils/mapOpenSpec.ts +44 -0
- package/src/utils/mapOpenSpec2.ts +50 -0
- package/src/utils/mapOpenSpec3.ts +25 -0
- package/src/utils/mapOpenSpecInfo.ts +19 -0
- package/src/utils/mapOperationOptions.ts +34 -0
- package/src/utils/matchGroups.spec.ts +85 -0
- package/src/utils/matchGroups.ts +64 -0
- package/src/utils/mergeSpec.spec.ts +533 -0
- package/src/utils/mergeSpec.ts +54 -0
- package/src/utils/operationIdFormatter.spec.ts +11 -0
- package/src/utils/operationIdFormatter.ts +49 -0
- package/src/utils/ref.ts +65 -0
- package/src/utils/removeHiddenOperation.ts +5 -0
- package/src/utils/serializeEnumValues.ts +27 -0
- package/src/utils/somethingOf.ts +11 -0
- package/src/utils/toJsonMapCollection.spec.ts +18 -0
- package/src/utils/toJsonMapCollection.ts +12 -0
- package/src/utils/toJsonRegex.ts +7 -0
- package/src/utils/transformToOS2.spec.ts +177 -0
- package/src/utils/transformToOS2.ts +607 -0
- package/src/utils/withErrorMsg.spec.ts +16 -0
- package/src/utils/withErrorMsg.ts +56 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {useDecorators} from "@tsed/core";
|
|
2
|
+
|
|
3
|
+
import {ErrorMsg} from "./errorMsg.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Set default error message.
|
|
7
|
+
*
|
|
8
|
+
* ## Example
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* @DefaultMsg("foo should be a string")
|
|
12
|
+
* class Model {
|
|
13
|
+
* property: number;
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* Will produce:
|
|
18
|
+
*
|
|
19
|
+
* ```json
|
|
20
|
+
* {
|
|
21
|
+
* "type": "object",
|
|
22
|
+
* "properties": {
|
|
23
|
+
* "property": "number"
|
|
24
|
+
* }
|
|
25
|
+
* "errorMessage": {
|
|
26
|
+
* "_": "foo should be a string"
|
|
27
|
+
* }
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @decorator
|
|
32
|
+
* @validation
|
|
33
|
+
* @swagger
|
|
34
|
+
* @schema
|
|
35
|
+
*/
|
|
36
|
+
export function DefaultMsg(message: string): ClassDecorator {
|
|
37
|
+
return useDecorators(ErrorMsg({_: message}));
|
|
38
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import {In, OperationPath, Property, SpecTypes} from "../../index.js";
|
|
2
|
+
import {getJsonSchema} from "../../utils/getJsonSchema.js";
|
|
3
|
+
import {getSpec} from "../../utils/getSpec.js";
|
|
4
|
+
import {Description} from "./description.js";
|
|
5
|
+
|
|
6
|
+
describe("@Description", () => {
|
|
7
|
+
it("should declare description on class", () => {
|
|
8
|
+
// WHEN
|
|
9
|
+
@Description("Description")
|
|
10
|
+
class Model {}
|
|
11
|
+
|
|
12
|
+
// THEN
|
|
13
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
14
|
+
description: "Description",
|
|
15
|
+
type: "object"
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
it("should declare description on method", () => {
|
|
19
|
+
// WHEN
|
|
20
|
+
|
|
21
|
+
class Model {
|
|
22
|
+
@OperationPath("GET", "/")
|
|
23
|
+
@Description("Description")
|
|
24
|
+
method() {}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// THEN
|
|
28
|
+
expect(getSpec(Model)).toEqual({
|
|
29
|
+
paths: {
|
|
30
|
+
"/": {
|
|
31
|
+
get: {
|
|
32
|
+
description: "Description",
|
|
33
|
+
operationId: "modelMethod",
|
|
34
|
+
parameters: [],
|
|
35
|
+
responses: {
|
|
36
|
+
"200": {
|
|
37
|
+
description: "Success"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
tags: ["Model"]
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
tags: [{name: "Model"}]
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
it("should declare description on property", () => {
|
|
48
|
+
// WHEN
|
|
49
|
+
|
|
50
|
+
class Model {
|
|
51
|
+
@Description("Description")
|
|
52
|
+
method: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// THEN
|
|
56
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
57
|
+
properties: {
|
|
58
|
+
method: {
|
|
59
|
+
description: "Description",
|
|
60
|
+
type: "string"
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
type: "object"
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
it("should declare description on params (QUERY)", () => {
|
|
67
|
+
// WHEN
|
|
68
|
+
|
|
69
|
+
class Model {
|
|
70
|
+
@OperationPath("GET", "/")
|
|
71
|
+
method(@In("query") @Description("Description") query: string) {}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// THEN
|
|
75
|
+
expect(getSpec(Model, {specType: SpecTypes.OPENAPI})).toEqual({
|
|
76
|
+
paths: {
|
|
77
|
+
"/": {
|
|
78
|
+
get: {
|
|
79
|
+
operationId: "modelMethod",
|
|
80
|
+
parameters: [
|
|
81
|
+
{
|
|
82
|
+
description: "Description",
|
|
83
|
+
in: "query",
|
|
84
|
+
required: false,
|
|
85
|
+
schema: {
|
|
86
|
+
type: "string"
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
],
|
|
90
|
+
responses: {
|
|
91
|
+
"200": {
|
|
92
|
+
description: "Success"
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
tags: ["Model"]
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
tags: [
|
|
100
|
+
{
|
|
101
|
+
name: "Model"
|
|
102
|
+
}
|
|
103
|
+
]
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
it("should declare description on params (BODY)", () => {
|
|
107
|
+
// WHEN
|
|
108
|
+
class MyModel {
|
|
109
|
+
@Property()
|
|
110
|
+
id: string;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
class MyController {
|
|
114
|
+
@OperationPath("POST", "/")
|
|
115
|
+
method(@In("body") @Description("Description") payload: MyModel) {}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const spec = getSpec(MyController, {specType: SpecTypes.OPENAPI});
|
|
119
|
+
|
|
120
|
+
// THEN
|
|
121
|
+
expect(spec).toEqual({
|
|
122
|
+
components: {
|
|
123
|
+
schemas: {
|
|
124
|
+
MyModel: {
|
|
125
|
+
properties: {
|
|
126
|
+
id: {
|
|
127
|
+
type: "string"
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
type: "object"
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
tags: [
|
|
135
|
+
{
|
|
136
|
+
name: "MyController"
|
|
137
|
+
}
|
|
138
|
+
],
|
|
139
|
+
paths: {
|
|
140
|
+
"/": {
|
|
141
|
+
post: {
|
|
142
|
+
operationId: "myControllerMethod",
|
|
143
|
+
parameters: [],
|
|
144
|
+
requestBody: {
|
|
145
|
+
content: {
|
|
146
|
+
"application/json": {
|
|
147
|
+
schema: {
|
|
148
|
+
$ref: "#/components/schemas/MyModel"
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
description: "Description",
|
|
153
|
+
required: false
|
|
154
|
+
},
|
|
155
|
+
responses: {
|
|
156
|
+
"200": {
|
|
157
|
+
description: "Success"
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
tags: ["MyController"]
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import {DecoratorParameters, decoratorTypeOf, DecoratorTypes} from "@tsed/core";
|
|
2
|
+
|
|
3
|
+
import {JsonEntityStore} from "../../domain/JsonEntityStore.js";
|
|
4
|
+
import type {JsonMethodStore} from "../../domain/JsonMethodStore.js";
|
|
5
|
+
import type {JsonParameterStore} from "../../domain/JsonParameterStore.js";
|
|
6
|
+
import {JsonEntityFn} from "./jsonEntityFn.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Add a description to the class, method or property
|
|
10
|
+
*
|
|
11
|
+
* ## Examples
|
|
12
|
+
* ### On class
|
|
13
|
+
*
|
|
14
|
+
* ```typescript
|
|
15
|
+
* @Description("description")
|
|
16
|
+
* class Model {
|
|
17
|
+
*
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* ### On method
|
|
22
|
+
*
|
|
23
|
+
* ```typescript
|
|
24
|
+
* @Controller("/")
|
|
25
|
+
* class ModelCtrl {
|
|
26
|
+
* @Description("description")
|
|
27
|
+
* async method() {}
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* ### On parameter
|
|
32
|
+
*
|
|
33
|
+
* ```typescript
|
|
34
|
+
* @Controller("/")
|
|
35
|
+
* class ModelCtrl {
|
|
36
|
+
* async method(@Description("description") @PathParam("id") id: string) {}
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* ### On property
|
|
41
|
+
*
|
|
42
|
+
* ```typescript
|
|
43
|
+
* class Model {
|
|
44
|
+
* @Description("description")
|
|
45
|
+
* id: string;
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* @param {string} description
|
|
50
|
+
* @decorator
|
|
51
|
+
* @validation
|
|
52
|
+
* @swagger
|
|
53
|
+
* @schema
|
|
54
|
+
* @input
|
|
55
|
+
* @classDecorator
|
|
56
|
+
* @methodDecorator
|
|
57
|
+
* @propertyDecorator
|
|
58
|
+
* @parameterDecorator
|
|
59
|
+
*/
|
|
60
|
+
export function Description(description: any) {
|
|
61
|
+
return JsonEntityFn((store: JsonEntityStore, args: DecoratorParameters) => {
|
|
62
|
+
switch (decoratorTypeOf(args)) {
|
|
63
|
+
case DecoratorTypes.PROP:
|
|
64
|
+
case DecoratorTypes.CLASS:
|
|
65
|
+
store.schema.description(description);
|
|
66
|
+
break;
|
|
67
|
+
|
|
68
|
+
case DecoratorTypes.PARAM:
|
|
69
|
+
(store as JsonParameterStore).parameter.description(description);
|
|
70
|
+
break;
|
|
71
|
+
|
|
72
|
+
case DecoratorTypes.METHOD:
|
|
73
|
+
(store as JsonMethodStore).operation?.description(description);
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import {useDecorators} from "@tsed/core";
|
|
2
|
+
|
|
3
|
+
import {JsonPropertyStore} from "../../domain/JsonPropertyStore.js";
|
|
4
|
+
import {JsonEntityFn} from "./jsonEntityFn.js";
|
|
5
|
+
import {Property} from "./property.js";
|
|
6
|
+
|
|
7
|
+
export function DiscriminatorKey(): PropertyDecorator {
|
|
8
|
+
return useDecorators(
|
|
9
|
+
Property(),
|
|
10
|
+
JsonEntityFn((store: JsonPropertyStore) => {
|
|
11
|
+
store.discriminatorKey();
|
|
12
|
+
})
|
|
13
|
+
);
|
|
14
|
+
}
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import {enums} from "../../utils/from.js";
|
|
2
|
+
import {getJsonSchema} from "../../utils/getJsonSchema.js";
|
|
3
|
+
import {Enum} from "./enum.js";
|
|
4
|
+
|
|
5
|
+
describe("@Enum", () => {
|
|
6
|
+
describe("when enum is a list of values", () => {
|
|
7
|
+
it("should declare prop (uniq type)", () => {
|
|
8
|
+
// WHEN
|
|
9
|
+
class Model {
|
|
10
|
+
@Enum("0", "1")
|
|
11
|
+
num: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
15
|
+
properties: {
|
|
16
|
+
num: {
|
|
17
|
+
enum: ["0", "1"],
|
|
18
|
+
type: "string"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
type: "object"
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
it("should declare prop (mixed type)", () => {
|
|
25
|
+
// WHEN
|
|
26
|
+
class Model {
|
|
27
|
+
@Enum("0", "1", 10)
|
|
28
|
+
num: string | number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
32
|
+
properties: {
|
|
33
|
+
num: {
|
|
34
|
+
anyOf: [
|
|
35
|
+
{
|
|
36
|
+
enum: ["0", "1"],
|
|
37
|
+
type: "string"
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
enum: [10],
|
|
41
|
+
type: "number"
|
|
42
|
+
}
|
|
43
|
+
]
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
type: "object"
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
it("should declare prop (mixed type and null)", () => {
|
|
50
|
+
// WHEN
|
|
51
|
+
class Model {
|
|
52
|
+
@Enum("0", "1", 10, null)
|
|
53
|
+
num: string | number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
57
|
+
properties: {
|
|
58
|
+
num: {
|
|
59
|
+
anyOf: [
|
|
60
|
+
{
|
|
61
|
+
type: "null"
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
enum: ["0", "1"],
|
|
65
|
+
type: "string"
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
enum: [10],
|
|
69
|
+
type: "number"
|
|
70
|
+
}
|
|
71
|
+
]
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
type: "object"
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
it("should declare prop (mixed type, object, and null)", () => {
|
|
78
|
+
// WHEN
|
|
79
|
+
class Model {
|
|
80
|
+
@Enum("0", "1", 10, {test: "test"}, null)
|
|
81
|
+
num: string | number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
85
|
+
properties: {
|
|
86
|
+
num: {
|
|
87
|
+
anyOf: [
|
|
88
|
+
{
|
|
89
|
+
type: "null"
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
enum: ["0", "1", "test"],
|
|
93
|
+
type: "string"
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
enum: [10],
|
|
97
|
+
type: "number"
|
|
98
|
+
}
|
|
99
|
+
]
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
type: "object"
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe("when is a typescript enum (string)", () => {
|
|
108
|
+
it("should declare prop", () => {
|
|
109
|
+
enum SomeEnum {
|
|
110
|
+
ENUM_1 = "enum1",
|
|
111
|
+
ENUM_2 = "enum2"
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// WHEN
|
|
115
|
+
class Model {
|
|
116
|
+
@Enum(SomeEnum)
|
|
117
|
+
num: SomeEnum;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
121
|
+
properties: {
|
|
122
|
+
num: {
|
|
123
|
+
enum: ["enum1", "enum2"],
|
|
124
|
+
type: "string"
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
type: "object"
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
describe("when is a typescript enum (index)", () => {
|
|
133
|
+
it("should declare prop", () => {
|
|
134
|
+
enum SomeEnum {
|
|
135
|
+
ENUM_1,
|
|
136
|
+
ENUM_2
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// WHEN
|
|
140
|
+
class Model {
|
|
141
|
+
@Enum(SomeEnum)
|
|
142
|
+
num: SomeEnum;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
146
|
+
properties: {
|
|
147
|
+
num: {
|
|
148
|
+
enum: [0, 1],
|
|
149
|
+
type: "number"
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
type: "object"
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe("when is a typescript enum (mixed type)", () => {
|
|
158
|
+
it("should declare prop", () => {
|
|
159
|
+
enum SomeEnum {
|
|
160
|
+
ENUM_1,
|
|
161
|
+
ENUM_2 = "test",
|
|
162
|
+
ENUM_3 = "test2"
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// WHEN
|
|
166
|
+
class Model {
|
|
167
|
+
@Enum(SomeEnum)
|
|
168
|
+
num: SomeEnum;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
172
|
+
properties: {
|
|
173
|
+
num: {
|
|
174
|
+
anyOf: [
|
|
175
|
+
{
|
|
176
|
+
enum: [0],
|
|
177
|
+
type: "number"
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
enum: ["test", "test2"],
|
|
181
|
+
type: "string"
|
|
182
|
+
}
|
|
183
|
+
]
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
type: "object"
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
describe("when is a typescript enum with a label (set enum schema)", () => {
|
|
191
|
+
it("should declare prop with a shared enum in definitions", () => {
|
|
192
|
+
enum SomeEnum {
|
|
193
|
+
ENUM_1,
|
|
194
|
+
ENUM_2 = "test",
|
|
195
|
+
ENUM_3 = "test2"
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const enumValues = enums(SomeEnum).label("SomeEnum");
|
|
199
|
+
|
|
200
|
+
// WHEN
|
|
201
|
+
class Model {
|
|
202
|
+
@Enum(enumValues)
|
|
203
|
+
num: SomeEnum;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
207
|
+
definitions: {
|
|
208
|
+
SomeEnum: {
|
|
209
|
+
anyOf: [
|
|
210
|
+
{
|
|
211
|
+
enum: [0],
|
|
212
|
+
type: "number"
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
enum: ["test", "test2"],
|
|
216
|
+
type: "string"
|
|
217
|
+
}
|
|
218
|
+
]
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
properties: {
|
|
222
|
+
num: {
|
|
223
|
+
$ref: "#/definitions/SomeEnum"
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
type: "object"
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
describe("when is a typescript enum with a label (set enum)", () => {
|
|
231
|
+
it("should declare prop with a shared enum in definitions", () => {
|
|
232
|
+
enum SomeEnum {
|
|
233
|
+
ENUM_1,
|
|
234
|
+
ENUM_2 = "test",
|
|
235
|
+
ENUM_3 = "test2"
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
enums(SomeEnum).label("SomeEnum");
|
|
239
|
+
|
|
240
|
+
// WHEN
|
|
241
|
+
class Model {
|
|
242
|
+
@Enum(SomeEnum)
|
|
243
|
+
num: SomeEnum;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
247
|
+
definitions: {
|
|
248
|
+
SomeEnum: {
|
|
249
|
+
anyOf: [
|
|
250
|
+
{
|
|
251
|
+
enum: [0],
|
|
252
|
+
type: "number"
|
|
253
|
+
},
|
|
254
|
+
{
|
|
255
|
+
enum: ["test", "test2"],
|
|
256
|
+
type: "string"
|
|
257
|
+
}
|
|
258
|
+
]
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
properties: {
|
|
262
|
+
num: {
|
|
263
|
+
$ref: "#/definitions/SomeEnum"
|
|
264
|
+
}
|
|
265
|
+
},
|
|
266
|
+
type: "object"
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
describe("when is a typescript enum schema without label (set enum)", () => {
|
|
271
|
+
it("should inline enum", () => {
|
|
272
|
+
enum SomeEnum {
|
|
273
|
+
ENUM_1,
|
|
274
|
+
ENUM_2 = "test",
|
|
275
|
+
ENUM_3 = "test2"
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
enums(SomeEnum);
|
|
279
|
+
|
|
280
|
+
// WHEN
|
|
281
|
+
class Model {
|
|
282
|
+
@Enum(SomeEnum)
|
|
283
|
+
num: SomeEnum;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
287
|
+
properties: {
|
|
288
|
+
num: {
|
|
289
|
+
anyOf: [
|
|
290
|
+
{
|
|
291
|
+
enum: [0],
|
|
292
|
+
type: "number"
|
|
293
|
+
},
|
|
294
|
+
{
|
|
295
|
+
enum: ["test", "test2"],
|
|
296
|
+
type: "string"
|
|
297
|
+
}
|
|
298
|
+
]
|
|
299
|
+
}
|
|
300
|
+
},
|
|
301
|
+
type: "object"
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
});
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type {JSONSchema6Type} from "json-schema";
|
|
2
|
+
|
|
3
|
+
import {withErrorMsg} from "../../utils/withErrorMsg.js";
|
|
4
|
+
import {JsonEntityFn} from "./jsonEntityFn.js";
|
|
5
|
+
|
|
6
|
+
function EnumFn(...enumValues: (JSONSchema6Type | any)[]): Function;
|
|
7
|
+
function EnumFn(enumValue: JSONSchema6Type | any, ...enumValues: JSONSchema6Type[]) {
|
|
8
|
+
return JsonEntityFn((store) => {
|
|
9
|
+
store.itemSchema.enum(enumValue, ...enumValues);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The enum keyword is used to restrict a value to a fixed set of values.
|
|
15
|
+
* It must be an array with at least one element, where each element is unique.
|
|
16
|
+
*
|
|
17
|
+
* Elements in the array might be of any value, including null.
|
|
18
|
+
*
|
|
19
|
+
* ::: warning
|
|
20
|
+
* For v6 user, use @@Enum@@ from @tsed/schema instead of @tsed/platform-http.
|
|
21
|
+
* :::
|
|
22
|
+
*
|
|
23
|
+
* ## Example
|
|
24
|
+
* ### With primitive type
|
|
25
|
+
*
|
|
26
|
+
* ```typescript
|
|
27
|
+
* class Model {
|
|
28
|
+
* @Enum("value1", "value2")
|
|
29
|
+
* property: "value1" | "value2";
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* Will produce:
|
|
34
|
+
*
|
|
35
|
+
* ```json
|
|
36
|
+
* {
|
|
37
|
+
* "type": "object",
|
|
38
|
+
* "properties": {
|
|
39
|
+
* "property": {
|
|
40
|
+
* "type": "string",
|
|
41
|
+
* "enum": ["value1", "value2"]
|
|
42
|
+
* }
|
|
43
|
+
* }
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* ### With array type
|
|
48
|
+
*
|
|
49
|
+
* ```typescript
|
|
50
|
+
* class Model {
|
|
51
|
+
* @Enum("value1", "value2")
|
|
52
|
+
* property: ("value1" | "value2")[];
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* Will produce:
|
|
57
|
+
*
|
|
58
|
+
* ```json
|
|
59
|
+
* {
|
|
60
|
+
* "type": "object",
|
|
61
|
+
* "properties": {
|
|
62
|
+
* "property": {
|
|
63
|
+
* "type": "array",
|
|
64
|
+
* "items": {
|
|
65
|
+
* "type": "string",
|
|
66
|
+
* "enum": ["value1", "value2"]
|
|
67
|
+
* }
|
|
68
|
+
* }
|
|
69
|
+
* }
|
|
70
|
+
* }
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* ### With Typescript Enum
|
|
74
|
+
*
|
|
75
|
+
* ```typescript
|
|
76
|
+
* enum SomeEnum {
|
|
77
|
+
* ENUM_1 = "enum1",
|
|
78
|
+
* ENUM_2 = "enum2"
|
|
79
|
+
* }
|
|
80
|
+
*
|
|
81
|
+
* class Model {
|
|
82
|
+
* @Enum(SomeEnum)
|
|
83
|
+
* property: SomeEnum;
|
|
84
|
+
* }
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* Will produce:
|
|
88
|
+
*
|
|
89
|
+
* ```json
|
|
90
|
+
* {
|
|
91
|
+
* "type": "object",
|
|
92
|
+
* "properties": {
|
|
93
|
+
* "property": {
|
|
94
|
+
* "type": "string",
|
|
95
|
+
* "enum": ["enum1", "enum2"]
|
|
96
|
+
* }
|
|
97
|
+
* }
|
|
98
|
+
* }
|
|
99
|
+
* ```
|
|
100
|
+
*
|
|
101
|
+
* @param {string | number | boolean | {}} enumValue
|
|
102
|
+
* @param enumValues
|
|
103
|
+
* @decorator
|
|
104
|
+
* @validation
|
|
105
|
+
* @swagger
|
|
106
|
+
* @schema
|
|
107
|
+
* @input
|
|
108
|
+
* @ajv-errors
|
|
109
|
+
*/
|
|
110
|
+
|
|
111
|
+
export const Enum = withErrorMsg("enum", EnumFn);
|