@tsed/schema 8.0.1 → 8.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (292) hide show
  1. package/package.json +11 -10
  2. package/readme.md +6 -6
  3. package/src/components/async-api/channelsMapper.ts +81 -0
  4. package/src/components/async-api/generate.ts +24 -0
  5. package/src/components/async-api/messageMapper.ts +62 -0
  6. package/src/components/async-api/payloadMapper.ts +61 -0
  7. package/src/components/async-api/responseMapper.ts +70 -0
  8. package/src/components/default/anyMapper.spec.ts +7 -0
  9. package/src/components/default/anyMapper.ts +33 -0
  10. package/src/components/default/classMapper.ts +55 -0
  11. package/src/components/default/discriminatorMappingMapper.ts +36 -0
  12. package/src/components/default/enumsMapper.spec.ts +64 -0
  13. package/src/components/default/enumsMapper.ts +17 -0
  14. package/src/components/default/genericsMapper.ts +70 -0
  15. package/src/components/default/inheritedClassMapper.ts +24 -0
  16. package/src/components/default/itemMapper.ts +8 -0
  17. package/src/components/default/lazyRefMapper.ts +21 -0
  18. package/src/components/default/mapMapper.ts +25 -0
  19. package/src/components/default/nullableMapper.ts +55 -0
  20. package/src/components/default/objectMapper.ts +36 -0
  21. package/src/components/default/ofMapper.ts +16 -0
  22. package/src/components/default/propertiesMapper.ts +25 -0
  23. package/src/components/default/requiredMapper.ts +69 -0
  24. package/src/components/default/schemaMapper.ts +151 -0
  25. package/src/components/index.ts +36 -0
  26. package/src/components/open-spec/discriminatorMappingMapper.ts +7 -0
  27. package/src/components/open-spec/enumsMapper.ts +20 -0
  28. package/src/components/open-spec/generate.ts +29 -0
  29. package/src/components/open-spec/nullableMapper.ts +51 -0
  30. package/src/components/open-spec/operationInFilesMapper.ts +32 -0
  31. package/src/components/open-spec/operationInParameterMapper.ts +56 -0
  32. package/src/components/open-spec/operationInParametersMapper.ts +9 -0
  33. package/src/components/open-spec/operationInQueryMapper.ts +74 -0
  34. package/src/components/open-spec/operationMapper.ts +65 -0
  35. package/src/components/open-spec/operationMediaMapper.ts +11 -0
  36. package/src/components/open-spec/operationRequestBodyMapper.ts +82 -0
  37. package/src/components/open-spec/operationResponseMapper.ts +26 -0
  38. package/src/components/open-spec/pathsMapper.ts +132 -0
  39. package/src/constants/OperationVerbs.ts +29 -0
  40. package/src/constants/httpStatusMessages.ts +21 -0
  41. package/src/constants/jsonSchemaProperties.ts +17 -0
  42. package/src/decorators/class/children.ts +22 -0
  43. package/src/decorators/class/discriminatorValue.ts +53 -0
  44. package/src/decorators/collections/collectionContains.ts +25 -0
  45. package/src/decorators/collections/collectionOf.spec.ts +327 -0
  46. package/src/decorators/collections/collectionOf.ts +182 -0
  47. package/src/decorators/collections/maxItems.spec.ts +40 -0
  48. package/src/decorators/collections/maxItems.ts +59 -0
  49. package/src/decorators/collections/maxProperties.spec.ts +109 -0
  50. package/src/decorators/collections/maxProperties.ts +82 -0
  51. package/src/decorators/collections/minItems.spec.ts +40 -0
  52. package/src/decorators/collections/minItems.ts +63 -0
  53. package/src/decorators/collections/minProperties.spec.ts +109 -0
  54. package/src/decorators/collections/minProperties.ts +81 -0
  55. package/src/decorators/collections/uniqueItems.spec.ts +28 -0
  56. package/src/decorators/collections/uniqueItems.ts +59 -0
  57. package/src/decorators/common/__snapshots__/groups.spec.ts.snap +321 -0
  58. package/src/decorators/common/additionalProperties.spec.ts +133 -0
  59. package/src/decorators/common/additionalProperties.ts +17 -0
  60. package/src/decorators/common/allOf.spec.ts +276 -0
  61. package/src/decorators/common/allOf.ts +19 -0
  62. package/src/decorators/common/allow.spec.ts +130 -0
  63. package/src/decorators/common/allow.ts +50 -0
  64. package/src/decorators/common/any.spec.ts +109 -0
  65. package/src/decorators/common/any.ts +45 -0
  66. package/src/decorators/common/anyOf.spec.ts +270 -0
  67. package/src/decorators/common/anyOf.ts +23 -0
  68. package/src/decorators/common/const.spec.ts +98 -0
  69. package/src/decorators/common/const.ts +68 -0
  70. package/src/decorators/common/customKey.spec.ts +105 -0
  71. package/src/decorators/common/customKey.ts +39 -0
  72. package/src/decorators/common/default.spec.ts +25 -0
  73. package/src/decorators/common/default.ts +47 -0
  74. package/src/decorators/common/defaultMsg.spec.ts +22 -0
  75. package/src/decorators/common/defaultMsg.ts +38 -0
  76. package/src/decorators/common/description.spec.ts +166 -0
  77. package/src/decorators/common/description.ts +77 -0
  78. package/src/decorators/common/discriminatorKey.ts +14 -0
  79. package/src/decorators/common/enum.spec.ts +305 -0
  80. package/src/decorators/common/enum.ts +111 -0
  81. package/src/decorators/common/errorMsg.spec.ts +57 -0
  82. package/src/decorators/common/errorMsg.ts +14 -0
  83. package/src/decorators/common/example.spec.ts +93 -0
  84. package/src/decorators/common/example.ts +18 -0
  85. package/src/decorators/common/examples.spec.ts +101 -0
  86. package/src/decorators/common/examples.ts +20 -0
  87. package/src/decorators/common/exclusiveMaximum.spec.ts +47 -0
  88. package/src/decorators/common/exclusiveMaximum.ts +75 -0
  89. package/src/decorators/common/exclusiveMinimum.spec.ts +47 -0
  90. package/src/decorators/common/exclusiveMinimum.ts +75 -0
  91. package/src/decorators/common/format.spec.ts +149 -0
  92. package/src/decorators/common/format.ts +486 -0
  93. package/src/decorators/common/forwardGroups.spec.ts +270 -0
  94. package/src/decorators/common/forwardGroups.ts +16 -0
  95. package/src/decorators/common/groups.spec.ts +968 -0
  96. package/src/decorators/common/groups.ts +66 -0
  97. package/src/decorators/common/hidden.spec.ts +52 -0
  98. package/src/decorators/common/hidden.ts +36 -0
  99. package/src/decorators/common/ignore.spec.ts +88 -0
  100. package/src/decorators/common/ignore.ts +66 -0
  101. package/src/decorators/common/integer.spec.ts +110 -0
  102. package/src/decorators/common/integer.ts +69 -0
  103. package/src/decorators/common/jsonEntityFn.spec.ts +16 -0
  104. package/src/decorators/common/jsonEntityFn.ts +33 -0
  105. package/src/decorators/common/maxLength.spec.ts +61 -0
  106. package/src/decorators/common/maxLength.ts +88 -0
  107. package/src/decorators/common/maximum.spec.ts +73 -0
  108. package/src/decorators/common/maximum.ts +146 -0
  109. package/src/decorators/common/minLength.spec.ts +186 -0
  110. package/src/decorators/common/minLength.ts +89 -0
  111. package/src/decorators/common/minimum.spec.ts +74 -0
  112. package/src/decorators/common/minimum.ts +145 -0
  113. package/src/decorators/common/multipleOf.spec.ts +33 -0
  114. package/src/decorators/common/multipleOf.ts +80 -0
  115. package/src/decorators/common/name.spec.ts +56 -0
  116. package/src/decorators/common/name.ts +50 -0
  117. package/src/decorators/common/nullable.spec.ts +818 -0
  118. package/src/decorators/common/nullable.ts +32 -0
  119. package/src/decorators/common/oneOf.spec.ts +301 -0
  120. package/src/decorators/common/oneOf.ts +24 -0
  121. package/src/decorators/common/optional.spec.ts +43 -0
  122. package/src/decorators/common/optional.ts +26 -0
  123. package/src/decorators/common/pattern.spec.ts +27 -0
  124. package/src/decorators/common/pattern.ts +102 -0
  125. package/src/decorators/common/property.ts +24 -0
  126. package/src/decorators/common/readOnly.spec.ts +135 -0
  127. package/src/decorators/common/readOnly.ts +17 -0
  128. package/src/decorators/common/recordOf.spec.ts +41 -0
  129. package/src/decorators/common/recordOf.ts +59 -0
  130. package/src/decorators/common/required.spec.ts +188 -0
  131. package/src/decorators/common/required.ts +50 -0
  132. package/src/decorators/common/requiredGroups.spec.ts +127 -0
  133. package/src/decorators/common/requiredGroups.ts +23 -0
  134. package/src/decorators/common/schema.spec.ts +23 -0
  135. package/src/decorators/common/schema.ts +64 -0
  136. package/src/decorators/common/title.spec.ts +20 -0
  137. package/src/decorators/common/title.ts +45 -0
  138. package/src/decorators/common/typeError.spec.ts +22 -0
  139. package/src/decorators/common/typeError.ts +15 -0
  140. package/src/decorators/common/writeOnly.spec.ts +105 -0
  141. package/src/decorators/common/writeOnly.ts +17 -0
  142. package/src/decorators/config/jsonEntityComponent.ts +16 -0
  143. package/src/decorators/generics/genericOf.spec.ts +246 -0
  144. package/src/decorators/generics/genericOf.ts +112 -0
  145. package/src/decorators/generics/generics.ts +22 -0
  146. package/src/decorators/operations/acceptMime.spec.ts +39 -0
  147. package/src/decorators/operations/acceptMime.ts +25 -0
  148. package/src/decorators/operations/consumes.spec.ts +89 -0
  149. package/src/decorators/operations/consumes.ts +37 -0
  150. package/src/decorators/operations/contentType.spec.ts +40 -0
  151. package/src/decorators/operations/contentType.ts +24 -0
  152. package/src/decorators/operations/deprecated.spec.ts +223 -0
  153. package/src/decorators/operations/deprecated.ts +49 -0
  154. package/src/decorators/operations/header.spec.ts +214 -0
  155. package/src/decorators/operations/header.ts +62 -0
  156. package/src/decorators/operations/in.spec.ts +321 -0
  157. package/src/decorators/operations/in.ts +141 -0
  158. package/src/decorators/operations/inFile.spec.ts +209 -0
  159. package/src/decorators/operations/inFile.ts +55 -0
  160. package/src/decorators/operations/location.spec.ts +51 -0
  161. package/src/decorators/operations/location.ts +26 -0
  162. package/src/decorators/operations/operation.ts +119 -0
  163. package/src/decorators/operations/operationId.spec.ts +43 -0
  164. package/src/decorators/operations/operationId.ts +22 -0
  165. package/src/decorators/operations/operationPath.spec.ts +42 -0
  166. package/src/decorators/operations/operationPath.ts +33 -0
  167. package/src/decorators/operations/partial.spec.ts +249 -0
  168. package/src/decorators/operations/partial.ts +14 -0
  169. package/src/decorators/operations/path.spec.ts +47 -0
  170. package/src/decorators/operations/path.ts +27 -0
  171. package/src/decorators/operations/produces.spec.ts +89 -0
  172. package/src/decorators/operations/produces.ts +44 -0
  173. package/src/decorators/operations/publish.spec.ts +26 -0
  174. package/src/decorators/operations/publish.ts +6 -0
  175. package/src/decorators/operations/redirect.spec.ts +146 -0
  176. package/src/decorators/operations/redirect.ts +82 -0
  177. package/src/decorators/operations/returns.spec.ts +1393 -0
  178. package/src/decorators/operations/returns.ts +662 -0
  179. package/src/decorators/operations/route.spec.ts +211 -0
  180. package/src/decorators/operations/route.ts +154 -0
  181. package/src/decorators/operations/security.spec.ts +162 -0
  182. package/src/decorators/operations/security.ts +83 -0
  183. package/src/decorators/operations/status.spec.ts +582 -0
  184. package/src/decorators/operations/status.ts +130 -0
  185. package/src/decorators/operations/subscribe.spec.ts +32 -0
  186. package/src/decorators/operations/subscribe.ts +6 -0
  187. package/src/decorators/operations/summary.spec.ts +45 -0
  188. package/src/decorators/operations/summary.ts +32 -0
  189. package/src/decorators/operations/tags.spec.ts +113 -0
  190. package/src/decorators/operations/tags.ts +53 -0
  191. package/src/decorators/operations/view.spec.ts +16 -0
  192. package/src/decorators/operations/view.ts +27 -0
  193. package/src/domain/DecoratorContext.ts +116 -0
  194. package/src/domain/JsonAliasMap.ts +19 -0
  195. package/src/domain/JsonClassStore.ts +35 -0
  196. package/src/domain/JsonDiscriminator.spec.ts +65 -0
  197. package/src/domain/JsonDiscriminator.ts +52 -0
  198. package/src/domain/JsonEntityStore.spec.ts +277 -0
  199. package/src/domain/JsonEntityStore.ts +284 -0
  200. package/src/domain/JsonFormatTypes.ts +17 -0
  201. package/src/domain/JsonLazyRef.ts +21 -0
  202. package/src/domain/JsonMap.spec.ts +15 -0
  203. package/src/domain/JsonMap.ts +33 -0
  204. package/src/domain/JsonMedia.ts +24 -0
  205. package/src/domain/JsonMethodStore.spec.ts +301 -0
  206. package/src/domain/JsonMethodStore.ts +212 -0
  207. package/src/domain/JsonOperation.spec.ts +126 -0
  208. package/src/domain/JsonOperation.ts +219 -0
  209. package/src/domain/JsonOperationPathsMap.ts +16 -0
  210. package/src/domain/JsonOperationRoute.spec.ts +109 -0
  211. package/src/domain/JsonOperationRoute.ts +75 -0
  212. package/src/domain/JsonParameter.ts +70 -0
  213. package/src/domain/JsonParameterStore.spec.ts +212 -0
  214. package/src/domain/JsonParameterStore.ts +138 -0
  215. package/src/domain/JsonParameterTypes.spec.ts +12 -0
  216. package/src/domain/JsonParameterTypes.ts +20 -0
  217. package/src/domain/JsonPropertyStore.spec.ts +177 -0
  218. package/src/domain/JsonPropertyStore.ts +92 -0
  219. package/src/domain/JsonRequestBody.ts +47 -0
  220. package/src/domain/JsonResponse.spec.ts +33 -0
  221. package/src/domain/JsonResponse.ts +64 -0
  222. package/src/domain/JsonSchema.spec.ts +1470 -0
  223. package/src/domain/JsonSchema.ts +1049 -0
  224. package/src/domain/SpecTypes.ts +6 -0
  225. package/src/hooks/alterIgnore.ts +25 -0
  226. package/src/hooks/alterOneOf.ts +67 -0
  227. package/src/hooks/alterRequiredGroups.ts +12 -0
  228. package/src/index.ts +185 -0
  229. package/src/interfaces/IgnoreCallback.ts +5 -0
  230. package/src/interfaces/JsonHookContext.ts +18 -0
  231. package/src/interfaces/JsonOpenSpec.ts +7 -0
  232. package/src/interfaces/JsonSchemaOptions.ts +35 -0
  233. package/src/registries/JsonSchemaMapperContainer.ts +52 -0
  234. package/src/registries/enumRegistries.ts +3 -0
  235. package/src/utils/__snapshots__/generateSpec.spec.ts.snap +109 -0
  236. package/src/utils/buildPath.ts +8 -0
  237. package/src/utils/concatParameters.ts +12 -0
  238. package/src/utils/concatPath.spec.ts +23 -0
  239. package/src/utils/concatPath.ts +24 -0
  240. package/src/utils/defineStatusModel.ts +11 -0
  241. package/src/utils/from.spec.ts +243 -0
  242. package/src/utils/from.ts +281 -0
  243. package/src/utils/generateSpec.spec.ts +1262 -0
  244. package/src/utils/generateSpec.ts +54 -0
  245. package/src/utils/generics.ts +83 -0
  246. package/src/utils/getComputedType.spec.ts +45 -0
  247. package/src/utils/getComputedType.ts +39 -0
  248. package/src/utils/getInheritedStores.spec.ts +47 -0
  249. package/src/utils/getInheritedStores.ts +23 -0
  250. package/src/utils/getJsonEntityStore.ts +36 -0
  251. package/src/utils/getJsonPathParameters.spec.ts +126 -0
  252. package/src/utils/getJsonPathParameters.ts +97 -0
  253. package/src/utils/getJsonSchema.spec.ts +695 -0
  254. package/src/utils/getJsonSchema.ts +69 -0
  255. package/src/utils/getJsonType.spec.ts +28 -0
  256. package/src/utils/getJsonType.ts +46 -0
  257. package/src/utils/getOperationsRoutes.spec.ts +284 -0
  258. package/src/utils/getOperationsRoutes.ts +56 -0
  259. package/src/utils/getOperationsStore.spec.ts +47 -0
  260. package/src/utils/getOperationsStores.ts +37 -0
  261. package/src/utils/getPropertiesStores.spec.ts +138 -0
  262. package/src/utils/getPropertiesStores.ts +66 -0
  263. package/src/utils/getSpec.spec.ts +610 -0
  264. package/src/utils/getSpec.ts +117 -0
  265. package/src/utils/getSpecType.spec.ts +16 -0
  266. package/src/utils/getSpecType.ts +19 -0
  267. package/src/utils/isSuccessStatus.spec.ts +22 -0
  268. package/src/utils/isSuccessStatus.ts +2 -0
  269. package/src/utils/mapHeaders.spec.ts +57 -0
  270. package/src/utils/mapHeaders.ts +33 -0
  271. package/src/utils/mapOpenSpec.ts +44 -0
  272. package/src/utils/mapOpenSpec2.ts +50 -0
  273. package/src/utils/mapOpenSpec3.ts +25 -0
  274. package/src/utils/mapOpenSpecInfo.ts +19 -0
  275. package/src/utils/mapOperationOptions.ts +34 -0
  276. package/src/utils/matchGroups.spec.ts +85 -0
  277. package/src/utils/matchGroups.ts +64 -0
  278. package/src/utils/mergeSpec.spec.ts +533 -0
  279. package/src/utils/mergeSpec.ts +54 -0
  280. package/src/utils/operationIdFormatter.spec.ts +11 -0
  281. package/src/utils/operationIdFormatter.ts +49 -0
  282. package/src/utils/ref.ts +65 -0
  283. package/src/utils/removeHiddenOperation.ts +5 -0
  284. package/src/utils/serializeEnumValues.ts +27 -0
  285. package/src/utils/somethingOf.ts +11 -0
  286. package/src/utils/toJsonMapCollection.spec.ts +18 -0
  287. package/src/utils/toJsonMapCollection.ts +12 -0
  288. package/src/utils/toJsonRegex.ts +7 -0
  289. package/src/utils/transformToOS2.spec.ts +177 -0
  290. package/src/utils/transformToOS2.ts +607 -0
  291. package/src/utils/withErrorMsg.spec.ts +16 -0
  292. package/src/utils/withErrorMsg.ts +56 -0
