@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.
Files changed (291) hide show
  1. package/package.json +11 -10
  2. package/src/components/async-api/channelsMapper.ts +81 -0
  3. package/src/components/async-api/generate.ts +24 -0
  4. package/src/components/async-api/messageMapper.ts +62 -0
  5. package/src/components/async-api/payloadMapper.ts +61 -0
  6. package/src/components/async-api/responseMapper.ts +70 -0
  7. package/src/components/default/anyMapper.spec.ts +7 -0
  8. package/src/components/default/anyMapper.ts +33 -0
  9. package/src/components/default/classMapper.ts +55 -0
  10. package/src/components/default/discriminatorMappingMapper.ts +36 -0
  11. package/src/components/default/enumsMapper.spec.ts +64 -0
  12. package/src/components/default/enumsMapper.ts +17 -0
  13. package/src/components/default/genericsMapper.ts +70 -0
  14. package/src/components/default/inheritedClassMapper.ts +24 -0
  15. package/src/components/default/itemMapper.ts +8 -0
  16. package/src/components/default/lazyRefMapper.ts +21 -0
  17. package/src/components/default/mapMapper.ts +25 -0
  18. package/src/components/default/nullableMapper.ts +55 -0
  19. package/src/components/default/objectMapper.ts +36 -0
  20. package/src/components/default/ofMapper.ts +16 -0
  21. package/src/components/default/propertiesMapper.ts +25 -0
  22. package/src/components/default/requiredMapper.ts +69 -0
  23. package/src/components/default/schemaMapper.ts +151 -0
  24. package/src/components/index.ts +36 -0
  25. package/src/components/open-spec/discriminatorMappingMapper.ts +7 -0
  26. package/src/components/open-spec/enumsMapper.ts +20 -0
  27. package/src/components/open-spec/generate.ts +29 -0
  28. package/src/components/open-spec/nullableMapper.ts +51 -0
  29. package/src/components/open-spec/operationInFilesMapper.ts +32 -0
  30. package/src/components/open-spec/operationInParameterMapper.ts +56 -0
  31. package/src/components/open-spec/operationInParametersMapper.ts +9 -0
  32. package/src/components/open-spec/operationInQueryMapper.ts +74 -0
  33. package/src/components/open-spec/operationMapper.ts +65 -0
  34. package/src/components/open-spec/operationMediaMapper.ts +11 -0
  35. package/src/components/open-spec/operationRequestBodyMapper.ts +82 -0
  36. package/src/components/open-spec/operationResponseMapper.ts +26 -0
  37. package/src/components/open-spec/pathsMapper.ts +132 -0
  38. package/src/constants/OperationVerbs.ts +29 -0
  39. package/src/constants/httpStatusMessages.ts +21 -0
  40. package/src/constants/jsonSchemaProperties.ts +17 -0
  41. package/src/decorators/class/children.ts +22 -0
  42. package/src/decorators/class/discriminatorValue.ts +53 -0
  43. package/src/decorators/collections/collectionContains.ts +25 -0
  44. package/src/decorators/collections/collectionOf.spec.ts +327 -0
  45. package/src/decorators/collections/collectionOf.ts +182 -0
  46. package/src/decorators/collections/maxItems.spec.ts +40 -0
  47. package/src/decorators/collections/maxItems.ts +59 -0
  48. package/src/decorators/collections/maxProperties.spec.ts +109 -0
  49. package/src/decorators/collections/maxProperties.ts +82 -0
  50. package/src/decorators/collections/minItems.spec.ts +40 -0
  51. package/src/decorators/collections/minItems.ts +63 -0
  52. package/src/decorators/collections/minProperties.spec.ts +109 -0
  53. package/src/decorators/collections/minProperties.ts +81 -0
  54. package/src/decorators/collections/uniqueItems.spec.ts +28 -0
  55. package/src/decorators/collections/uniqueItems.ts +59 -0
  56. package/src/decorators/common/__snapshots__/groups.spec.ts.snap +321 -0
  57. package/src/decorators/common/additionalProperties.spec.ts +133 -0
  58. package/src/decorators/common/additionalProperties.ts +17 -0
  59. package/src/decorators/common/allOf.spec.ts +276 -0
  60. package/src/decorators/common/allOf.ts +19 -0
  61. package/src/decorators/common/allow.spec.ts +130 -0
  62. package/src/decorators/common/allow.ts +50 -0
  63. package/src/decorators/common/any.spec.ts +109 -0
  64. package/src/decorators/common/any.ts +45 -0
  65. package/src/decorators/common/anyOf.spec.ts +270 -0
  66. package/src/decorators/common/anyOf.ts +23 -0
  67. package/src/decorators/common/const.spec.ts +98 -0
  68. package/src/decorators/common/const.ts +68 -0
  69. package/src/decorators/common/customKey.spec.ts +105 -0
  70. package/src/decorators/common/customKey.ts +39 -0
  71. package/src/decorators/common/default.spec.ts +25 -0
  72. package/src/decorators/common/default.ts +47 -0
  73. package/src/decorators/common/defaultMsg.spec.ts +22 -0
  74. package/src/decorators/common/defaultMsg.ts +38 -0
  75. package/src/decorators/common/description.spec.ts +166 -0
  76. package/src/decorators/common/description.ts +77 -0
  77. package/src/decorators/common/discriminatorKey.ts +14 -0
  78. package/src/decorators/common/enum.spec.ts +305 -0
  79. package/src/decorators/common/enum.ts +111 -0
  80. package/src/decorators/common/errorMsg.spec.ts +57 -0
  81. package/src/decorators/common/errorMsg.ts +14 -0
  82. package/src/decorators/common/example.spec.ts +93 -0
  83. package/src/decorators/common/example.ts +18 -0
  84. package/src/decorators/common/examples.spec.ts +101 -0
  85. package/src/decorators/common/examples.ts +20 -0
  86. package/src/decorators/common/exclusiveMaximum.spec.ts +47 -0
  87. package/src/decorators/common/exclusiveMaximum.ts +75 -0
  88. package/src/decorators/common/exclusiveMinimum.spec.ts +47 -0
  89. package/src/decorators/common/exclusiveMinimum.ts +75 -0
  90. package/src/decorators/common/format.spec.ts +149 -0
  91. package/src/decorators/common/format.ts +486 -0
  92. package/src/decorators/common/forwardGroups.spec.ts +270 -0
  93. package/src/decorators/common/forwardGroups.ts +16 -0
  94. package/src/decorators/common/groups.spec.ts +968 -0
  95. package/src/decorators/common/groups.ts +66 -0
  96. package/src/decorators/common/hidden.spec.ts +52 -0
  97. package/src/decorators/common/hidden.ts +36 -0
  98. package/src/decorators/common/ignore.spec.ts +88 -0
  99. package/src/decorators/common/ignore.ts +66 -0
  100. package/src/decorators/common/integer.spec.ts +110 -0
  101. package/src/decorators/common/integer.ts +69 -0
  102. package/src/decorators/common/jsonEntityFn.spec.ts +16 -0
  103. package/src/decorators/common/jsonEntityFn.ts +33 -0
  104. package/src/decorators/common/maxLength.spec.ts +61 -0
  105. package/src/decorators/common/maxLength.ts +88 -0
  106. package/src/decorators/common/maximum.spec.ts +73 -0
  107. package/src/decorators/common/maximum.ts +146 -0
  108. package/src/decorators/common/minLength.spec.ts +186 -0
  109. package/src/decorators/common/minLength.ts +89 -0
  110. package/src/decorators/common/minimum.spec.ts +74 -0
  111. package/src/decorators/common/minimum.ts +145 -0
  112. package/src/decorators/common/multipleOf.spec.ts +33 -0
  113. package/src/decorators/common/multipleOf.ts +80 -0
  114. package/src/decorators/common/name.spec.ts +56 -0
  115. package/src/decorators/common/name.ts +50 -0
  116. package/src/decorators/common/nullable.spec.ts +818 -0
  117. package/src/decorators/common/nullable.ts +32 -0
  118. package/src/decorators/common/oneOf.spec.ts +301 -0
  119. package/src/decorators/common/oneOf.ts +24 -0
  120. package/src/decorators/common/optional.spec.ts +43 -0
  121. package/src/decorators/common/optional.ts +26 -0
  122. package/src/decorators/common/pattern.spec.ts +27 -0
  123. package/src/decorators/common/pattern.ts +102 -0
  124. package/src/decorators/common/property.ts +24 -0
  125. package/src/decorators/common/readOnly.spec.ts +135 -0
  126. package/src/decorators/common/readOnly.ts +17 -0
  127. package/src/decorators/common/recordOf.spec.ts +41 -0
  128. package/src/decorators/common/recordOf.ts +59 -0
  129. package/src/decorators/common/required.spec.ts +188 -0
  130. package/src/decorators/common/required.ts +50 -0
  131. package/src/decorators/common/requiredGroups.spec.ts +127 -0
  132. package/src/decorators/common/requiredGroups.ts +23 -0
  133. package/src/decorators/common/schema.spec.ts +23 -0
  134. package/src/decorators/common/schema.ts +64 -0
  135. package/src/decorators/common/title.spec.ts +20 -0
  136. package/src/decorators/common/title.ts +45 -0
  137. package/src/decorators/common/typeError.spec.ts +22 -0
  138. package/src/decorators/common/typeError.ts +15 -0
  139. package/src/decorators/common/writeOnly.spec.ts +105 -0
  140. package/src/decorators/common/writeOnly.ts +17 -0
  141. package/src/decorators/config/jsonEntityComponent.ts +16 -0
  142. package/src/decorators/generics/genericOf.spec.ts +246 -0
  143. package/src/decorators/generics/genericOf.ts +112 -0
  144. package/src/decorators/generics/generics.ts +22 -0
  145. package/src/decorators/operations/acceptMime.spec.ts +39 -0
  146. package/src/decorators/operations/acceptMime.ts +25 -0
  147. package/src/decorators/operations/consumes.spec.ts +89 -0
  148. package/src/decorators/operations/consumes.ts +37 -0
  149. package/src/decorators/operations/contentType.spec.ts +40 -0
  150. package/src/decorators/operations/contentType.ts +24 -0
  151. package/src/decorators/operations/deprecated.spec.ts +223 -0
  152. package/src/decorators/operations/deprecated.ts +49 -0
  153. package/src/decorators/operations/header.spec.ts +214 -0
  154. package/src/decorators/operations/header.ts +62 -0
  155. package/src/decorators/operations/in.spec.ts +321 -0
  156. package/src/decorators/operations/in.ts +141 -0
  157. package/src/decorators/operations/inFile.spec.ts +209 -0
  158. package/src/decorators/operations/inFile.ts +55 -0
  159. package/src/decorators/operations/location.spec.ts +51 -0
  160. package/src/decorators/operations/location.ts +26 -0
  161. package/src/decorators/operations/operation.ts +119 -0
  162. package/src/decorators/operations/operationId.spec.ts +43 -0
  163. package/src/decorators/operations/operationId.ts +22 -0
  164. package/src/decorators/operations/operationPath.spec.ts +42 -0
  165. package/src/decorators/operations/operationPath.ts +33 -0
  166. package/src/decorators/operations/partial.spec.ts +249 -0
  167. package/src/decorators/operations/partial.ts +14 -0
  168. package/src/decorators/operations/path.spec.ts +47 -0
  169. package/src/decorators/operations/path.ts +27 -0
  170. package/src/decorators/operations/produces.spec.ts +89 -0
  171. package/src/decorators/operations/produces.ts +44 -0
  172. package/src/decorators/operations/publish.spec.ts +26 -0
  173. package/src/decorators/operations/publish.ts +6 -0
  174. package/src/decorators/operations/redirect.spec.ts +146 -0
  175. package/src/decorators/operations/redirect.ts +82 -0
  176. package/src/decorators/operations/returns.spec.ts +1393 -0
  177. package/src/decorators/operations/returns.ts +662 -0
  178. package/src/decorators/operations/route.spec.ts +211 -0
  179. package/src/decorators/operations/route.ts +154 -0
  180. package/src/decorators/operations/security.spec.ts +162 -0
  181. package/src/decorators/operations/security.ts +83 -0
  182. package/src/decorators/operations/status.spec.ts +582 -0
  183. package/src/decorators/operations/status.ts +130 -0
  184. package/src/decorators/operations/subscribe.spec.ts +32 -0
  185. package/src/decorators/operations/subscribe.ts +6 -0
  186. package/src/decorators/operations/summary.spec.ts +45 -0
  187. package/src/decorators/operations/summary.ts +32 -0
  188. package/src/decorators/operations/tags.spec.ts +113 -0
  189. package/src/decorators/operations/tags.ts +53 -0
  190. package/src/decorators/operations/view.spec.ts +16 -0
  191. package/src/decorators/operations/view.ts +27 -0
  192. package/src/domain/DecoratorContext.ts +116 -0
  193. package/src/domain/JsonAliasMap.ts +19 -0
  194. package/src/domain/JsonClassStore.ts +35 -0
  195. package/src/domain/JsonDiscriminator.spec.ts +65 -0
  196. package/src/domain/JsonDiscriminator.ts +52 -0
  197. package/src/domain/JsonEntityStore.spec.ts +277 -0
  198. package/src/domain/JsonEntityStore.ts +284 -0
  199. package/src/domain/JsonFormatTypes.ts +17 -0
  200. package/src/domain/JsonLazyRef.ts +21 -0
  201. package/src/domain/JsonMap.spec.ts +15 -0
  202. package/src/domain/JsonMap.ts +33 -0
  203. package/src/domain/JsonMedia.ts +24 -0
  204. package/src/domain/JsonMethodStore.spec.ts +301 -0
  205. package/src/domain/JsonMethodStore.ts +212 -0
  206. package/src/domain/JsonOperation.spec.ts +126 -0
  207. package/src/domain/JsonOperation.ts +219 -0
  208. package/src/domain/JsonOperationPathsMap.ts +16 -0
  209. package/src/domain/JsonOperationRoute.spec.ts +109 -0
  210. package/src/domain/JsonOperationRoute.ts +75 -0
  211. package/src/domain/JsonParameter.ts +70 -0
  212. package/src/domain/JsonParameterStore.spec.ts +212 -0
  213. package/src/domain/JsonParameterStore.ts +138 -0
  214. package/src/domain/JsonParameterTypes.spec.ts +12 -0
  215. package/src/domain/JsonParameterTypes.ts +20 -0
  216. package/src/domain/JsonPropertyStore.spec.ts +177 -0
  217. package/src/domain/JsonPropertyStore.ts +92 -0
  218. package/src/domain/JsonRequestBody.ts +47 -0
  219. package/src/domain/JsonResponse.spec.ts +33 -0
  220. package/src/domain/JsonResponse.ts +64 -0
  221. package/src/domain/JsonSchema.spec.ts +1470 -0
  222. package/src/domain/JsonSchema.ts +1049 -0
  223. package/src/domain/SpecTypes.ts +6 -0
  224. package/src/hooks/alterIgnore.ts +25 -0
  225. package/src/hooks/alterOneOf.ts +67 -0
  226. package/src/hooks/alterRequiredGroups.ts +12 -0
  227. package/src/index.ts +185 -0
  228. package/src/interfaces/IgnoreCallback.ts +5 -0
  229. package/src/interfaces/JsonHookContext.ts +18 -0
  230. package/src/interfaces/JsonOpenSpec.ts +7 -0
  231. package/src/interfaces/JsonSchemaOptions.ts +35 -0
  232. package/src/registries/JsonSchemaMapperContainer.ts +52 -0
  233. package/src/registries/enumRegistries.ts +3 -0
  234. package/src/utils/__snapshots__/generateSpec.spec.ts.snap +109 -0
  235. package/src/utils/buildPath.ts +8 -0
  236. package/src/utils/concatParameters.ts +12 -0
  237. package/src/utils/concatPath.spec.ts +23 -0
  238. package/src/utils/concatPath.ts +24 -0
  239. package/src/utils/defineStatusModel.ts +11 -0
  240. package/src/utils/from.spec.ts +243 -0
  241. package/src/utils/from.ts +281 -0
  242. package/src/utils/generateSpec.spec.ts +1262 -0
  243. package/src/utils/generateSpec.ts +54 -0
  244. package/src/utils/generics.ts +83 -0
  245. package/src/utils/getComputedType.spec.ts +45 -0
  246. package/src/utils/getComputedType.ts +39 -0
  247. package/src/utils/getInheritedStores.spec.ts +47 -0
  248. package/src/utils/getInheritedStores.ts +23 -0
  249. package/src/utils/getJsonEntityStore.ts +36 -0
  250. package/src/utils/getJsonPathParameters.spec.ts +126 -0
  251. package/src/utils/getJsonPathParameters.ts +97 -0
  252. package/src/utils/getJsonSchema.spec.ts +695 -0
  253. package/src/utils/getJsonSchema.ts +69 -0
  254. package/src/utils/getJsonType.spec.ts +28 -0
  255. package/src/utils/getJsonType.ts +46 -0
  256. package/src/utils/getOperationsRoutes.spec.ts +284 -0
  257. package/src/utils/getOperationsRoutes.ts +56 -0
  258. package/src/utils/getOperationsStore.spec.ts +47 -0
  259. package/src/utils/getOperationsStores.ts +37 -0
  260. package/src/utils/getPropertiesStores.spec.ts +138 -0
  261. package/src/utils/getPropertiesStores.ts +66 -0
  262. package/src/utils/getSpec.spec.ts +610 -0
  263. package/src/utils/getSpec.ts +117 -0
  264. package/src/utils/getSpecType.spec.ts +16 -0
  265. package/src/utils/getSpecType.ts +19 -0
  266. package/src/utils/isSuccessStatus.spec.ts +22 -0
  267. package/src/utils/isSuccessStatus.ts +2 -0
  268. package/src/utils/mapHeaders.spec.ts +57 -0
  269. package/src/utils/mapHeaders.ts +33 -0
  270. package/src/utils/mapOpenSpec.ts +44 -0
  271. package/src/utils/mapOpenSpec2.ts +50 -0
  272. package/src/utils/mapOpenSpec3.ts +25 -0
  273. package/src/utils/mapOpenSpecInfo.ts +19 -0
  274. package/src/utils/mapOperationOptions.ts +34 -0
  275. package/src/utils/matchGroups.spec.ts +85 -0
  276. package/src/utils/matchGroups.ts +64 -0
  277. package/src/utils/mergeSpec.spec.ts +533 -0
  278. package/src/utils/mergeSpec.ts +54 -0
  279. package/src/utils/operationIdFormatter.spec.ts +11 -0
  280. package/src/utils/operationIdFormatter.ts +49 -0
  281. package/src/utils/ref.ts +65 -0
  282. package/src/utils/removeHiddenOperation.ts +5 -0
  283. package/src/utils/serializeEnumValues.ts +27 -0
  284. package/src/utils/somethingOf.ts +11 -0
  285. package/src/utils/toJsonMapCollection.spec.ts +18 -0
  286. package/src/utils/toJsonMapCollection.ts +12 -0
  287. package/src/utils/toJsonRegex.ts +7 -0
  288. package/src/utils/transformToOS2.spec.ts +177 -0
  289. package/src/utils/transformToOS2.ts +607 -0
  290. package/src/utils/withErrorMsg.spec.ts +16 -0
  291. package/src/utils/withErrorMsg.ts +56 -0
