@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,695 @@
1
+ import {ancestorsOf, nameOf, Type} from "@tsed/core";
2
+
3
+ import {Post} from "../../test/helpers/Post.js";
4
+ import {CollectionOf, Email, Format, getJsonSchema, JsonEntityStore, MinLength, Name, Property, Required} from "../index.js";
5
+
6
+ describe("getJsonSchema", () => {
7
+ it("should declare all schema correctly (basic)", () => {
8
+ // WHEN
9
+ class Model {
10
+ @Property()
11
+ @Required()
12
+ prop1: string;
13
+
14
+ @CollectionOf(String)
15
+ prop2: string[];
16
+
17
+ @CollectionOf(String)
18
+ prop3: Set<string>;
19
+
20
+ @CollectionOf(String)
21
+ prop4: Map<string, string>;
22
+ }
23
+
24
+ // THEN
25
+ expect(getJsonSchema(Model)).toEqual({
26
+ type: "object",
27
+ required: ["prop1"],
28
+ properties: {
29
+ prop1: {
30
+ minLength: 1,
31
+ type: "string"
32
+ },
33
+ prop2: {
34
+ type: "array",
35
+ items: {
36
+ type: "string"
37
+ }
38
+ },
39
+ prop3: {
40
+ type: "array",
41
+ uniqueItems: true,
42
+ items: {
43
+ type: "string"
44
+ }
45
+ },
46
+ prop4: {
47
+ type: "object",
48
+ additionalProperties: {
49
+ type: "string"
50
+ }
51
+ }
52
+ }
53
+ });
54
+ });
55
+ it("should declare all schema correctly (alias)", () => {
56
+ // WHEN
57
+ class Model {
58
+ @Name("prop_1")
59
+ @Required()
60
+ prop1: string;
61
+ }
62
+
63
+ // THEN
64
+ const classSchema = JsonEntityStore.from(Model);
65
+
66
+ expect(classSchema.schema.toJSON()).toEqual({
67
+ type: "object",
68
+ required: ["prop_1"],
69
+ properties: {
70
+ prop_1: {
71
+ minLength: 1,
72
+ type: "string"
73
+ }
74
+ }
75
+ });
76
+ });
77
+ it("should declare prop with a nested model", () => {
78
+ // WHEN
79
+ class NestedModel {
80
+ @Property()
81
+ id: string;
82
+
83
+ @Property()
84
+ prop1: string;
85
+ }
86
+
87
+ class Model {
88
+ @Property()
89
+ id: string;
90
+
91
+ @Property()
92
+ nested: NestedModel;
93
+ }
94
+
95
+ // THEN
96
+
97
+ expect(getJsonSchema(NestedModel)).toEqual({
98
+ type: "object",
99
+ properties: {
100
+ id: {
101
+ type: "string"
102
+ },
103
+ prop1: {
104
+ type: "string"
105
+ }
106
+ }
107
+ });
108
+
109
+ expect(getJsonSchema(Model)).toEqual({
110
+ type: "object",
111
+ properties: {
112
+ id: {
113
+ type: "string"
114
+ },
115
+ nested: {
116
+ $ref: "#/definitions/NestedModel"
117
+ }
118
+ },
119
+ definitions: {
120
+ NestedModel: {
121
+ type: "object",
122
+ properties: {
123
+ id: {
124
+ type: "string"
125
+ },
126
+ prop1: {
127
+ type: "string"
128
+ }
129
+ }
130
+ }
131
+ }
132
+ });
133
+ });
134
+ it("should declare prop with a nested model with named model", () => {
135
+ // WHEN
136
+ @Name("Nested")
137
+ class NestedModel {
138
+ @Property()
139
+ id: string;
140
+
141
+ @Property()
142
+ prop1: string;
143
+ }
144
+
145
+ class Model {
146
+ @Property()
147
+ id: string;
148
+
149
+ @Property()
150
+ nested: NestedModel;
151
+ }
152
+
153
+ // THEN
154
+
155
+ expect(getJsonSchema(NestedModel)).toEqual({
156
+ type: "object",
157
+ properties: {
158
+ id: {
159
+ type: "string"
160
+ },
161
+ prop1: {
162
+ type: "string"
163
+ }
164
+ }
165
+ });
166
+
167
+ const options = {components: {schemas: {}}};
168
+ expect(JsonEntityStore.from(Model).schema.clone().toJSON(options)).toEqual({
169
+ type: "object",
170
+ properties: {
171
+ id: {
172
+ type: "string"
173
+ },
174
+ nested: {
175
+ $ref: "#/definitions/Nested"
176
+ }
177
+ }
178
+ });
179
+ expect(options).toEqual({
180
+ components: {
181
+ schemas: {
182
+ Nested: {
183
+ properties: {
184
+ id: {
185
+ type: "string"
186
+ },
187
+ prop1: {
188
+ type: "string"
189
+ }
190
+ },
191
+ type: "object"
192
+ }
193
+ }
194
+ }
195
+ });
196
+ expect(getJsonSchema(Model)).toEqual({
197
+ type: "object",
198
+ properties: {
199
+ id: {
200
+ type: "string"
201
+ },
202
+ nested: {
203
+ $ref: "#/definitions/Nested"
204
+ }
205
+ },
206
+ definitions: {
207
+ Nested: {
208
+ type: "object",
209
+ properties: {
210
+ id: {
211
+ type: "string"
212
+ },
213
+ prop1: {
214
+ type: "string"
215
+ }
216
+ }
217
+ }
218
+ }
219
+ });
220
+ });
221
+ it("should declare prop with a nested of nested model (Array)", () => {
222
+ // WHEN
223
+ class ChildModel {
224
+ @Property()
225
+ id: string;
226
+
227
+ @Property()
228
+ prop1: string;
229
+ }
230
+
231
+ class NestedModel {
232
+ @Property()
233
+ id: string;
234
+
235
+ @CollectionOf(ChildModel)
236
+ children: ChildModel[];
237
+ }
238
+
239
+ class Model {
240
+ @Property()
241
+ id: string;
242
+
243
+ @Property()
244
+ nested: NestedModel;
245
+ }
246
+
247
+ // THEN
248
+ expect(getJsonSchema(ChildModel)).toEqual({
249
+ type: "object",
250
+ properties: {
251
+ id: {
252
+ type: "string"
253
+ },
254
+ prop1: {
255
+ type: "string"
256
+ }
257
+ }
258
+ });
259
+
260
+ expect(getJsonSchema(NestedModel)).toEqual({
261
+ type: "object",
262
+ properties: {
263
+ id: {
264
+ type: "string"
265
+ },
266
+ children: {
267
+ type: "array",
268
+ items: {
269
+ $ref: "#/definitions/ChildModel"
270
+ }
271
+ }
272
+ },
273
+ definitions: {
274
+ ChildModel: {
275
+ type: "object",
276
+ properties: {
277
+ id: {
278
+ type: "string"
279
+ },
280
+ prop1: {
281
+ type: "string"
282
+ }
283
+ }
284
+ }
285
+ }
286
+ });
287
+
288
+ expect(getJsonSchema(Model)).toEqual({
289
+ type: "object",
290
+ properties: {
291
+ id: {
292
+ type: "string"
293
+ },
294
+ nested: {
295
+ $ref: "#/definitions/NestedModel"
296
+ }
297
+ },
298
+ definitions: {
299
+ NestedModel: {
300
+ type: "object",
301
+ properties: {
302
+ id: {
303
+ type: "string"
304
+ },
305
+ children: {
306
+ type: "array",
307
+ items: {
308
+ $ref: "#/definitions/ChildModel"
309
+ }
310
+ }
311
+ }
312
+ },
313
+ ChildModel: {
314
+ type: "object",
315
+ properties: {
316
+ id: {
317
+ type: "string"
318
+ },
319
+ prop1: {
320
+ type: "string"
321
+ }
322
+ }
323
+ }
324
+ }
325
+ });
326
+ });
327
+ it("should declare prop with a nested of nested model (Set)", () => {
328
+ // WHEN
329
+ class ChildModel {
330
+ @Property()
331
+ id: string;
332
+
333
+ @Property()
334
+ prop1: string;
335
+ }
336
+
337
+ class NestedModel {
338
+ @Property()
339
+ id: string;
340
+
341
+ @CollectionOf(ChildModel)
342
+ children: Set<ChildModel>;
343
+ }
344
+
345
+ class Model {
346
+ @Property()
347
+ id: string;
348
+
349
+ @Property()
350
+ nested: NestedModel;
351
+ }
352
+
353
+ // THEN
354
+ expect(getJsonSchema(ChildModel)).toEqual({
355
+ type: "object",
356
+ properties: {
357
+ id: {
358
+ type: "string"
359
+ },
360
+ prop1: {
361
+ type: "string"
362
+ }
363
+ }
364
+ });
365
+
366
+ expect(getJsonSchema(NestedModel)).toEqual({
367
+ type: "object",
368
+ properties: {
369
+ id: {
370
+ type: "string"
371
+ },
372
+ children: {
373
+ type: "array",
374
+ uniqueItems: true,
375
+ items: {
376
+ $ref: "#/definitions/ChildModel"
377
+ }
378
+ }
379
+ },
380
+ definitions: {
381
+ ChildModel: {
382
+ type: "object",
383
+ properties: {
384
+ id: {
385
+ type: "string"
386
+ },
387
+ prop1: {
388
+ type: "string"
389
+ }
390
+ }
391
+ }
392
+ }
393
+ });
394
+
395
+ expect(getJsonSchema(Model)).toEqual({
396
+ type: "object",
397
+ properties: {
398
+ id: {
399
+ type: "string"
400
+ },
401
+ nested: {
402
+ $ref: "#/definitions/NestedModel"
403
+ }
404
+ },
405
+ definitions: {
406
+ NestedModel: {
407
+ type: "object",
408
+ properties: {
409
+ id: {
410
+ type: "string"
411
+ },
412
+ children: {
413
+ type: "array",
414
+ uniqueItems: true,
415
+ items: {
416
+ $ref: "#/definitions/ChildModel"
417
+ }
418
+ }
419
+ }
420
+ },
421
+ ChildModel: {
422
+ type: "object",
423
+ properties: {
424
+ id: {
425
+ type: "string"
426
+ },
427
+ prop1: {
428
+ type: "string"
429
+ }
430
+ }
431
+ }
432
+ }
433
+ });
434
+ });
435
+ it("should declare prop with a nested of nested model (Map)", () => {
436
+ // WHEN
437
+ class ChildModel {
438
+ @Property()
439
+ id: string;
440
+
441
+ @Property()
442
+ prop1: string;
443
+ }
444
+
445
+ class NestedModel {
446
+ @Property()
447
+ id: string;
448
+
449
+ @CollectionOf(ChildModel)
450
+ children: Map<string, ChildModel>;
451
+ }
452
+
453
+ class Model {
454
+ @Property()
455
+ id: string;
456
+
457
+ @Property()
458
+ nested: NestedModel;
459
+ }
460
+
461
+ // THEN
462
+ expect(getJsonSchema(ChildModel)).toEqual({
463
+ type: "object",
464
+ properties: {
465
+ id: {
466
+ type: "string"
467
+ },
468
+ prop1: {
469
+ type: "string"
470
+ }
471
+ }
472
+ });
473
+
474
+ expect(getJsonSchema(NestedModel)).toEqual({
475
+ type: "object",
476
+ properties: {
477
+ id: {
478
+ type: "string"
479
+ },
480
+ children: {
481
+ type: "object",
482
+ additionalProperties: {
483
+ $ref: "#/definitions/ChildModel"
484
+ }
485
+ }
486
+ },
487
+ definitions: {
488
+ ChildModel: {
489
+ type: "object",
490
+ properties: {
491
+ id: {
492
+ type: "string"
493
+ },
494
+ prop1: {
495
+ type: "string"
496
+ }
497
+ }
498
+ }
499
+ }
500
+ });
501
+
502
+ expect(getJsonSchema(Model)).toEqual({
503
+ type: "object",
504
+ properties: {
505
+ id: {
506
+ type: "string"
507
+ },
508
+ nested: {
509
+ $ref: "#/definitions/NestedModel"
510
+ }
511
+ },
512
+ definitions: {
513
+ NestedModel: {
514
+ type: "object",
515
+ properties: {
516
+ id: {
517
+ type: "string"
518
+ },
519
+ children: {
520
+ type: "object",
521
+ additionalProperties: {
522
+ $ref: "#/definitions/ChildModel"
523
+ }
524
+ }
525
+ }
526
+ },
527
+ ChildModel: {
528
+ type: "object",
529
+ properties: {
530
+ id: {
531
+ type: "string"
532
+ },
533
+ prop1: {
534
+ type: "string"
535
+ }
536
+ }
537
+ }
538
+ }
539
+ });
540
+ });
541
+ it("should accept circular ref", () => {
542
+ // WHEN
543
+
544
+ // THEN
545
+ const classSchema = getJsonSchema(Post);
546
+
547
+ expect(classSchema).toEqual({
548
+ definitions: {
549
+ Post: {
550
+ properties: {
551
+ id: {
552
+ type: "string"
553
+ },
554
+ owner: {
555
+ $ref: "#/definitions/User"
556
+ }
557
+ },
558
+ type: "object"
559
+ },
560
+ User: {
561
+ properties: {
562
+ name: {
563
+ type: "string"
564
+ },
565
+ posts: {
566
+ items: {
567
+ $ref: "#/definitions/Post"
568
+ },
569
+ type: "array"
570
+ }
571
+ },
572
+ type: "object"
573
+ }
574
+ },
575
+ properties: {
576
+ id: {
577
+ type: "string"
578
+ },
579
+ owner: {
580
+ $ref: "#/definitions/User"
581
+ }
582
+ },
583
+ type: "object"
584
+ });
585
+ });
586
+ it("should return the json schema for an inherited model", () => {
587
+ class Base {
588
+ @Property()
589
+ id: string;
590
+
591
+ @Required()
592
+ @Email()
593
+ email: string;
594
+ }
595
+
596
+ class Model extends Base {
597
+ @MinLength(0)
598
+ declare email: string;
599
+
600
+ @Property()
601
+ name: string;
602
+ }
603
+
604
+ expect(getJsonSchema(Model)).toEqual({
605
+ type: "object",
606
+ properties: {
607
+ id: {
608
+ type: "string"
609
+ },
610
+ email: {
611
+ type: "string",
612
+ minLength: 0,
613
+ format: "email"
614
+ },
615
+ name: {
616
+ type: "string"
617
+ }
618
+ },
619
+ required: ["email"]
620
+ });
621
+ });
622
+ it("should keep required meta from inherited class", () => {
623
+ // WHEN
624
+ const AutoUUID = <T extends Type<any>>(Base: T) => {
625
+ class AutoUUIDMixin extends Base {
626
+ @Property() //showing up in swagger as a property
627
+ @Format("uuid") //showing up as a uuid in swagger
628
+ @Required(true)
629
+ id: string;
630
+ }
631
+
632
+ return AutoUUIDMixin;
633
+ };
634
+
635
+ const WithDates = <T extends Type<any>>(Base: T) => {
636
+ class WithDates extends Base {
637
+ @Property() //showing up in swagger as a property
638
+ @Format("datetime") //showing up as a uuid in swagger
639
+ @Required(true)
640
+ created_at: string;
641
+
642
+ @Property() //showing up in swagger as a property
643
+ @Format("datetime") //showing up as a uuid in swagger
644
+ @Required(true)
645
+ updated_at: string;
646
+ }
647
+
648
+ return WithDates;
649
+ };
650
+
651
+ class Model {
652
+ @Required()
653
+ name: string;
654
+ }
655
+
656
+ const DefaultMixins = <T extends Type<any>>(Base: T) => AutoUUID(WithDates(Base));
657
+
658
+ class EmailTemplate extends DefaultMixins(Model) {
659
+ @Required()
660
+ engine: string;
661
+ }
662
+
663
+ // THEN
664
+ expect(ancestorsOf(EmailTemplate).map(nameOf)).toEqual(["Model", "WithDates", "AutoUUIDMixin", "EmailTemplate"]);
665
+ expect(getJsonSchema(EmailTemplate)).toEqual({
666
+ properties: {
667
+ created_at: {
668
+ format: "datetime",
669
+ minLength: 1,
670
+ type: "string"
671
+ },
672
+ engine: {
673
+ minLength: 1,
674
+ type: "string"
675
+ },
676
+ id: {
677
+ format: "uuid",
678
+ minLength: 1,
679
+ type: "string"
680
+ },
681
+ name: {
682
+ minLength: 1,
683
+ type: "string"
684
+ },
685
+ updated_at: {
686
+ format: "datetime",
687
+ minLength: 1,
688
+ type: "string"
689
+ }
690
+ },
691
+ required: ["name", "created_at", "updated_at", "id", "engine"],
692
+ type: "object"
693
+ });
694
+ });
695
+ });