@tsed/schema 8.0.0 → 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,276 @@
|
|
|
1
|
+
import {JsonEntityStore} from "../../domain/JsonEntityStore.js";
|
|
2
|
+
import {SpecTypes} from "../../domain/SpecTypes.js";
|
|
3
|
+
import {number, string} from "../../utils/from.js";
|
|
4
|
+
import {getJsonSchema} from "../../utils/getJsonSchema.js";
|
|
5
|
+
import {getSpec} from "../../utils/getSpec.js";
|
|
6
|
+
import {In} from "../operations/in.js";
|
|
7
|
+
import {OperationPath} from "../operations/operationPath.js";
|
|
8
|
+
import {Path} from "../operations/path.js";
|
|
9
|
+
import {AllOf} from "./allOf.js";
|
|
10
|
+
import {Property} from "./property.js";
|
|
11
|
+
|
|
12
|
+
describe("@AllOf", () => {
|
|
13
|
+
it("should declare return schema", () => {
|
|
14
|
+
// WHEN
|
|
15
|
+
class Model {
|
|
16
|
+
@AllOf(string(), number())
|
|
17
|
+
num: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// THEN
|
|
21
|
+
const classSchema = JsonEntityStore.from(Model);
|
|
22
|
+
|
|
23
|
+
expect(classSchema.schema.toJSON()).toEqual({
|
|
24
|
+
properties: {
|
|
25
|
+
num: {
|
|
26
|
+
allOf: [
|
|
27
|
+
{
|
|
28
|
+
type: "string"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
type: "number"
|
|
32
|
+
}
|
|
33
|
+
]
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
type: "object"
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
it("should declare two models", () => {
|
|
40
|
+
class One1 {
|
|
41
|
+
@Property()
|
|
42
|
+
id: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
class One2 {
|
|
46
|
+
@Property()
|
|
47
|
+
id: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
class Model {
|
|
51
|
+
@AllOf(One1, One2)
|
|
52
|
+
test: One1 | One2;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const schema = getJsonSchema(Model);
|
|
56
|
+
|
|
57
|
+
expect(schema).toEqual({
|
|
58
|
+
definitions: {
|
|
59
|
+
One1: {
|
|
60
|
+
type: "object",
|
|
61
|
+
properties: {
|
|
62
|
+
id: {
|
|
63
|
+
type: "string"
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
One2: {
|
|
68
|
+
type: "object",
|
|
69
|
+
properties: {
|
|
70
|
+
id: {
|
|
71
|
+
type: "string"
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
properties: {
|
|
77
|
+
test: {
|
|
78
|
+
allOf: [
|
|
79
|
+
{
|
|
80
|
+
$ref: "#/definitions/One1"
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
$ref: "#/definitions/One2"
|
|
84
|
+
}
|
|
85
|
+
]
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
type: "object"
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
it("should declare two models - OS3", () => {
|
|
92
|
+
class One1 {
|
|
93
|
+
@Property()
|
|
94
|
+
id: string;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
class One2 {
|
|
98
|
+
@Property()
|
|
99
|
+
id: string;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
class Model {
|
|
103
|
+
@AllOf(One1, One2)
|
|
104
|
+
test: One1 | One2;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
@Path("/")
|
|
108
|
+
class MyController {
|
|
109
|
+
@OperationPath("POST", "/")
|
|
110
|
+
get(@In("body") payload: Model) {}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const spec = getSpec(MyController, {specType: SpecTypes.OPENAPI});
|
|
114
|
+
|
|
115
|
+
expect(spec).toEqual({
|
|
116
|
+
components: {
|
|
117
|
+
schemas: {
|
|
118
|
+
Model: {
|
|
119
|
+
properties: {
|
|
120
|
+
test: {
|
|
121
|
+
allOf: [
|
|
122
|
+
{
|
|
123
|
+
$ref: "#/components/schemas/One1"
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
$ref: "#/components/schemas/One2"
|
|
127
|
+
}
|
|
128
|
+
]
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
type: "object"
|
|
132
|
+
},
|
|
133
|
+
One1: {
|
|
134
|
+
properties: {
|
|
135
|
+
id: {
|
|
136
|
+
type: "string"
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
type: "object"
|
|
140
|
+
},
|
|
141
|
+
One2: {
|
|
142
|
+
properties: {
|
|
143
|
+
id: {
|
|
144
|
+
type: "string"
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
type: "object"
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
paths: {
|
|
152
|
+
"/": {
|
|
153
|
+
post: {
|
|
154
|
+
operationId: "myControllerGet",
|
|
155
|
+
parameters: [],
|
|
156
|
+
requestBody: {
|
|
157
|
+
content: {
|
|
158
|
+
"application/json": {
|
|
159
|
+
schema: {
|
|
160
|
+
$ref: "#/components/schemas/Model"
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
required: false
|
|
165
|
+
},
|
|
166
|
+
responses: {
|
|
167
|
+
"200": {
|
|
168
|
+
description: "Success"
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
tags: ["MyController"]
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
tags: [
|
|
176
|
+
{
|
|
177
|
+
name: "MyController"
|
|
178
|
+
}
|
|
179
|
+
]
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
it("should declare two models (array) - OS3", () => {
|
|
183
|
+
class One1 {
|
|
184
|
+
@Property()
|
|
185
|
+
id: string;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
class One2 {
|
|
189
|
+
@Property()
|
|
190
|
+
id: string;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
class Model {
|
|
194
|
+
@AllOf(One1, One2)
|
|
195
|
+
list: (One1 | One2)[];
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
@Path("/")
|
|
199
|
+
class MyController {
|
|
200
|
+
@OperationPath("POST", "/")
|
|
201
|
+
get(@In("body") payload: Model) {}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const spec = getSpec(MyController, {specType: SpecTypes.OPENAPI});
|
|
205
|
+
|
|
206
|
+
expect(spec).toEqual({
|
|
207
|
+
components: {
|
|
208
|
+
schemas: {
|
|
209
|
+
Model: {
|
|
210
|
+
properties: {
|
|
211
|
+
list: {
|
|
212
|
+
type: "array",
|
|
213
|
+
items: {
|
|
214
|
+
allOf: [
|
|
215
|
+
{
|
|
216
|
+
$ref: "#/components/schemas/One1"
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
$ref: "#/components/schemas/One2"
|
|
220
|
+
}
|
|
221
|
+
]
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
},
|
|
225
|
+
type: "object"
|
|
226
|
+
},
|
|
227
|
+
One1: {
|
|
228
|
+
properties: {
|
|
229
|
+
id: {
|
|
230
|
+
type: "string"
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
type: "object"
|
|
234
|
+
},
|
|
235
|
+
One2: {
|
|
236
|
+
properties: {
|
|
237
|
+
id: {
|
|
238
|
+
type: "string"
|
|
239
|
+
}
|
|
240
|
+
},
|
|
241
|
+
type: "object"
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
},
|
|
245
|
+
paths: {
|
|
246
|
+
"/": {
|
|
247
|
+
post: {
|
|
248
|
+
operationId: "myControllerGet",
|
|
249
|
+
parameters: [],
|
|
250
|
+
requestBody: {
|
|
251
|
+
content: {
|
|
252
|
+
"application/json": {
|
|
253
|
+
schema: {
|
|
254
|
+
$ref: "#/components/schemas/Model"
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
required: false
|
|
259
|
+
},
|
|
260
|
+
responses: {
|
|
261
|
+
"200": {
|
|
262
|
+
description: "Success"
|
|
263
|
+
}
|
|
264
|
+
},
|
|
265
|
+
tags: ["MyController"]
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
},
|
|
269
|
+
tags: [
|
|
270
|
+
{
|
|
271
|
+
name: "MyController"
|
|
272
|
+
}
|
|
273
|
+
]
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {AnyJsonSchema} from "../../domain/JsonSchema.js";
|
|
2
|
+
import {JsonEntityFn} from "./jsonEntityFn.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* See https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.26
|
|
6
|
+
*
|
|
7
|
+
* @decorator
|
|
8
|
+
* @validation
|
|
9
|
+
* @swagger
|
|
10
|
+
* @schema
|
|
11
|
+
* @classDecorator
|
|
12
|
+
* @input
|
|
13
|
+
* @param allOf
|
|
14
|
+
*/
|
|
15
|
+
export function AllOf(...allOf: AnyJsonSchema[]) {
|
|
16
|
+
return JsonEntityFn((entity) => {
|
|
17
|
+
entity.itemSchema.allOf(allOf);
|
|
18
|
+
});
|
|
19
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import {Ajv} from "ajv";
|
|
2
|
+
|
|
3
|
+
import {getJsonSchema} from "../../index.js";
|
|
4
|
+
import {Allow} from "./allow.js";
|
|
5
|
+
import {Property} from "./property.js";
|
|
6
|
+
import {Required} from "./required.js";
|
|
7
|
+
|
|
8
|
+
describe("@Allow", () => {
|
|
9
|
+
it("should declare required and allow field (without Allow)", () => {
|
|
10
|
+
// WHEN
|
|
11
|
+
class Model {
|
|
12
|
+
@Required()
|
|
13
|
+
allow: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// THEN
|
|
17
|
+
const classSchema = getJsonSchema(Model);
|
|
18
|
+
|
|
19
|
+
expect(classSchema).toEqual({
|
|
20
|
+
properties: {
|
|
21
|
+
allow: {
|
|
22
|
+
type: "string",
|
|
23
|
+
minLength: 1
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
required: ["allow"],
|
|
27
|
+
type: "object"
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const validate = new Ajv().compile(classSchema);
|
|
31
|
+
expect(validate({allow: ""})).toBe(false);
|
|
32
|
+
expect(validate({allow: 0})).toBe(false);
|
|
33
|
+
expect(validate({})).toBe(false);
|
|
34
|
+
});
|
|
35
|
+
it("should declare required and allow field (with Allowed empty string)", () => {
|
|
36
|
+
// WHEN
|
|
37
|
+
class Model {
|
|
38
|
+
@Allow("")
|
|
39
|
+
allow: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// THEN
|
|
43
|
+
const classSchema = getJsonSchema(Model);
|
|
44
|
+
|
|
45
|
+
expect(classSchema).toEqual({
|
|
46
|
+
properties: {
|
|
47
|
+
allow: {
|
|
48
|
+
type: "string"
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
required: ["allow"],
|
|
52
|
+
type: "object"
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const validate = new Ajv().compile(classSchema);
|
|
56
|
+
expect(validate({allow: ""})).toBe(true);
|
|
57
|
+
expect(validate({})).toBe(false);
|
|
58
|
+
});
|
|
59
|
+
it("should declare required and allow field (with Allowed null basic type)", () => {
|
|
60
|
+
// WHEN
|
|
61
|
+
class Model {
|
|
62
|
+
@Allow(null)
|
|
63
|
+
allow: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// THEN
|
|
67
|
+
const classSchema = getJsonSchema(Model);
|
|
68
|
+
|
|
69
|
+
expect(classSchema).toEqual({
|
|
70
|
+
properties: {
|
|
71
|
+
allow: {
|
|
72
|
+
minLength: 1,
|
|
73
|
+
type: ["null", "string"]
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
required: ["allow"],
|
|
77
|
+
type: "object"
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const validate = new Ajv().compile(classSchema);
|
|
81
|
+
expect(validate({allow: null})).toBe(true);
|
|
82
|
+
expect(validate({})).toBe(false);
|
|
83
|
+
});
|
|
84
|
+
it("should declare required and allow field (with Allowed null NestedModel)", () => {
|
|
85
|
+
// WHEN
|
|
86
|
+
class NestedModel {
|
|
87
|
+
@Property()
|
|
88
|
+
prop: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
class Model {
|
|
92
|
+
@Allow(null, NestedModel)
|
|
93
|
+
allow: NestedModel | null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// THEN
|
|
97
|
+
const spec = getJsonSchema(Model);
|
|
98
|
+
|
|
99
|
+
expect(spec).toEqual({
|
|
100
|
+
definitions: {
|
|
101
|
+
NestedModel: {
|
|
102
|
+
properties: {
|
|
103
|
+
prop: {
|
|
104
|
+
type: "string"
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
type: "object"
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
properties: {
|
|
111
|
+
allow: {
|
|
112
|
+
anyOf: [
|
|
113
|
+
{
|
|
114
|
+
type: "null"
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
$ref: "#/definitions/NestedModel"
|
|
118
|
+
}
|
|
119
|
+
]
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
required: ["allow"],
|
|
123
|
+
type: "object"
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const validate = new Ajv().compile(spec);
|
|
127
|
+
expect(validate({allow: null})).toBe(true);
|
|
128
|
+
expect(validate({})).toBe(false);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import {DecoratorTypes, isClass, useDecorators} from "@tsed/core";
|
|
2
|
+
|
|
3
|
+
import type {JsonParameterStore} from "../../domain/JsonParameterStore.js";
|
|
4
|
+
import {JsonEntityFn} from "./jsonEntityFn.js";
|
|
5
|
+
import {Property} from "./property.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Add allowed values when the property or parameters is required.
|
|
9
|
+
*
|
|
10
|
+
* #### Example on parameter:
|
|
11
|
+
*
|
|
12
|
+
* ```typescript
|
|
13
|
+
* @Post("/")
|
|
14
|
+
* async method(@Allow("") @BodyParams("field") field: string) {}
|
|
15
|
+
* ```
|
|
16
|
+
* > Required will throw a BadRequest when the given value is `null` or `undefined` but not for an empty string.
|
|
17
|
+
*
|
|
18
|
+
* #### Example on model:
|
|
19
|
+
*
|
|
20
|
+
* ```typescript
|
|
21
|
+
* class Model {
|
|
22
|
+
* @Allow("")
|
|
23
|
+
* field: string;
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @returns {Function}
|
|
28
|
+
* @decorator
|
|
29
|
+
* @validation
|
|
30
|
+
* @swagger
|
|
31
|
+
* @schema
|
|
32
|
+
* @input
|
|
33
|
+
*/
|
|
34
|
+
export function Allow(...values: any[]) {
|
|
35
|
+
const model = values.find((item) => isClass(item));
|
|
36
|
+
return useDecorators(
|
|
37
|
+
model && Property(model),
|
|
38
|
+
JsonEntityFn((store, args) => {
|
|
39
|
+
if (store.decoratorType === DecoratorTypes.PARAM) {
|
|
40
|
+
(store as JsonParameterStore).required = true;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (store.decoratorType === DecoratorTypes.PROP) {
|
|
44
|
+
store.parentSchema.addRequired(store.propertyName);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
store.schema.allow(...values);
|
|
48
|
+
})
|
|
49
|
+
);
|
|
50
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import {getJsonSchema} from "../../utils/getJsonSchema.js";
|
|
2
|
+
import {Any} from "./any.js";
|
|
3
|
+
|
|
4
|
+
describe("@Any", () => {
|
|
5
|
+
it("should declare any prop", () => {
|
|
6
|
+
// WHEN
|
|
7
|
+
class Model {
|
|
8
|
+
@Any()
|
|
9
|
+
prop: any;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// THEN
|
|
13
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
14
|
+
properties: {
|
|
15
|
+
prop: {
|
|
16
|
+
anyOf: [
|
|
17
|
+
{
|
|
18
|
+
type: "null"
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
multipleOf: 1,
|
|
22
|
+
type: "integer"
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
type: "number"
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
type: "string"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
type: "boolean"
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
type: "array"
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
type: "object"
|
|
38
|
+
}
|
|
39
|
+
]
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
type: "object"
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
it("should declare any prop (uniq type)", () => {
|
|
46
|
+
// WHEN
|
|
47
|
+
class Model {
|
|
48
|
+
@Any(String)
|
|
49
|
+
prop: any;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// THEN
|
|
53
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
54
|
+
properties: {
|
|
55
|
+
prop: {
|
|
56
|
+
type: "string"
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
type: "object"
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
it("should declare any prop (with list)", () => {
|
|
63
|
+
// WHEN
|
|
64
|
+
class Model {
|
|
65
|
+
@Any(String, Number, Boolean, null)
|
|
66
|
+
prop: any;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// THEN
|
|
70
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
71
|
+
properties: {
|
|
72
|
+
prop: {
|
|
73
|
+
anyOf: [
|
|
74
|
+
{
|
|
75
|
+
type: "null"
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
type: "string"
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
type: "number"
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
type: "boolean"
|
|
85
|
+
}
|
|
86
|
+
]
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
type: "object"
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
it("should declare any prop (with string, list)", () => {
|
|
93
|
+
// WHEN
|
|
94
|
+
class Model {
|
|
95
|
+
@Any("string", "null")
|
|
96
|
+
prop: any;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// THEN
|
|
100
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
101
|
+
properties: {
|
|
102
|
+
prop: {
|
|
103
|
+
type: ["null", "string"]
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
type: "object"
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import {Type} from "@tsed/core";
|
|
2
|
+
import type {JSONSchema6TypeName} from "json-schema";
|
|
3
|
+
|
|
4
|
+
import {JsonEntityFn} from "./jsonEntityFn.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Set the type of the array items.
|
|
8
|
+
*
|
|
9
|
+
* ## Example
|
|
10
|
+
*
|
|
11
|
+
* ```typescript
|
|
12
|
+
* class Model {
|
|
13
|
+
* @Any()
|
|
14
|
+
* property: any;
|
|
15
|
+
*
|
|
16
|
+
* @Any(String, Number, Boolean)
|
|
17
|
+
* property: string | number | boolean;
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* Will produce:
|
|
22
|
+
*
|
|
23
|
+
* ```json
|
|
24
|
+
* {
|
|
25
|
+
* "type": "object",
|
|
26
|
+
* "properties": {
|
|
27
|
+
* "property": {
|
|
28
|
+
* "type": ["integer", "number", "string", "boolean", "array", "object", "null"]
|
|
29
|
+
* }
|
|
30
|
+
* }
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @returns {Function}
|
|
35
|
+
* @decorator
|
|
36
|
+
* @validation
|
|
37
|
+
* @swagger
|
|
38
|
+
* @schema
|
|
39
|
+
* @input
|
|
40
|
+
*/
|
|
41
|
+
export function Any(...types: (JSONSchema6TypeName | Type<any> | any)[]) {
|
|
42
|
+
return JsonEntityFn((store) => {
|
|
43
|
+
store.itemSchema.any(...types);
|
|
44
|
+
});
|
|
45
|
+
}
|