@@ -0,0 +1,1049 @@
1
+ import {ancestorsOf, classOf, isArray, isClass, isFunction, isObject, isPrimitiveClass, nameOf, Type, uniq, ValueOf} from "@tsed/core";
2
+ import {Hooks} from "@tsed/hooks";
3
+ import type {JSONSchema6, JSONSchema6Definition, JSONSchema6Type, JSONSchema6TypeName, JSONSchema6Version} from "json-schema";
4
+
5
+ import {IgnoreCallback} from "../interfaces/IgnoreCallback.js";
6
+ import {JsonSchemaOptions} from "../interfaces/JsonSchemaOptions.js";
7
+ import {enumsRegistry} from "../registries/enumRegistries.js";
8
+ import {execMapper} from "../registries/JsonSchemaMapperContainer.js";
9
+ import {NestedGenerics} from "../utils/generics.js";
10
+ import {getComputedType} from "../utils/getComputedType.js";
11
+ import {getJsonType} from "../utils/getJsonType.js";
12
+ import {serializeEnumValues} from "../utils/serializeEnumValues.js";
13
+ import {toJsonRegex} from "../utils/toJsonRegex.js";
14
+ import {AliasMap, AliasType} from "./JsonAliasMap.js";
15
+ import {Discriminator} from "./JsonDiscriminator.js";
16
+ import {JsonEntityStore} from "./JsonEntityStore.js";
17
+ import {JsonFormatTypes} from "./JsonFormatTypes.js";
18
+ import {JsonLazyRef} from "./JsonLazyRef.js";
19
+
20
+ export interface JsonSchemaObject extends JSONSchema6, Record<string, any> {
21
+ type: (any | JSONSchema6TypeName) | (any | JSONSchema6TypeName)[];
22
+ additionalProperties?: boolean | JSONSchema6 | any;
23
+ propertyNames?: boolean | JSONSchema6 | any;
24
+ items?: (any | JSONSchema6Definition) | (any | JSONSchema6Definition)[];
25
+ }
26
+
27
+ export type AnyJsonSchema = Partial<JsonSchemaObject> | JsonSchema | JsonLazyRef | any;
28
+
29
+ function mapProperties(properties: Record<string, any>) {
30
+ // istanbul ignore next
31
+ if (properties instanceof JsonSchema) {
32
+ return properties;
33
+ }
34
+
35
+ return Object.entries(properties).reduce<any>((properties, [key, schema]) => {
36
+ properties[toJsonRegex(key)] = mapToJsonSchema(schema);
37
+
38
+ return properties;
39
+ }, {});
40
+ }
41
+
42
+ function mapToJsonSchema(item: any): any {
43
+ if (isArray(item)) {
44
+ return (item as any[]).map(mapToJsonSchema);
45
+ }
46
+
47
+ if (item.isStore || item.$isJsonDocument || item.isLazyRef) {
48
+ return item;
49
+ }
50
+
51
+ if (classOf(item) !== Object && isClass(item)) {
52
+ return JsonEntityStore.from(item).schema;
53
+ }
54
+
55
+ if (isObject(item)) {
56
+ return JsonSchema.from(item as any);
57
+ }
58
+
59
+ if (isPrimitiveClass(item)) {
60
+ return JsonSchema.from({type: item});
61
+ }
62
+
63
+ return item;
64
+ }
65
+
66
+ export class JsonSchema extends Map<string, any> implements NestedGenerics {
67
+ readonly $kind: string = "schema";
68
+ readonly $isJsonDocument = true;
69
+ readonly $hooks = new Hooks();
70
+ readonly $required: Set<string> = new Set();
71
+ readonly $allow: any[] = [];
72
+ public $selfRequired: boolean;
73
+ public $forwardGroups: boolean = false;
74
+ public $ignore: boolean | IgnoreCallback = false;
75
+
76
+ public isDiscriminatorKey = false;
77
+ public isDiscriminator = false;
78
+
79
+ #nullable: boolean = false;
80
+ #discriminator: null | Discriminator = null;
81
+ #genericLabels: string[];
82
+ #nestedGenerics: Type<any>[][] = [];
83
+ #alias: AliasMap = new Map();
84
+ #itemSchema: JsonSchema;
85
+ #target: Type<any>;
86
+ #isGeneric: boolean = false;
87
+ #isCollection: boolean = false;
88
+ #ref: boolean = false;
89
+
90
+ constructor(obj: JsonSchema | Partial<JsonSchemaObject> = {}) {
91
+ super();
92
+
93
+ if (obj) {
94
+ this.assign(obj);
95
+ }
96
+ }
97
+
98
+ get alias() {
99
+ return this.#alias;
100
+ }
101
+
102
+ get nestedGenerics(): any[] {
103
+ return this.#nestedGenerics;
104
+ }
105
+
106
+ set nestedGenerics(value: any[]) {
107
+ this.#nestedGenerics = value;
108
+ }
109
+
110
+ get genericLabels(): string[] {
111
+ return this.#genericLabels;
112
+ }
113
+
114
+ set genericLabels(value: string[]) {
115
+ this.#genericLabels = value;
116
+ }
117
+
118
+ get isClass() {
119
+ return isClass(this.class) && ![Map, Array, Set, Object, Date, Boolean, Number, String].includes(this.#target as any);
120
+ }
121
+
122
+ /**
123
+ * Current schema is a collection
124
+ */
125
+ get isCollection() {
126
+ return this.#isCollection;
127
+ }
128
+
129
+ /**
130
+ * Current schema is a generic
131
+ */
132
+ get isGeneric() {
133
+ return this.#isGeneric;
134
+ }
135
+
136
+ get discriminatorAncestor() {
137
+ const ancestors = ancestorsOf(this.#target);
138
+ const ancestor = ancestors.find((ancestor) => JsonEntityStore.from(ancestor).schema.isDiscriminator);
139
+ return ancestor && JsonEntityStore.from(ancestor).schema;
140
+ }
141
+
142
+ /**
143
+ * Current schema has generics items
144
+ */
145
+ get hasGenerics(): boolean {
146
+ return !!(this.nestedGenerics && this.nestedGenerics.length);
147
+ }
148
+
149
+ get genericType(): string {
150
+ return this.get("$ref");
151
+ }
152
+
153
+ get class() {
154
+ return this.getComputedType();
155
+ }
156
+
157
+ get canRef(): boolean {
158
+ return this.#ref;
159
+ }
160
+
161
+ get isNullable(): boolean {
162
+ return this.#nullable;
163
+ }
164
+
165
+ get isReadOnly() {
166
+ return this.get("readOnly");
167
+ }
168
+
169
+ get isWriteOnly() {
170
+ return this.get("writeOnly");
171
+ }
172
+
173
+ get hasDiscriminator() {
174
+ return !!this.#discriminator;
175
+ }
176
+
177
+ static from(obj: Partial<JsonSchemaObject> = {}) {
178
+ return new JsonSchema(obj);
179
+ }
180
+
181
+ nullable(value: boolean) {
182
+ this.#nullable = value;
183
+ }
184
+
185
+ itemSchema(obj: AnyJsonSchema = {}) {
186
+ this.#itemSchema = this.#itemSchema || mapToJsonSchema(obj);
187
+ this.#itemSchema.assign(obj);
188
+
189
+ return this.#itemSchema;
190
+ }
191
+
192
+ getAliasOf(property: AliasType) {
193
+ return this.#alias.get(property as any);
194
+ }
195
+
196
+ addAlias(property: AliasType, alias: AliasType) {
197
+ this.#alias.set(property, alias);
198
+ this.#alias.set(alias, property);
199
+
200
+ return this;
201
+ }
202
+
203
+ removeAlias(property: AliasType) {
204
+ const alias = this.#alias.get(property);
205
+ alias && this.#alias.delete(alias);
206
+ this.#alias.delete(property);
207
+
208
+ return this;
209
+ }
210
+
211
+ $id($id: string) {
212
+ super.set("$id", $id);
213
+
214
+ return this;
215
+ }
216
+
217
+ $ref($ref: string) {
218
+ super.set("$ref", $ref);
219
+
220
+ return this;
221
+ }
222
+
223
+ $schema($schema: JSONSchema6Version) {
224
+ super.set("$schema", $schema);
225
+
226
+ return this;
227
+ }
228
+
229
+ /**
230
+ * Create a ref and use name to sharing schema
231
+ * @param name
232
+ */
233
+ label(name: string) {
234
+ this.#ref = true;
235
+
236
+ super.set("name", name);
237
+
238
+ return this;
239
+ }
240
+
241
+ name(name: string) {
242
+ super.set("name", name);
243
+
244
+ return this;
245
+ }
246
+
247
+ ignore(cb: boolean | IgnoreCallback) {
248
+ if (typeof cb !== "boolean") {
249
+ this.$hooks.on("ignore", cb);
250
+ }
251
+
252
+ this.$ignore = cb;
253
+
254
+ return this;
255
+ }
256
+
257
+ /**
258
+ * This keyword can be used to supply a default JSON value associated with a particular schema.
259
+ * It is RECOMMENDED that a default value be valid against the associated schema.
260
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.3
261
+ */
262
+ default(value: JSONSchema6Type) {
263
+ super.set("default", value);
264
+
265
+ return this;
266
+ }
267
+
268
+ /**
269
+ * More readible form of a one-element "enum"
270
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.24
271
+ */
272
+ const(value: JSONSchema6Type) {
273
+ super.set("const", value);
274
+
275
+ return this;
276
+ }
277
+
278
+ /**
279
+ * This attribute is a string that provides a full description of the of purpose the instance property.
280
+ *
281
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.2
282
+ */
283
+ description(description: string) {
284
+ super.set("description", description);
285
+
286
+ return this;
287
+ }
288
+
289
+ discriminator() {
290
+ this.isDiscriminator = true;
291
+ return (this.#discriminator =
292
+ this.#discriminator ||
293
+ new Discriminator({
294
+ base: this.#target
295
+ }));
296
+ }
297
+
298
+ discriminatorKey(propertyName: string) {
299
+ this.discriminator().propertyName = propertyName;
300
+ this.isDiscriminator = true;
301
+
302
+ return this;
303
+ }
304
+
305
+ discriminatorValue(...values: string[]) {
306
+ const discriminator = this.discriminatorAncestor.discriminator();
307
+ discriminator.add(this.#target, values);
308
+
309
+ this.isDiscriminator = true;
310
+
311
+ const properties = this.get("properties");
312
+ const schema: JsonSchema =
313
+ properties[discriminator.propertyName] ||
314
+ new JsonSchema({
315
+ type: "string"
316
+ });
317
+
318
+ if (values.length === 1) {
319
+ schema.const(values[0]);
320
+ schema.examples([values[0]]);
321
+ } else {
322
+ schema.enum(...values);
323
+ schema.examples(values);
324
+ }
325
+
326
+ properties[discriminator.propertyName] = schema;
327
+
328
+ this.set("properties", properties);
329
+
330
+ return this;
331
+ }
332
+
333
+ /**
334
+ * This keyword determines how child instances validate for arrays, and does not directly validate the immediate instance itself.
335
+ * If "items" is an array of schemas, validation succeeds if every instance element
336
+ * at a position greater than the size of "items" validates against "additionalItems".
337
+ * Otherwise, "additionalItems" MUST be ignored, as the "items" schema
338
+ * (possibly the default value of an empty schema) is applied to all elements.
339
+ * Omitting this keyword has the same behavior as an empty schema.
340
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.10
341
+ */
342
+ additionalItems(additionalItems: boolean | AnyJsonSchema) {
343
+ super.set("additionalItems", mapToJsonSchema(additionalItems));
344
+
345
+ return this;
346
+ }
347
+
348
+ /**
349
+ * An array instance is valid against "contains" if at least one of its elements is valid against the given schema.
350
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.14
351
+ */
352
+ contains(contains: JSONSchema6Definition) {
353
+ super.set("contains", mapToJsonSchema(contains));
354
+
355
+ return this;
356
+ }
357
+
358
+ /**
359
+ * Array of examples with no validation effect the value of "default" is usable as an example without repeating it under this keyword
360
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.4
361
+ */
362
+ examples(examples: JSONSchema6Type[]) {
363
+ super.set("examples", examples);
364
+
365
+ return this;
366
+ }
367
+
368
+ /**
369
+ * Array of examples with no validation effect the value of "default" is usable as an example without repeating it under this keyword
370
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.4
371
+ */
372
+ example(...examples: JSONSchema6Type[]) {
373
+ return this.examples(examples);
374
+ }
375
+
376
+ /**
377
+ * This keyword determines how child instances validate for arrays, and does not directly validate the immediate instance itself.
378
+ * Omitting this keyword has the same behavior as an empty schema.
379
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.9
380
+ */
381
+ items(items: AnyJsonSchema | AnyJsonSchema[]) {
382
+ super.set("items", (this.#itemSchema = mapToJsonSchema(items)));
383
+
384
+ return this;
385
+ }
386
+
387
+ /**
388
+ * Must be a non-negative integer.
389
+ * An array instance is valid against "maxItems" if its size is less than, or equal to, the value of this keyword.
390
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.11
391
+ */
392
+ maxItems(maxItems: number) {
393
+ super.set("maxItems", maxItems);
394
+
395
+ return this;
396
+ }
397
+
398
+ /**
399
+ * Must be a non-negative integer.
400
+ * An array instance is valid against "maxItems" if its size is greater than, or equal to, the value of this keyword.
401
+ * Omitting this keyword has the same behavior as a value of 0.
402
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.12
403
+ */
404
+ minItems(minItems: number) {
405
+ super.set("minItems", minItems);
406
+
407
+ return this;
408
+ }
409
+
410
+ /**
411
+ * If this keyword has boolean value false, the instance validates successfully.
412
+ * If it has boolean value true, the instance validates successfully if all of its elements are unique.
413
+ * Omitting this keyword has the same behavior as a value of false.
414
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.13
415
+ */
416
+ uniqueItems(uniqueItems: boolean) {
417
+ super.set("uniqueItems", uniqueItems);
418
+
419
+ return this;
420
+ }
421
+
422
+ /**
423
+ * Must be a non-negative integer.
424
+ * An object instance is valid against "maxProperties" if its number of properties is less than, or equal to, the value of this keyword.
425
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.15
426
+ */
427
+ maxProperties(maxProperties: number) {
428
+ super.set("maxProperties", maxProperties);
429
+
430
+ return this;
431
+ }
432
+
433
+ /**
434
+ * Must be a non-negative integer.
435
+ * An object instance is valid against "maxProperties" if its number of properties is greater than,
436
+ * or equal to, the value of this keyword.
437
+ * Omitting this keyword has the same behavior as a value of 0.
438
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.16
439
+ */
440
+ minProperties(minProperties: number) {
441
+ super.set("minProperties", minProperties);
442
+
443
+ return this;
444
+ }
445
+
446
+ allow(...allow: any[]) {
447
+ if (([] as string[]).concat(this.getJsonType()).includes("string") && !this.has("minLength")) {
448
+ this.minLength(1);
449
+ }
450
+
451
+ allow.forEach((value) => {
452
+ switch (value) {
453
+ case "":
454
+ this.set("minLength", undefined);
455
+ break;
456
+ case null:
457
+ this.any(
458
+ ...["null"].concat(
459
+ this.getJsonType(),
460
+ this.$allow.map((v) => typeof v)
461
+ )
462
+ );
463
+ break;
464
+ }
465
+ });
466
+
467
+ this.$allow.push(...allow);
468
+
469
+ return this;
470
+ }
471
+
472
+ /**
473
+ * Elements of this array must be unique.
474
+ * An object instance is valid against this keyword if every item in the array is the name of a property in the instance.
475
+ * Omitting this keyword has the same behavior as an empty array.
476
+ *
477
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.17
478
+ */
479
+ required(required: boolean | string[] = true) {
480
+ if (isArray(required)) {
481
+ this.$required.clear();
482
+
483
+ required.forEach((value: any) => this.$required.add(value));
484
+ } else {
485
+ const schema = this.clone();
486
+ schema.$selfRequired = required;
487
+
488
+ if (([] as string[]).concat(schema.getJsonType()).includes("string")) {
489
+ schema.minLength(1);
490
+ }
491
+
492
+ return schema;
493
+ }
494
+
495
+ return this;
496
+ }
497
+
498
+ addRequired(property: string) {
499
+ this.$required.add(property);
500
+
501
+ return this;
502
+ }
503
+
504
+ removeRequired(property: string) {
505
+ this.$required.delete(property);
506
+
507
+ return this;
508
+ }
509
+
510
+ isRequired(property: string): boolean {
511
+ return this.$required.has(property);
512
+ }
513
+
514
+ /**
515
+ * This keyword determines how child instances validate for objects, and does not directly validate the immediate instance itself.
516
+ * Validation succeeds if, for each name that appears in both the instance and as a name within this keyword's value,
517
+ * the child instance for that name successfully validates against the corresponding schema.
518
+ * Omitting this keyword has the same behavior as an empty object.
519
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.18
520
+ */
521
+ properties(properties: AnyJsonSchema | Record<string, AnyJsonSchema>) {
522
+ super.set("properties", mapProperties(properties));
523
+
524
+ return this;
525
+ }
526
+
527
+ addProperty(key: string, schema: AnyJsonSchema) {
528
+ const properties = this.get("properties") || {};
529
+
530
+ properties[key] = schema;
531
+
532
+ super.set("properties", mapProperties(properties));
533
+
534
+ return this;
535
+ }
536
+
537
+ /**
538
+ * This attribute is an object that defines the schema for a set of property names of an object instance.
539
+ * The name of each property of this attribute's object is a regular expression pattern in the ECMA 262, while the value is a schema.
540
+ * If the pattern matches the name of a property on the instance object, the value of the instance's property
541
+ * MUST be valid against the pattern name's schema value.
542
+ * Omitting this keyword has the same behavior as an empty object.
543
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.19
544
+ */
545
+ patternProperties(patternProperties: Record<string, AnyJsonSchema>) {
546
+ super.set("patternProperties", mapProperties(patternProperties));
547
+
548
+ return this;
549
+ }
550
+
551
+ /**
552
+ * This attribute defines a schema for all properties that are not explicitly defined in an object type definition.
553
+ * If specified, the value MUST be a schema or a boolean.
554
+ * If false is provided, no additional properties are allowed beyond the properties defined in the schema.
555
+ * The default value is an empty schema which allows any value for additional properties.
556
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.20
557
+ */
558
+ additionalProperties(additionalProperties: boolean | AnyJsonSchema) {
559
+ super.set("additionalProperties", mapToJsonSchema(additionalProperties));
560
+
561
+ return this;
562
+ }
563
+
564
+ /**
565
+ * This attribute defines a schema for all properties that are not explicitly defined in an object type definition.
566
+ * If specified, the value MUST be a schema or a boolean.
567
+ * If false is provided, no additional properties are allowed beyond the properties defined in the schema.
568
+ * The default value is an empty schema which allows any value for additional properties.
569
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.20
570
+ * @alias additionalProperties
571
+ * @param unknown
572
+ */
573
+ unknown(unknown: boolean = true) {
574
+ return this.additionalProperties(unknown);
575
+ }
576
+
577
+ /**
578
+ * This keyword specifies rules that are evaluated if the instance is an object and contains a certain property.
579
+ * Each property specifies a dependency.
580
+ * If the dependency value is an array, each element in the array must be unique.
581
+ * Omitting this keyword has the same behavior as an empty object.
582
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.21
583
+ */
584
+ dependencies(dependencies: {[p: string]: JSONSchema6Definition | JsonSchema | string[]}) {
585
+ super.set("dependencies", mapProperties(dependencies));
586
+
587
+ return this;
588
+ }
589
+
590
+ /**
591
+ * Takes a schema which validates the names of all properties rather than their values.
592
+ * Note the property name that the schema is testing will always be a string.
593
+ * Omitting this keyword has the same behavior as an empty schema.
594
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.22
595
+ */
596
+ propertyNames(propertyNames: JSONSchema6Definition | JsonSchema) {
597
+ super.set("propertyNames", mapToJsonSchema(propertyNames));
598
+
599
+ return this;
600
+ }
601
+
602
+ /**
603
+ * This provides an enumeration of all possible values that are valid
604
+ * for the instance property. This MUST be an array, and each item in
605
+ * the array represents a possible value for the instance value. If
606
+ * this attribute is defined, the instance value MUST be one of the
607
+ * values in the array in order for the schema to be valid.
608
+ *
609
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.23
610
+ */
611
+ enum(...enumValues: any[]): this;
612
+ enum(enumSchema: JsonSchema): this;
613
+ enum(enumValue: any | any[] | JsonSchema, ...enumValues: any[]): this {
614
+ if (enumsRegistry.has(enumValue)) {
615
+ return this.enum(enumsRegistry.get(enumValue));
616
+ }
617
+
618
+ if (enumValue instanceof JsonSchema) {
619
+ if (enumValue.getName()) {
620
+ super.set("enum", enumValue);
621
+ } else {
622
+ super.set("enum", enumValue.get("enum")).any(...enumValue.get("enum").map((value: any) => typeof value));
623
+ }
624
+ } else {
625
+ const {values, types} = serializeEnumValues([enumValue, enumValues].flat());
626
+
627
+ super.set("enum", values).any(...types);
628
+ }
629
+
630
+ return this;
631
+ }
632
+
633
+ /**
634
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.1
635
+ */
636
+ definitions(definitions: Record<string, AnyJsonSchema>) {
637
+ super.set("definitions", mapProperties(definitions));
638
+
639
+ return this;
640
+ }
641
+
642
+ /**
643
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.26
644
+ */
645
+ allOf(allOf: AnyJsonSchema[]) {
646
+ this.setManyOf("allOf", allOf);
647
+
648
+ return this;
649
+ }
650
+
651
+ /**
652
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.27
653
+ */
654
+ anyOf(anyOf: AnyJsonSchema[]) {
655
+ this.setManyOf("anyOf", anyOf);
656
+
657
+ return this;
658
+ }
659
+
660
+ /*
661
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.28
662
+ */
663
+ oneOf(oneOf: AnyJsonSchema[]) {
664
+ this.setManyOf("oneOf", oneOf);
665
+
666
+ return this;
667
+ }
668
+
669
+ /**
670
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.29
671
+ */
672
+ not(not: AnyJsonSchema) {
673
+ super.set("not", mapToJsonSchema(not));
674
+
675
+ return this;
676
+ }
677
+
678
+ /**
679
+ * Must be strictly greater than 0.
680
+ * A numeric instance is valid only if division by this keyword's value results in an integer.
681
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.1
682
+ */
683
+ multipleOf(multipleOf: number): this {
684
+ super.set("multipleOf", multipleOf);
685
+
686
+ return this;
687
+ }
688
+
689
+ /**
690
+ * Representing an inclusive upper limit for a numeric instance.
691
+ * This keyword validates only if the instance is less than or exactly equal to "maximum".
692
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.2
693
+ */
694
+ maximum(maximum: number): this {
695
+ super.set("maximum", maximum);
696
+
697
+ return this;
698
+ }
699
+
700
+ /**
701
+ * Representing an exclusive upper limit for a numeric instance.
702
+ * This keyword validates only if the instance is strictly less than (not equal to) to "exclusiveMaximum".
703
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.3
704
+ */
705
+ exclusiveMaximum(exclusiveMaximum: number): this {
706
+ super.set("exclusiveMaximum", exclusiveMaximum);
707
+
708
+ return this;
709
+ }
710
+
711
+ /**
712
+ * Representing an inclusive lower limit for a numeric instance.
713
+ * This keyword validates only if the instance is greater than or exactly equal to "minimum".
714
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.4
715
+ */
716
+ minimum(minimum: number): this {
717
+ super.set("minimum", minimum);
718
+
719
+ return this;
720
+ }
721
+
722
+ /**
723
+ * Representing an exclusive lower limit for a numeric instance.
724
+ * This keyword validates only if the instance is strictly greater than (not equal to) to "exclusiveMinimum".
725
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.5
726
+ */
727
+ exclusiveMinimum(exclusiveMinimum: number): this {
728
+ super.set("exclusiveMinimum", exclusiveMinimum);
729
+
730
+ return this;
731
+ }
732
+
733
+ /**
734
+ * Must be a non-negative integer.
735
+ * A string instance is valid against this keyword if its length is less than, or equal to, the value of this keyword.
736
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.6
737
+ */
738
+ maxLength(maxLength: number): this {
739
+ super.set("maxLength", maxLength);
740
+
741
+ return this;
742
+ }
743
+
744
+ /**
745
+ * Must be a non-negative integer.
746
+ * A string instance is valid against this keyword if its length is greater than, or equal to, the value of this keyword.
747
+ * Omitting this keyword has the same behavior as a value of 0.
748
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.7
749
+ */
750
+ minLength(minLength: number): this {
751
+ super.set("minLength", minLength);
752
+
753
+ return this;
754
+ }
755
+
756
+ /**
757
+ * Should be a valid regular expression, according to the ECMA 262 regular expression dialect.
758
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.8
759
+ */
760
+ pattern(pattern: string | RegExp): this {
761
+ super.set("pattern", toJsonRegex(pattern));
762
+
763
+ return this;
764
+ }
765
+
766
+ /**
767
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-8
768
+ */
769
+ format(format: JsonFormatTypes | ValueOf<JsonFormatTypes>): this {
770
+ super.set("format", format);
771
+
772
+ return this;
773
+ }
774
+
775
+ /**
776
+ * A single type, or a union of simple types
777
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.25
778
+ */
779
+ type(type: any | JSONSchema6TypeName | JSONSchema6TypeName[]): this {
780
+ switch (type) {
781
+ case "map":
782
+ case Map:
783
+ super.set("type", getJsonType(type));
784
+ this.#target = type;
785
+ this.#isCollection = true;
786
+ if (!this.has("additionalProperties")) {
787
+ super.set("additionalProperties", this.itemSchema({}));
788
+ }
789
+ break;
790
+
791
+ case "array":
792
+ case Array:
793
+ super.set("type", getJsonType(type));
794
+ this.#target = type;
795
+ this.#isCollection = true;
796
+
797
+ if (!this.has("items")) {
798
+ super.set("items", this.itemSchema({}));
799
+ }
800
+ break;
801
+
802
+ case "set":
803
+ case Set:
804
+ super.set("type", getJsonType(type));
805
+ this.#target = type;
806
+ this.#isCollection = true;
807
+ this.uniqueItems(true);
808
+
809
+ if (!this.has("items")) {
810
+ super.set("items", this.itemSchema({}));
811
+ }
812
+ break;
813
+
814
+ case "integer":
815
+ this.integer();
816
+ break;
817
+
818
+ case "number":
819
+ case "string":
820
+ case "boolean":
821
+ case "object":
822
+ case Object:
823
+ case Date:
824
+ case Boolean:
825
+ case Number:
826
+ case String:
827
+ super.set("type", getJsonType(type));
828
+ this.#target = type;
829
+ if (!this.has("properties")) {
830
+ super.set("properties", {});
831
+ }
832
+ break;
833
+
834
+ default:
835
+ if (isClass(type) || isFunction(type)) {
836
+ super.set("type", undefined);
837
+ this.#target = type;
838
+
839
+ if (!this.has("properties")) {
840
+ super.set("properties", {});
841
+ }
842
+ } else {
843
+ const jsonType = getJsonType(type);
844
+ if (jsonType === "generic") {
845
+ this.#isGeneric = true;
846
+ super.set("$ref", type);
847
+ } else {
848
+ super.set("type", jsonType);
849
+ }
850
+ }
851
+ }
852
+
853
+ return this;
854
+ }
855
+
856
+ any(...types: any[]) {
857
+ types = types.length ? types : ["integer", "number", "string", "boolean", "array", "object", "null"];
858
+
859
+ types = uniq(types).map((o) => {
860
+ return isClass(o) ? o : {type: getJsonType(o)};
861
+ });
862
+
863
+ if (types.length > 1) {
864
+ this.anyOf(types);
865
+ } else {
866
+ this.type(types[0]?.type || types[0]);
867
+ }
868
+
869
+ return this;
870
+ }
871
+
872
+ integer() {
873
+ super.set("type", "integer");
874
+ super.set("multipleOf", 1.0);
875
+
876
+ return this;
877
+ }
878
+
879
+ /**
880
+ * This attribute is a string that provides a short description of the instance property.
881
+ *
882
+ * @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-7.2
883
+ */
884
+ title(title: string): this {
885
+ super.set("title", title);
886
+
887
+ return this;
888
+ }
889
+
890
+ readOnly(readOnly: boolean): this {
891
+ super.set("readOnly", readOnly);
892
+
893
+ return this;
894
+ }
895
+
896
+ writeOnly(readOnly: boolean): this {
897
+ super.set("writeOnly", readOnly);
898
+
899
+ return this;
900
+ }
901
+
902
+ customKey(key: string, value: any) {
903
+ super.set(`#${key}`, value);
904
+
905
+ return this;
906
+ }
907
+
908
+ toObject(options?: JsonSchemaOptions) {
909
+ return this.toJSON(options);
910
+ }
911
+
912
+ toJSON(options?: JsonSchemaOptions) {
913
+ return execMapper("schema", [this], options);
914
+ }
915
+
916
+ assign(obj: JsonSchema | Partial<JsonSchemaObject> = {}) {
917
+ const entries = obj instanceof JsonSchema ? [...obj.entries()] : Object.entries(obj);
918
+ entries.forEach(([key, value]) => {
919
+ this.set(key, value);
920
+ });
921
+
922
+ if (obj instanceof JsonSchema) {
923
+ this.$selfRequired = obj.$selfRequired;
924
+ this.$allow.push(...obj.$allow);
925
+
926
+ obj.$required.forEach((key) => {
927
+ this.$required.add(key);
928
+ });
929
+
930
+ this.#discriminator = this.#discriminator ? new Discriminator(this.#discriminator) : null;
931
+ this.isDiscriminator = obj.isDiscriminator;
932
+ this.isDiscriminatorKey = obj.isDiscriminatorKey;
933
+ this.#ref = obj.#ref;
934
+
935
+ this.#alias = new Map(this.#alias.entries());
936
+ obj.#genericLabels && (this.#genericLabels = [...obj.#genericLabels]);
937
+ this.#nestedGenerics = obj.#nestedGenerics.map((item) => [...item]);
938
+ this.#target = obj.#target;
939
+ this.#isGeneric = obj.#isGeneric;
940
+ this.#isCollection = obj.#isCollection;
941
+ this.#ref = obj.#ref;
942
+ this.#nullable = obj.#nullable;
943
+
944
+ super.set("type", obj.get("type"));
945
+ }
946
+
947
+ return this;
948
+ }
949
+
950
+ set(key: string, value: any): this {
951
+ if (key in this) {
952
+ isFunction((this as any)[key]) && (this as any)[key](value);
953
+ } else {
954
+ super.set(key, value);
955
+ }
956
+
957
+ return this;
958
+ }
959
+
960
+ /**
961
+ * Return the itemSchema computed type.
962
+ * If the type is a function used for recursive model,
963
+ * the function will be called to get the right type.
964
+ */
965
+ getComputedType(): any {
966
+ return getComputedType(this.#target);
967
+ }
968
+
969
+ getComputedItemType(): any {
970
+ return this.#itemSchema ? this.#itemSchema.getComputedType() : this.getComputedType();
971
+ }
972
+
973
+ /**
974
+ * Return the Json type as string
975
+ */
976
+ getJsonType(): string | string[] {
977
+ if (this.get("anyOf")) {
978
+ return this.get("anyOf").map((o: JsonSchema) => {
979
+ return o.getJsonType();
980
+ });
981
+ }
982
+
983
+ return this.get("type") || getJsonType(this.getComputedType());
984
+ }
985
+
986
+ getTarget() {
987
+ return this.#target;
988
+ }
989
+
990
+ /**
991
+ * Get the symbolic name of the entity
992
+ */
993
+ getName() {
994
+ return this.get("name") || (isClass(this.#target) ? nameOf(classOf(this.getComputedType())) : "");
995
+ }
996
+
997
+ clone() {
998
+ return new JsonSchema(this);
999
+ }
1000
+
1001
+ protected setManyOf(keyword: "oneOf" | "anyOf" | "allOf", value: AnyJsonSchema[]) {
1002
+ let resolved = value
1003
+ .filter((o) => {
1004
+ if (o?.type === "null") {
1005
+ this.nullable(true);
1006
+ return false;
1007
+ }
1008
+ return true;
1009
+ })
1010
+ .map(mapToJsonSchema);
1011
+
1012
+ if (resolved.length === 1 && !(value[0] instanceof JsonSchema) && !this.isNullable) {
1013
+ if (!resolved[0].hasDiscriminator) {
1014
+ return this.type(value[0]);
1015
+ }
1016
+
1017
+ const children = resolved[0].discriminator().children();
1018
+
1019
+ if (!children.length) {
1020
+ return this.type(value[0]);
1021
+ }
1022
+
1023
+ resolved = children.map(mapToJsonSchema);
1024
+ }
1025
+
1026
+ super.set(keyword, resolved);
1027
+
1028
+ const jsonSchema: JsonSchema = resolved[0];
1029
+
1030
+ if (jsonSchema.isDiscriminator) {
1031
+ const discriminator = jsonSchema.discriminatorAncestor.discriminator();
1032
+ const {propertyName} = discriminator;
1033
+
1034
+ super.set("discriminator", {
1035
+ propertyName,
1036
+ mapping: resolved.reduce((acc, schema: JsonSchema) => {
1037
+ discriminator.types.get(schema.getTarget())?.forEach((key: string) => {
1038
+ acc[key] = schema;
1039
+ });
1040
+
1041
+ return acc;
1042
+ }, {})
1043
+ });
1044
+
1045
+ this.isDiscriminator = true;
1046
+ this.#discriminator = discriminator;
1047
+ }
1048
+ }
1049
+ }