@@ -0,0 +1,818 @@
1
+ import {BodyParams} from "@tsed/platform-params";
2
+ import {Ajv} from "ajv";
3
+
4
+ import {SpecTypes} from "../../domain/SpecTypes.js";
5
+ import {getJsonSchema} from "../../utils/getJsonSchema.js";
6
+ import {getSpec} from "../../utils/getSpec.js";
7
+ import {In} from "../operations/in.js";
8
+ import {Path} from "../operations/path.js";
9
+ import {Post} from "../operations/route.js";
10
+ import {Format} from "./format.js";
11
+ import {Integer} from "./integer.js";
12
+ import {MaxLength} from "./maxLength.js";
13
+ import {Minimum} from "./minimum.js";
14
+ import {Nullable} from "./nullable.js";
15
+ import {Property} from "./property.js";
16
+ import {Required} from "./required.js";
17
+
18
+ describe("@Nullable", () => {
19
+ it("should declare any prop (Required + Nullable)", () => {
20
+ // WHEN
21
+ class Model {
22
+ @Required(true, null) // allow null
23
+ @Nullable(String)
24
+ prop2: string | null;
25
+ }
26
+
27
+ // THEN
28
+ const schema = getJsonSchema(Model);
29
+
30
+ expect(schema).toEqual({
31
+ properties: {
32
+ prop2: {
33
+ type: ["null", "string"],
34
+ minLength: 1
35
+ }
36
+ },
37
+ required: ["prop2"],
38
+ type: "object"
39
+ });
40
+
41
+ const ajv = new Ajv({strict: true});
42
+
43
+ expect(ajv.validate(schema, {prop2: null})).toBeTruthy();
44
+ expect(ajv.validate(schema, {prop2: "test"})).toBeTruthy();
45
+ expect(ajv.validate(schema, {prop2: 1})).toBeFalsy();
46
+ expect(ajv.validate(schema, {prop2: ""})).toBeFalsy();
47
+
48
+ @Path("/")
49
+ class Test {
50
+ @Post("/")
51
+ test(@BodyParams() model: Model) {}
52
+ }
53
+
54
+ expect(getSpec(Test, {specType: SpecTypes.OPENAPI})).toEqual({
55
+ components: {
56
+ schemas: {
57
+ Model: {
58
+ properties: {
59
+ prop2: {
60
+ minLength: 1,
61
+ type: "string",
62
+ nullable: true
63
+ }
64
+ },
65
+ required: ["prop2"],
66
+ type: "object"
67
+ }
68
+ }
69
+ },
70
+ paths: {
71
+ "/": {
72
+ post: {
73
+ operationId: "testTest",
74
+ parameters: [],
75
+ requestBody: {
76
+ content: {
77
+ "application/json": {
78
+ schema: {
79
+ $ref: "#/components/schemas/Model"
80
+ }
81
+ }
82
+ },
83
+ required: false
84
+ },
85
+ responses: {
86
+ "200": {
87
+ description: "Success"
88
+ }
89
+ },
90
+ tags: ["Test"]
91
+ }
92
+ }
93
+ },
94
+ tags: [
95
+ {
96
+ name: "Test"
97
+ }
98
+ ]
99
+ });
100
+ });
101
+ it("should declare any prop (String + Required + Nullable)", () => {
102
+ // WHEN
103
+ class Model {
104
+ @Required(true, null, "") // allow null
105
+ @Nullable(String)
106
+ prop2: string | null;
107
+ }
108
+
109
+ // THEN
110
+ const schema = getJsonSchema(Model);
111
+ const ajv = new Ajv();
112
+
113
+ ajv.compile(schema);
114
+
115
+ expect(schema).toEqual({
116
+ properties: {
117
+ prop2: {
118
+ type: ["null", "string"]
119
+ }
120
+ },
121
+ required: ["prop2"],
122
+ type: "object"
123
+ });
124
+
125
+ @Path("/")
126
+ class Test {
127
+ @Post("/")
128
+ test(@BodyParams() model: Model) {}
129
+ }
130
+
131
+ expect(getSpec(Test, {specType: SpecTypes.OPENAPI})).toEqual({
132
+ components: {
133
+ schemas: {
134
+ Model: {
135
+ properties: {
136
+ prop2: {
137
+ type: "string",
138
+ nullable: true
139
+ }
140
+ },
141
+ required: ["prop2"],
142
+ type: "object"
143
+ }
144
+ }
145
+ },
146
+ paths: {
147
+ "/": {
148
+ post: {
149
+ operationId: "testTest",
150
+ parameters: [],
151
+ requestBody: {
152
+ content: {"application/json": {schema: {$ref: "#/components/schemas/Model"}}},
153
+ required: false
154
+ },
155
+ responses: {"200": {description: "Success"}},
156
+ tags: ["Test"]
157
+ }
158
+ }
159
+ },
160
+ tags: [{name: "Test"}]
161
+ });
162
+ });
163
+ it("should declare any prop (String + Nullable)", () => {
164
+ // WHEN
165
+ class Model {
166
+ @Nullable(String)
167
+ prop2: string | null;
168
+ }
169
+
170
+ // THEN
171
+ expect(getJsonSchema(Model)).toEqual({
172
+ properties: {
173
+ prop2: {
174
+ type: ["null", "string"]
175
+ }
176
+ },
177
+ type: "object"
178
+ });
179
+ });
180
+ it("should declare any prop (Integer + Nullable)", () => {
181
+ // WHEN
182
+ class Model {
183
+ @Integer()
184
+ prop1: number;
185
+
186
+ @Nullable(Number)
187
+ @Integer()
188
+ prop2: number | null;
189
+
190
+ @Nullable(Number, String)
191
+ @Integer()
192
+ prop3: number | string | null;
193
+ }
194
+
195
+ @Path("/")
196
+ class MyController {
197
+ @Post("/")
198
+ body(@BodyParams() model: Model) {}
199
+ }
200
+
201
+ // THEN
202
+ expect(getJsonSchema(Model)).toEqual({
203
+ properties: {
204
+ prop1: {
205
+ multipleOf: 1,
206
+ type: "integer"
207
+ },
208
+ prop2: {
209
+ multipleOf: 1,
210
+ type: ["null", "integer"]
211
+ },
212
+ prop3: {
213
+ anyOf: [
214
+ {
215
+ type: "null"
216
+ },
217
+ {
218
+ multipleOf: 1,
219
+ type: "integer"
220
+ },
221
+ {
222
+ type: "string"
223
+ }
224
+ ]
225
+ }
226
+ },
227
+ type: "object"
228
+ });
229
+
230
+ expect(getSpec(MyController, {specType: SpecTypes.OPENAPI})).toEqual({
231
+ components: {
232
+ schemas: {
233
+ Model: {
234
+ properties: {
235
+ prop1: {
236
+ multipleOf: 1,
237
+ type: "integer"
238
+ },
239
+ prop2: {
240
+ multipleOf: 1,
241
+ nullable: true,
242
+ type: "integer"
243
+ },
244
+ prop3: {
245
+ anyOf: [
246
+ {
247
+ multipleOf: 1,
248
+ type: "integer"
249
+ },
250
+ {
251
+ type: "string"
252
+ }
253
+ ],
254
+ nullable: true
255
+ }
256
+ },
257
+ type: "object"
258
+ }
259
+ }
260
+ },
261
+ paths: {
262
+ "/": {
263
+ post: {
264
+ operationId: "myControllerBody",
265
+ parameters: [],
266
+ requestBody: {
267
+ content: {
268
+ "application/json": {
269
+ schema: {
270
+ $ref: "#/components/schemas/Model"
271
+ }
272
+ }
273
+ },
274
+ required: false
275
+ },
276
+ responses: {
277
+ "200": {
278
+ description: "Success"
279
+ }
280
+ },
281
+ tags: ["MyController"]
282
+ }
283
+ }
284
+ },
285
+ tags: [
286
+ {
287
+ name: "MyController"
288
+ }
289
+ ]
290
+ });
291
+ });
292
+ it("should declare any prop (String & Number + Nullable)", () => {
293
+ // WHEN
294
+ class Model {
295
+ @Nullable(String, Number)
296
+ prop2: number | string | null;
297
+ }
298
+
299
+ // THEN
300
+ const schema = getJsonSchema(Model);
301
+ expect(schema).toEqual({
302
+ properties: {
303
+ prop2: {
304
+ anyOf: [
305
+ {
306
+ type: "null"
307
+ },
308
+ {
309
+ type: "string"
310
+ },
311
+ {
312
+ type: "number"
313
+ }
314
+ ]
315
+ }
316
+ },
317
+ type: "object"
318
+ });
319
+
320
+ const ajv = new Ajv({strict: true});
321
+
322
+ expect(ajv.validate(schema, {prop2: null})).toBeTruthy();
323
+ expect(ajv.validate(schema, {prop2: "test"})).toBeTruthy();
324
+ expect(ajv.validate(schema, {prop2: 1})).toBeTruthy();
325
+ expect(ajv.validate(schema, {prop2: false})).toBeFalsy();
326
+ });
327
+ it("should declare any prop (String & Number + Required + Nullable)", () => {
328
+ // WHEN
329
+ class Model {
330
+ @Required(true, null, "")
331
+ @Nullable(String, Number)
332
+ @MaxLength(10)
333
+ @Minimum(0)
334
+ prop2: number | string | null;
335
+ }
336
+
337
+ // THEN
338
+ expect(getJsonSchema(Model)).toEqual({
339
+ properties: {
340
+ prop2: {
341
+ anyOf: [
342
+ {
343
+ type: "null"
344
+ },
345
+ {
346
+ maxLength: 10,
347
+ type: "string"
348
+ },
349
+ {
350
+ minimum: 0,
351
+ type: "number"
352
+ }
353
+ ]
354
+ }
355
+ },
356
+ required: ["prop2"],
357
+ type: "object"
358
+ });
359
+ });
360
+ it("should declare any prop (Date + Nullable)", () => {
361
+ // WHEN
362
+ class Model {
363
+ @Nullable(Date)
364
+ @Format("date-time")
365
+ prop2: Date | null;
366
+ }
367
+
368
+ // THEN
369
+ expect(getJsonSchema(Model)).toEqual({
370
+ properties: {
371
+ prop2: {
372
+ format: "date-time",
373
+ type: ["null", "string"]
374
+ }
375
+ },
376
+ type: "object"
377
+ });
378
+ });
379
+ it("should declare any prop (Model + Nullable)", () => {
380
+ // WHEN
381
+ class Nested {
382
+ @Property()
383
+ id: string;
384
+ }
385
+
386
+ class Model {
387
+ @Nullable(Nested)
388
+ prop2: Nested | null;
389
+ }
390
+
391
+ @Path("/")
392
+ class Test {
393
+ @Post("/")
394
+ test(@BodyParams() model: Model) {}
395
+ }
396
+
397
+ // THEN
398
+ expect(getSpec(Test)).toEqual({
399
+ components: {
400
+ schemas: {
401
+ Model: {
402
+ properties: {
403
+ prop2: {
404
+ anyOf: [
405
+ {
406
+ $ref: "#/components/schemas/Nested"
407
+ }
408
+ ],
409
+ nullable: true
410
+ }
411
+ },
412
+ type: "object"
413
+ },
414
+ Nested: {
415
+ properties: {
416
+ id: {
417
+ type: "string"
418
+ }
419
+ },
420
+ type: "object"
421
+ }
422
+ }
423
+ },
424
+ paths: {
425
+ "/": {
426
+ post: {
427
+ operationId: "testTest",
428
+ parameters: [],
429
+ requestBody: {
430
+ content: {
431
+ "application/json": {
432
+ schema: {
433
+ $ref: "#/components/schemas/Model"
434
+ }
435
+ }
436
+ },
437
+ required: false
438
+ },
439
+ responses: {
440
+ "200": {
441
+ description: "Success"
442
+ }
443
+ },
444
+ tags: ["Test"]
445
+ }
446
+ }
447
+ },
448
+ tags: [
449
+ {
450
+ name: "Test"
451
+ }
452
+ ]
453
+ });
454
+ const schema = getJsonSchema(Model);
455
+ expect(schema).toEqual({
456
+ definitions: {
457
+ Nested: {
458
+ properties: {
459
+ id: {
460
+ type: "string"
461
+ }
462
+ },
463
+ type: "object"
464
+ }
465
+ },
466
+ properties: {
467
+ prop2: {
468
+ anyOf: [
469
+ {
470
+ type: "null"
471
+ },
472
+ {
473
+ $ref: "#/definitions/Nested"
474
+ }
475
+ ]
476
+ }
477
+ },
478
+ type: "object"
479
+ });
480
+
481
+ const ajv = new Ajv({strict: true});
482
+
483
+ expect(ajv.validate(schema, {prop2: null})).toBeTruthy();
484
+ expect(ajv.validate(schema, {prop2: {id: "id"}})).toBeTruthy();
485
+ });
486
+ it("should declare any prop (many Models + Nullable + JsonSchema)", () => {
487
+ // WHEN
488
+ class Nested1 {
489
+ @Property()
490
+ id: string;
491
+
492
+ @Property()
493
+ top: string;
494
+ }
495
+
496
+ class Nested2 {
497
+ @Property()
498
+ id: string;
499
+
500
+ @Property()
501
+ other: string;
502
+ }
503
+
504
+ class Model {
505
+ @Nullable(Nested1, Nested2)
506
+ prop2: Nested1 | Nested2 | null;
507
+ }
508
+
509
+ const schema = getJsonSchema(Model);
510
+
511
+ expect(schema).toEqual({
512
+ definitions: {
513
+ Nested1: {
514
+ properties: {
515
+ id: {
516
+ type: "string"
517
+ },
518
+ top: {
519
+ type: "string"
520
+ }
521
+ },
522
+ type: "object"
523
+ },
524
+ Nested2: {
525
+ properties: {
526
+ id: {
527
+ type: "string"
528
+ },
529
+ other: {
530
+ type: "string"
531
+ }
532
+ },
533
+ type: "object"
534
+ }
535
+ },
536
+ properties: {
537
+ prop2: {
538
+ anyOf: [
539
+ {
540
+ type: "null"
541
+ },
542
+ {
543
+ $ref: "#/definitions/Nested1"
544
+ },
545
+ {
546
+ $ref: "#/definitions/Nested2"
547
+ }
548
+ ]
549
+ }
550
+ },
551
+ type: "object"
552
+ });
553
+
554
+ const ajv = new Ajv({strict: true});
555
+
556
+ ajv.validate(schema, {prop2: null});
557
+
558
+ expect(ajv.errors).toBe(null);
559
+ });
560
+ it("should declare any prop (many Models + Nullable + OS3)", () => {
561
+ // WHEN
562
+ class Nested1 {
563
+ @Property()
564
+ id: string;
565
+
566
+ @Property()
567
+ top: string;
568
+ }
569
+
570
+ class Nested2 {
571
+ @Property()
572
+ id: string;
573
+
574
+ @Property()
575
+ other: string;
576
+ }
577
+
578
+ class Model {
579
+ @Nullable(Nested1, Nested2)
580
+ prop2: Nested1 | Nested2 | null;
581
+ }
582
+
583
+ @Path("/")
584
+ class MyController {
585
+ @Post("/")
586
+ body(@BodyParams() model: Model) {}
587
+ }
588
+
589
+ // THEN
590
+ expect(getSpec(MyController, {specType: SpecTypes.OPENAPI})).toEqual({
591
+ components: {
592
+ schemas: {
593
+ Model: {
594
+ properties: {
595
+ prop2: {
596
+ anyOf: [
597
+ {
598
+ $ref: "#/components/schemas/Nested1"
599
+ },
600
+ {
601
+ $ref: "#/components/schemas/Nested2"
602
+ }
603
+ ],
604
+ nullable: true
605
+ }
606
+ },
607
+ type: "object"
608
+ },
609
+ Nested1: {
610
+ properties: {
611
+ id: {
612
+ type: "string"
613
+ },
614
+ top: {
615
+ type: "string"
616
+ }
617
+ },
618
+ type: "object"
619
+ },
620
+ Nested2: {
621
+ properties: {
622
+ id: {
623
+ type: "string"
624
+ },
625
+ other: {
626
+ type: "string"
627
+ }
628
+ },
629
+ type: "object"
630
+ }
631
+ }
632
+ },
633
+ paths: {
634
+ "/": {
635
+ post: {
636
+ operationId: "myControllerBody",
637
+ parameters: [],
638
+ requestBody: {
639
+ content: {
640
+ "application/json": {
641
+ schema: {
642
+ $ref: "#/components/schemas/Model"
643
+ }
644
+ }
645
+ },
646
+ required: false
647
+ },
648
+ responses: {
649
+ "200": {
650
+ description: "Success"
651
+ }
652
+ },
653
+ tags: ["MyController"]
654
+ }
655
+ }
656
+ },
657
+ tags: [
658
+ {
659
+ name: "MyController"
660
+ }
661
+ ]
662
+ });
663
+
664
+ const schema = getJsonSchema(Model);
665
+
666
+ expect(schema).toEqual({
667
+ definitions: {
668
+ Nested1: {
669
+ properties: {
670
+ id: {
671
+ type: "string"
672
+ },
673
+ top: {
674
+ type: "string"
675
+ }
676
+ },
677
+ type: "object"
678
+ },
679
+ Nested2: {
680
+ properties: {
681
+ id: {
682
+ type: "string"
683
+ },
684
+ other: {
685
+ type: "string"
686
+ }
687
+ },
688
+ type: "object"
689
+ }
690
+ },
691
+ properties: {
692
+ prop2: {
693
+ anyOf: [
694
+ {
695
+ type: "null"
696
+ },
697
+ {
698
+ $ref: "#/definitions/Nested1"
699
+ },
700
+ {
701
+ $ref: "#/definitions/Nested2"
702
+ }
703
+ ]
704
+ }
705
+ },
706
+ type: "object"
707
+ });
708
+
709
+ const ajv = new Ajv({strict: true});
710
+
711
+ expect(ajv.validate(schema, {prop2: null})).toBeTruthy();
712
+ expect(ajv.validate(schema, {prop2: {id: "id", other: "other"}})).toBeTruthy();
713
+ });
714
+ it("should declare any prop (many Models + Nullable + OS2)", () => {
715
+ // WHEN
716
+ class Nested1 {
717
+ @Property()
718
+ id: string;
719
+
720
+ @Property()
721
+ top: string;
722
+ }
723
+
724
+ class Nested2 {
725
+ @Property()
726
+ id: string;
727
+
728
+ @Property()
729
+ other: string;
730
+ }
731
+
732
+ class Model {
733
+ @Nullable(Nested1, Nested2)
734
+ prop2: Nested1 | Nested2 | null;
735
+ }
736
+
737
+ @Path("/")
738
+ class MyController {
739
+ @Post("/")
740
+ body(@In("body") model: Model) {}
741
+ }
742
+
743
+ // THEN
744
+ expect(getSpec(MyController, {specType: SpecTypes.OPENAPI})).toEqual({
745
+ components: {
746
+ schemas: {
747
+ Model: {
748
+ properties: {
749
+ prop2: {
750
+ anyOf: [
751
+ {
752
+ $ref: "#/components/schemas/Nested1"
753
+ },
754
+ {
755
+ $ref: "#/components/schemas/Nested2"
756
+ }
757
+ ],
758
+ nullable: true
759
+ }
760
+ },
761
+ type: "object"
762
+ },
763
+ Nested1: {
764
+ properties: {
765
+ id: {
766
+ type: "string"
767
+ },
768
+ top: {
769
+ type: "string"
770
+ }
771
+ },
772
+ type: "object"
773
+ },
774
+ Nested2: {
775
+ properties: {
776
+ id: {
777
+ type: "string"
778
+ },
779
+ other: {
780
+ type: "string"
781
+ }
782
+ },
783
+ type: "object"
784
+ }
785
+ }
786
+ },
787
+ paths: {
788
+ "/": {
789
+ post: {
790
+ operationId: "myControllerBody",
791
+ parameters: [],
792
+ requestBody: {
793
+ content: {
794
+ "application/json": {
795
+ schema: {
796
+ $ref: "#/components/schemas/Model"
797
+ }
798
+ }
799
+ },
800
+ required: false
801
+ },
802
+ responses: {
803
+ "200": {
804
+ description: "Success"
805
+ }
806
+ },
807
+ tags: ["MyController"]
808
+ }
809
+ }
810
+ },
811
+ tags: [
812
+ {
813
+ name: "MyController"
814
+ }
815
+ ]
816
+ });
817
+ });
818
+ });