@allgemein/schema-api 1.5.0

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 (228) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +34 -0
  3. package/api/IBaseRef.d.ts +44 -0
  4. package/api/IBaseRef.js +4 -0
  5. package/api/IBaseRef.js.map +1 -0
  6. package/api/IBuildOptions.d.ts +31 -0
  7. package/api/IBuildOptions.js +4 -0
  8. package/api/IBuildOptions.js.map +1 -0
  9. package/api/IClassRef.d.ts +24 -0
  10. package/api/IClassRef.js +13 -0
  11. package/api/IClassRef.js.map +1 -0
  12. package/api/IEntityRef.d.ts +16 -0
  13. package/api/IEntityRef.js +11 -0
  14. package/api/IEntityRef.js.map +1 -0
  15. package/api/IJsonSchema.d.ts +11 -0
  16. package/api/IJsonSchema.js +26 -0
  17. package/api/IJsonSchema.js.map +1 -0
  18. package/api/ILookupRegistry.d.ts +60 -0
  19. package/api/ILookupRegistry.js +17 -0
  20. package/api/ILookupRegistry.js.map +1 -0
  21. package/api/IPropertyExtentions.d.ts +8 -0
  22. package/api/IPropertyExtentions.js +5 -0
  23. package/api/IPropertyExtentions.js.map +1 -0
  24. package/api/IPropertyRef.d.ts +28 -0
  25. package/api/IPropertyRef.js +13 -0
  26. package/api/IPropertyRef.js.map +1 -0
  27. package/api/ISchemaRef.d.ts +8 -0
  28. package/api/ISchemaRef.js +4 -0
  29. package/api/ISchemaRef.js.map +1 -0
  30. package/api/ISerializeSupport.d.ts +11 -0
  31. package/api/ISerializeSupport.js +26 -0
  32. package/api/ISerializeSupport.js.map +1 -0
  33. package/browser.d.ts +62 -0
  34. package/browser.js +117 -0
  35. package/browser.js.map +1 -0
  36. package/decorators/Embeddable.d.ts +2 -0
  37. package/decorators/Embeddable.js +14 -0
  38. package/decorators/Embeddable.js.map +1 -0
  39. package/decorators/Entity.d.ts +2 -0
  40. package/decorators/Entity.js +14 -0
  41. package/decorators/Entity.js.map +1 -0
  42. package/decorators/Namespace.d.ts +1 -0
  43. package/decorators/Namespace.js +20 -0
  44. package/decorators/Namespace.js.map +1 -0
  45. package/decorators/Property.d.ts +3 -0
  46. package/decorators/Property.js +59 -0
  47. package/decorators/Property.js.map +1 -0
  48. package/decorators/PropertyOf.d.ts +2 -0
  49. package/decorators/PropertyOf.js +28 -0
  50. package/decorators/PropertyOf.js.map +1 -0
  51. package/decorators/Schema.d.ts +2 -0
  52. package/decorators/Schema.js +14 -0
  53. package/decorators/Schema.js.map +1 -0
  54. package/decorators/validate/Hostname.d.ts +6 -0
  55. package/decorators/validate/Hostname.js +38 -0
  56. package/decorators/validate/Hostname.js.map +1 -0
  57. package/decorators/validate/Ip4.d.ts +6 -0
  58. package/decorators/validate/Ip4.js +37 -0
  59. package/decorators/validate/Ip4.js.map +1 -0
  60. package/decorators/validate/Ip6.d.ts +5 -0
  61. package/decorators/validate/Ip6.js +37 -0
  62. package/decorators/validate/Ip6.js.map +1 -0
  63. package/decorators/validate/IsEmail.d.ts +5 -0
  64. package/decorators/validate/IsEmail.js +37 -0
  65. package/decorators/validate/IsEmail.js.map +1 -0
  66. package/decorators/validate/IsNotEmpty.d.ts +5 -0
  67. package/decorators/validate/IsNotEmpty.js +36 -0
  68. package/decorators/validate/IsNotEmpty.js.map +1 -0
  69. package/decorators/validate/MaxLength.d.ts +6 -0
  70. package/decorators/validate/MaxLength.js +34 -0
  71. package/decorators/validate/MaxLength.js.map +1 -0
  72. package/decorators/validate/MinLength.d.ts +6 -0
  73. package/decorators/validate/MinLength.js +34 -0
  74. package/decorators/validate/MinLength.js.map +1 -0
  75. package/decorators/validate/Regex.d.ts +7 -0
  76. package/decorators/validate/Regex.js +46 -0
  77. package/decorators/validate/Regex.js.map +1 -0
  78. package/decorators/validate/Required.d.ts +5 -0
  79. package/decorators/validate/Required.js +34 -0
  80. package/decorators/validate/Required.js.map +1 -0
  81. package/decorators/validate/index.d.ts +9 -0
  82. package/decorators/validate/index.js +21 -0
  83. package/decorators/validate/index.js.map +1 -0
  84. package/index.d.ts +1 -0
  85. package/index.js +15 -0
  86. package/index.js.map +1 -0
  87. package/lib/AbstractRef.d.ts +26 -0
  88. package/lib/AbstractRef.js +126 -0
  89. package/lib/AbstractRef.js.map +1 -0
  90. package/lib/AnnotationsHelper.d.ts +7 -0
  91. package/lib/AnnotationsHelper.js +97 -0
  92. package/lib/AnnotationsHelper.js.map +1 -0
  93. package/lib/ClassRef.d.ts +120 -0
  94. package/lib/ClassRef.js +319 -0
  95. package/lib/ClassRef.js.map +1 -0
  96. package/lib/Constants.d.ts +45 -0
  97. package/lib/Constants.js +65 -0
  98. package/lib/Constants.js.map +1 -0
  99. package/lib/DataContainer.d.ts +31 -0
  100. package/lib/DataContainer.js +148 -0
  101. package/lib/DataContainer.js.map +1 -0
  102. package/lib/LookupRegistry.d.ts +36 -0
  103. package/lib/LookupRegistry.js +106 -0
  104. package/lib/LookupRegistry.js.map +1 -0
  105. package/lib/SchemaRef.d.ts +14 -0
  106. package/lib/SchemaRef.js +51 -0
  107. package/lib/SchemaRef.js.map +1 -0
  108. package/lib/SchemaUtils.d.ts +39 -0
  109. package/lib/SchemaUtils.js +129 -0
  110. package/lib/SchemaUtils.js.map +1 -0
  111. package/lib/json-schema/Constants.d.ts +2 -0
  112. package/lib/json-schema/Constants.js +7 -0
  113. package/lib/json-schema/Constants.js.map +1 -0
  114. package/lib/json-schema/IJsonSchemaSerializeOptions.d.ts +40 -0
  115. package/lib/json-schema/IJsonSchemaSerializeOptions.js +4 -0
  116. package/lib/json-schema/IJsonSchemaSerializeOptions.js.map +1 -0
  117. package/lib/json-schema/IJsonSchemaSerializer.d.ts +7 -0
  118. package/lib/json-schema/IJsonSchemaSerializer.js +4 -0
  119. package/lib/json-schema/IJsonSchemaSerializer.js.map +1 -0
  120. package/lib/json-schema/IJsonSchemaUnserializeOptions.d.ts +41 -0
  121. package/lib/json-schema/IJsonSchemaUnserializeOptions.js +4 -0
  122. package/lib/json-schema/IJsonSchemaUnserializeOptions.js.map +1 -0
  123. package/lib/json-schema/IJsonSchemaUnserializer.d.ts +5 -0
  124. package/lib/json-schema/IJsonSchemaUnserializer.js +4 -0
  125. package/lib/json-schema/IJsonSchemaUnserializer.js.map +1 -0
  126. package/lib/json-schema/IParseOptions.d.ts +16 -0
  127. package/lib/json-schema/IParseOptions.js +6 -0
  128. package/lib/json-schema/IParseOptions.js.map +1 -0
  129. package/lib/json-schema/JsonSchema.d.ts +22 -0
  130. package/lib/json-schema/JsonSchema.js +109 -0
  131. package/lib/json-schema/JsonSchema.js.map +1 -0
  132. package/lib/json-schema/JsonSchema7.d.ts +16 -0
  133. package/lib/json-schema/JsonSchema7.js +22 -0
  134. package/lib/json-schema/JsonSchema7.js.map +1 -0
  135. package/lib/json-schema/JsonSchema7Serializer.d.ts +42 -0
  136. package/lib/json-schema/JsonSchema7Serializer.js +438 -0
  137. package/lib/json-schema/JsonSchema7Serializer.js.map +1 -0
  138. package/lib/json-schema/JsonSchema7Unserializer.d.ts +60 -0
  139. package/lib/json-schema/JsonSchema7Unserializer.js +499 -0
  140. package/lib/json-schema/JsonSchema7Unserializer.js.map +1 -0
  141. package/lib/options/IAbstractOptions.d.ts +19 -0
  142. package/lib/options/IAbstractOptions.js +4 -0
  143. package/lib/options/IAbstractOptions.js.map +1 -0
  144. package/lib/options/IAttributeOptions.d.ts +7 -0
  145. package/lib/options/IAttributeOptions.js +4 -0
  146. package/lib/options/IAttributeOptions.js.map +1 -0
  147. package/lib/options/IEntityOptions.d.ts +4 -0
  148. package/lib/options/IEntityOptions.js +4 -0
  149. package/lib/options/IEntityOptions.js.map +1 -0
  150. package/lib/options/IObjectOptions.d.ts +4 -0
  151. package/lib/options/IObjectOptions.js +4 -0
  152. package/lib/options/IObjectOptions.js.map +1 -0
  153. package/lib/options/IPropertyOptions.d.ts +24 -0
  154. package/lib/options/IPropertyOptions.js +4 -0
  155. package/lib/options/IPropertyOptions.js.map +1 -0
  156. package/lib/options/ISchemaOptions.d.ts +4 -0
  157. package/lib/options/ISchemaOptions.js +4 -0
  158. package/lib/options/ISchemaOptions.js.map +1 -0
  159. package/lib/registry/AbstractRegistry.d.ts +91 -0
  160. package/lib/registry/AbstractRegistry.js +135 -0
  161. package/lib/registry/AbstractRegistry.js.map +1 -0
  162. package/lib/registry/Binding.d.ts +9 -0
  163. package/lib/registry/Binding.js +17 -0
  164. package/lib/registry/Binding.js.map +1 -0
  165. package/lib/registry/DefaultEntityRef.d.ts +30 -0
  166. package/lib/registry/DefaultEntityRef.js +60 -0
  167. package/lib/registry/DefaultEntityRef.js.map +1 -0
  168. package/lib/registry/DefaultNamespacedRegistry.d.ts +153 -0
  169. package/lib/registry/DefaultNamespacedRegistry.js +511 -0
  170. package/lib/registry/DefaultNamespacedRegistry.js.map +1 -0
  171. package/lib/registry/DefaultPropertyRef.d.ts +44 -0
  172. package/lib/registry/DefaultPropertyRef.js +175 -0
  173. package/lib/registry/DefaultPropertyRef.js.map +1 -0
  174. package/lib/registry/MetadataRegistry.d.ts +52 -0
  175. package/lib/registry/MetadataRegistry.js +186 -0
  176. package/lib/registry/MetadataRegistry.js.map +1 -0
  177. package/lib/registry/RegistryFactory.d.ts +43 -0
  178. package/lib/registry/RegistryFactory.js +109 -0
  179. package/lib/registry/RegistryFactory.js.map +1 -0
  180. package/lib/serializer/ISerializeOptions.d.ts +10 -0
  181. package/lib/serializer/ISerializeOptions.js +4 -0
  182. package/lib/serializer/ISerializeOptions.js.map +1 -0
  183. package/lib/serializer/ISerializer.d.ts +5 -0
  184. package/lib/serializer/ISerializer.js +4 -0
  185. package/lib/serializer/ISerializer.js.map +1 -0
  186. package/lib/serializer/ISerializerFactory.d.ts +13 -0
  187. package/lib/serializer/ISerializerFactory.js +4 -0
  188. package/lib/serializer/ISerializerFactory.js.map +1 -0
  189. package/lib/serializer/IUnserializeOptions.d.ts +14 -0
  190. package/lib/serializer/IUnserializeOptions.js +4 -0
  191. package/lib/serializer/IUnserializeOptions.js.map +1 -0
  192. package/lib/serializer/IUnserializer.d.ts +5 -0
  193. package/lib/serializer/IUnserializer.js +4 -0
  194. package/lib/serializer/IUnserializer.js.map +1 -0
  195. package/lib/serializer/SerializerRegistry.d.ts +8 -0
  196. package/lib/serializer/SerializerRegistry.js +20 -0
  197. package/lib/serializer/SerializerRegistry.js.map +1 -0
  198. package/lib/validation/DefaultValidator.d.ts +14 -0
  199. package/lib/validation/DefaultValidator.js +221 -0
  200. package/lib/validation/DefaultValidator.js.map +1 -0
  201. package/lib/validation/IValidateOptions.d.ts +4 -0
  202. package/lib/validation/IValidateOptions.js +4 -0
  203. package/lib/validation/IValidateOptions.js.map +1 -0
  204. package/lib/validation/IValidationError.d.ts +10 -0
  205. package/lib/validation/IValidationError.js +4 -0
  206. package/lib/validation/IValidationError.js.map +1 -0
  207. package/lib/validation/IValidationMessage.d.ts +4 -0
  208. package/lib/validation/IValidationMessage.js +4 -0
  209. package/lib/validation/IValidationMessage.js.map +1 -0
  210. package/lib/validation/IValidationOptions.d.ts +1 -0
  211. package/lib/validation/IValidationOptions.js +30 -0
  212. package/lib/validation/IValidationOptions.js.map +1 -0
  213. package/lib/validation/IValidationResult.d.ts +7 -0
  214. package/lib/validation/IValidationResult.js +4 -0
  215. package/lib/validation/IValidationResult.js.map +1 -0
  216. package/lib/validation/IValidator.d.ts +9 -0
  217. package/lib/validation/IValidator.js +4 -0
  218. package/lib/validation/IValidator.js.map +1 -0
  219. package/lib/validation/IValidatorEntry.d.ts +7 -0
  220. package/lib/validation/IValidatorEntry.js +4 -0
  221. package/lib/validation/IValidatorEntry.js.map +1 -0
  222. package/lib/validation/IValidatorHandle.d.ts +2 -0
  223. package/lib/validation/IValidatorHandle.js +4 -0
  224. package/lib/validation/IValidatorHandle.js.map +1 -0
  225. package/lib/validation/Validator.d.ts +17 -0
  226. package/lib/validation/Validator.js +54 -0
  227. package/lib/validation/Validator.js.map +1 -0
  228. package/package.json +73 -0
@@ -0,0 +1,438 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JsonSchema7Serializer = void 0;
4
+ const lodash_1 = require("lodash");
5
+ const base_1 = require("@allgemein/base");
6
+ const JsonSchema7_1 = require("./JsonSchema7");
7
+ const Constants_1 = require("../Constants");
8
+ const IClassRef_1 = require("../../api/IClassRef");
9
+ const SchemaUtils_1 = require("../SchemaUtils");
10
+ const IEntityRef_1 = require("../../api/IEntityRef");
11
+ const Constants_2 = require("./Constants");
12
+ const MetadataRegistry_1 = require("../registry/MetadataRegistry");
13
+ class JsonSchema7Serializer {
14
+ constructor(opts) {
15
+ this.options = opts || {};
16
+ lodash_1.defaults(this.options, {
17
+ keysToSkip: Constants_1.DEFAULT_KEY_TO_SKIP,
18
+ postProcess: (src, dst, serializer) => {
19
+ if (src && src.metaType === Constants_1.METATYPE_PROPERTY && lodash_1.has(dst, 'cardinality')) {
20
+ const cardinality = dst['cardinality'];
21
+ delete dst['cardinality'];
22
+ // TODO handle value corretly minItem/maxItem when item!
23
+ if (cardinality === 0) {
24
+ }
25
+ else {
26
+ //delete dst['cardinality'];
27
+ }
28
+ }
29
+ }
30
+ });
31
+ this.data = this.getOrCreateSchemaDefinitions();
32
+ }
33
+ uri() {
34
+ return `http://json-schema.org/${Constants_2.DRAFT_07}/schema`;
35
+ }
36
+ isCurrentClass(x) {
37
+ return lodash_1.snakeCase(this.current) === lodash_1.snakeCase(x);
38
+ }
39
+ serialize(klass) {
40
+ if (lodash_1.isFunction(klass)) {
41
+ this.current = base_1.ClassUtils.getClassName(klass);
42
+ this.describeClass(klass);
43
+ }
44
+ else if (IClassRef_1.isClassRef(klass)) {
45
+ this.current = klass.name;
46
+ this.describeClassRef(klass);
47
+ }
48
+ else if (IEntityRef_1.isEntityRef(klass)) {
49
+ this.current = klass.name;
50
+ this.describeEntityRef(klass);
51
+ }
52
+ else {
53
+ throw new base_1.NotSupportedError('class or function can\'t be used');
54
+ }
55
+ return this.getJsonSchema();
56
+ }
57
+ getJsonSchema() {
58
+ return this.data;
59
+ }
60
+ getOrCreateSchemaDefinitions(schema) {
61
+ if (!schema) {
62
+ schema = {
63
+ $schema: this.uri() + '#',
64
+ definitions: {}
65
+ };
66
+ }
67
+ else if (schema && !schema.definitions) {
68
+ schema.definitions = {};
69
+ }
70
+ return schema;
71
+ }
72
+ getOrCreateRoot(className, klass) {
73
+ if (this.data.definitions[className]) {
74
+ // definition is present and
75
+ this.applyRef(className);
76
+ return null;
77
+ }
78
+ const appendTarget = this.isAppendTargetSet();
79
+ const root = this.data.definitions[className] = {
80
+ title: className,
81
+ type: Constants_1.T_OBJECT,
82
+ };
83
+ if (IEntityRef_1.isEntityRef(klass)) {
84
+ // when an entity mark with $id!
85
+ root.$id = '#' + klass.name;
86
+ }
87
+ else if (IClassRef_1.isClassRef(klass) && klass.hasEntityRef()) {
88
+ root.$id = '#' + klass.getEntityRef().name;
89
+ }
90
+ if (IEntityRef_1.isEntityRef(klass) || IClassRef_1.isClassRef(klass)) {
91
+ const data = klass.getOptions();
92
+ this.appendAdditionalOptions(root, data);
93
+ }
94
+ if (appendTarget) {
95
+ root.$target = SchemaUtils_1.SchemaUtils.getFunction(klass);
96
+ }
97
+ this.applyRef(className);
98
+ if (this.options.postProcess) {
99
+ this.options.postProcess(klass, root, this);
100
+ }
101
+ return root;
102
+ }
103
+ applyRef(className) {
104
+ if (!this.data.$ref && !this.data.anyOf) {
105
+ this.data.$ref = '#/definitions/' + className;
106
+ }
107
+ else if (this.data.$ref && !this.data.anyOf) {
108
+ if (this.isCurrentClass(className)) {
109
+ this.data.anyOf = [
110
+ { $ref: this.data.$ref },
111
+ { $ref: '#/definitions/' + className }
112
+ ];
113
+ delete this.data['$ref'];
114
+ }
115
+ }
116
+ else if (!this.data.$ref && this.data.anyOf) {
117
+ if (this.isCurrentClass(className)) {
118
+ this.data.anyOf.push({ $ref: '#/definitions/' + className });
119
+ }
120
+ }
121
+ if (this.data.anyOf) {
122
+ this.data.anyOf = lodash_1.uniqBy(this.data.anyOf, x => JSON.stringify(x));
123
+ }
124
+ }
125
+ describeEntityRef(klass) {
126
+ return this.describeRef(klass);
127
+ }
128
+ describeClassRef(klass) {
129
+ return this.describeRef(klass);
130
+ }
131
+ describeRef(klass) {
132
+ const clsRef = IEntityRef_1.isEntityRef(klass) ? klass.getClassRef() : klass;
133
+ const className = IEntityRef_1.isEntityRef(klass) ? klass.name : clsRef.name;
134
+ // schema = this.getOrCreateSchemaDefinitions(schema);
135
+ const root = this.getOrCreateRoot(className, klass);
136
+ if (!root) {
137
+ return null;
138
+ }
139
+ const rootProps = this.describePropertiesForRef(clsRef);
140
+ this.appendProperties(root, rootProps);
141
+ const proto = clsRef.getExtend();
142
+ if (proto) {
143
+ this.describeInheritedClass(root, proto);
144
+ }
145
+ return root;
146
+ }
147
+ describeInheritedClass(root, proto) {
148
+ const inheritedClassName = proto.name;
149
+ root.allOf = [{ $ref: '#/definitions/' + inheritedClassName }];
150
+ if (!this.data.definitions[inheritedClassName]) {
151
+ const inheritedClass = this.getOrCreateRoot(inheritedClassName, proto);
152
+ const props = this.describePropertiesForRef(proto);
153
+ this.appendProperties(inheritedClass, props);
154
+ }
155
+ }
156
+ describeClass(klass) {
157
+ const className = base_1.ClassUtils.getClassName(klass);
158
+ const root = this.getOrCreateRoot(className, klass);
159
+ if (!root) {
160
+ return null;
161
+ }
162
+ const rootProps = this.describePropertiesForFunction(klass);
163
+ this.appendProperties(root, rootProps);
164
+ const proto = SchemaUtils_1.SchemaUtils.getInherited(klass);
165
+ if (proto) {
166
+ const inheritedClassName = base_1.ClassUtils.getClassName(proto);
167
+ root.allOf = [{ $ref: '#/definitions/' + inheritedClassName }];
168
+ if (this.data && !this.data.definitions[inheritedClassName]) {
169
+ const inheritedClass = this.getOrCreateRoot(inheritedClassName, proto);
170
+ const props = this.describePropertiesForFunction(proto);
171
+ this.appendProperties(inheritedClass, props);
172
+ }
173
+ }
174
+ return root;
175
+ }
176
+ appendProperties(data, properties) {
177
+ data.properties = {};
178
+ for (const k of lodash_1.keys(properties)) {
179
+ const p = properties[k];
180
+ if (p[Constants_1.K_PATTERN_PROPERTY]) {
181
+ if (!data.patternProperties) {
182
+ data.patternProperties = {};
183
+ }
184
+ data.patternProperties[k] = p;
185
+ }
186
+ else {
187
+ data.properties[k] = p;
188
+ }
189
+ delete p[Constants_1.K_PATTERN_PROPERTY];
190
+ }
191
+ }
192
+ describePropertiesForFunction(klass) {
193
+ const properties = {};
194
+ const instance = Reflect.construct(klass, []);
195
+ const _properties = Reflect.ownKeys(instance);
196
+ for (const p of _properties) {
197
+ if (lodash_1.isString(p)) {
198
+ const result = this.describePropertyForFunction(klass, p, instance);
199
+ properties[p] = result;
200
+ }
201
+ }
202
+ return properties;
203
+ }
204
+ describePropertiesForRef(klass) {
205
+ const properties = {};
206
+ for (const prop of klass.getPropertyRefs()) {
207
+ const result = this.describePropertyForRef(klass, prop);
208
+ properties[prop.name] = result;
209
+ }
210
+ const instance = klass.create(false);
211
+ // TODO own key
212
+ const _properties = Reflect.ownKeys(instance);
213
+ for (const p of _properties) {
214
+ if (lodash_1.isString(p) && !lodash_1.has(properties, p)) {
215
+ const result = this.describePropertyForFunction(klass, p, instance);
216
+ properties[p] = result;
217
+ }
218
+ else if (lodash_1.isString(p) && lodash_1.has(properties, p)) {
219
+ const value = instance[p];
220
+ if (!lodash_1.isNull(value) || !lodash_1.isUndefined(value)) {
221
+ // add default value if exists
222
+ properties[p].default = SchemaUtils_1.SchemaUtils.normValue(value);
223
+ }
224
+ }
225
+ }
226
+ return properties;
227
+ }
228
+ isAppendTargetSet() {
229
+ return lodash_1.get(this.options, 'appendTarget', false);
230
+ }
231
+ describePropertyForFunction(klass, propertyName, instance) {
232
+ var _a;
233
+ const clazz = IClassRef_1.isClassRef(klass) ? klass.getClass() : klass;
234
+ const propMeta = {};
235
+ // check if property is object
236
+ let value = instance[propertyName];
237
+ let typeHint = typeof value;
238
+ if (typeHint === Constants_1.T_OBJECT) {
239
+ typeHint = (_a = Reflect.getPrototypeOf(value)) === null || _a === void 0 ? void 0 : _a.constructor;
240
+ if (typeHint.name === Object.name) {
241
+ typeHint = Constants_1.T_OBJECT;
242
+ }
243
+ else if (typeHint.name === Array.name) {
244
+ typeHint = Constants_1.T_ARRAY;
245
+ }
246
+ }
247
+ let target = null;
248
+ if (typeHint) {
249
+ if (lodash_1.isString(typeHint)) {
250
+ propMeta.type = typeHint;
251
+ if (propMeta.type === Constants_1.T_ARRAY) {
252
+ this.setDefaultArray(propMeta);
253
+ }
254
+ }
255
+ else if (lodash_1.isFunction(typeHint)) {
256
+ if (typeHint === Date) {
257
+ // propMeta.type = 'date' as any;
258
+ propMeta.type = Constants_1.T_STRING;
259
+ propMeta.format = 'date-time';
260
+ }
261
+ else {
262
+ const name = base_1.ClassUtils.getClassName(typeHint);
263
+ if (name === '' || name === Function.name) {
264
+ // Function passing the parameter type
265
+ // propMeta.$target = typeHint();
266
+ target = typeHint();
267
+ }
268
+ else {
269
+ // propMeta.$target = typeHint as Function;
270
+ target = typeHint;
271
+ }
272
+ // maybe follow the object here
273
+ if (target.name === Array.name) {
274
+ this.setDefaultArray(propMeta);
275
+ }
276
+ else {
277
+ propMeta.type = Constants_1.T_OBJECT;
278
+ }
279
+ }
280
+ }
281
+ }
282
+ if (!propMeta.type || (lodash_1.isString(propMeta.type) && lodash_1.isEmpty(propMeta.type))) {
283
+ const reflectMetadataType = Reflect && Reflect.getMetadata ?
284
+ Reflect.getMetadata(Constants_1.REFLECT_DESIGN_TYPE, clazz, propertyName) : undefined;
285
+ if (reflectMetadataType) {
286
+ const className = base_1.ClassUtils.getClassName(reflectMetadataType);
287
+ if (JsonSchema7_1.JSON_SCHEMA_7_TYPES.includes(className.toLowerCase())) {
288
+ propMeta.type = className.toLowerCase();
289
+ }
290
+ else if (className === Array.name) {
291
+ this.setDefaultArray(propMeta);
292
+ }
293
+ else {
294
+ propMeta.type = Constants_1.T_OBJECT;
295
+ target = reflectMetadataType;
296
+ }
297
+ }
298
+ else {
299
+ // not reflection data
300
+ propMeta.type = Constants_1.T_STRING;
301
+ }
302
+ }
303
+ if (target) {
304
+ if (this.isAppendTargetSet()) {
305
+ propMeta.$target = target;
306
+ }
307
+ const className = base_1.ClassUtils.getClassName(target);
308
+ let ref = '#/definitions/' + className;
309
+ if (propMeta.type === Constants_1.T_ARRAY) {
310
+ propMeta.items = {
311
+ $ref: ref
312
+ };
313
+ }
314
+ else {
315
+ propMeta['$ref'] = ref;
316
+ delete propMeta['type'];
317
+ }
318
+ if (this.data && !this.data.definitions[className]) {
319
+ this.describeClass(target);
320
+ }
321
+ }
322
+ if (!lodash_1.isUndefined(value) || !lodash_1.isNull(value)) {
323
+ propMeta.default = SchemaUtils_1.SchemaUtils.normValue(value);
324
+ }
325
+ this.propertyPostproces(propMeta);
326
+ const data = MetadataRegistry_1.MetadataRegistry.$().find(Constants_1.METATYPE_PROPERTY, (x) => x.target === clazz && x.propertyName === propertyName);
327
+ this.appendAdditionalOptions(propMeta, data);
328
+ return propMeta;
329
+ }
330
+ appendAdditionalOptions(propMeta, data) {
331
+ if (data && lodash_1.keys(data).length > 0) {
332
+ const metaType = data.metaType;
333
+ if (metaType === Constants_1.METATYPE_PROPERTY) {
334
+ if (IClassRef_1.isClassRef(data.type) || IEntityRef_1.isEntityRef(data.type)) {
335
+ // if reference ignore adding
336
+ if (lodash_1.get(this.options, 'deleteReferenceKeys', true)) {
337
+ return;
338
+ }
339
+ }
340
+ }
341
+ const _keys = lodash_1.keys(data);
342
+ for (const k of _keys) {
343
+ if (this.options.keysToSkip.includes(k)) {
344
+ continue;
345
+ }
346
+ if (!propMeta[k] || lodash_1.get(this.options, 'allowKeyOverride', false)) {
347
+ propMeta[k] = data[k];
348
+ }
349
+ }
350
+ }
351
+ }
352
+ setDefaultArray(schema) {
353
+ lodash_1.assign(schema, {
354
+ type: Constants_1.T_ARRAY,
355
+ items: {
356
+ type: Constants_1.T_OBJECT
357
+ }
358
+ });
359
+ }
360
+ describePropertyForRef(klass, property) {
361
+ const propMeta = {};
362
+ if (property.isCollection()) {
363
+ this.setDefaultArray(propMeta);
364
+ if (property.isReference()) {
365
+ this.describeTargetRef(property, propMeta, 'collection');
366
+ }
367
+ else {
368
+ propMeta.items = {
369
+ type: property.getType()
370
+ };
371
+ this.propertyPostproces(propMeta.items);
372
+ }
373
+ }
374
+ else {
375
+ propMeta.type = Constants_1.T_OBJECT;
376
+ if (property.isReference()) {
377
+ this.describeTargetRef(property, propMeta, 'single');
378
+ }
379
+ else {
380
+ propMeta.type = property.getType();
381
+ this.propertyPostproces(propMeta);
382
+ }
383
+ }
384
+ const data = property.getOptions();
385
+ this.appendAdditionalOptions(propMeta, data);
386
+ // pass data pattern property for later correct selection
387
+ if (data[Constants_1.K_PATTERN_PROPERTY]) {
388
+ propMeta[Constants_1.K_PATTERN_PROPERTY] = true;
389
+ }
390
+ if (this.options.postProcess) {
391
+ this.options.postProcess(property, propMeta, this);
392
+ }
393
+ return propMeta;
394
+ }
395
+ describeTargetRef(property, propMeta, mode) {
396
+ let targetRef = property.getTargetRef();
397
+ if (lodash_1.get(this.options, 'deleteReferenceKeys', true)) {
398
+ lodash_1.keys(propMeta).map(k => delete propMeta[k]);
399
+ }
400
+ else {
401
+ this.options.keysToSkip.map(x => delete propMeta[x]);
402
+ }
403
+ if (targetRef.hasEntityRef()) {
404
+ targetRef = targetRef.getEntityRef();
405
+ if (mode === 'collection') {
406
+ propMeta.type = Constants_1.T_ARRAY;
407
+ propMeta.items = { $ref: '#/definitions/' + targetRef.name };
408
+ }
409
+ else {
410
+ propMeta.$ref = '#/definitions/' + targetRef.name;
411
+ }
412
+ if (this.data && !this.data.definitions[targetRef.name]) {
413
+ this.describeEntityRef(targetRef);
414
+ }
415
+ }
416
+ else {
417
+ if (mode === 'collection') {
418
+ propMeta.type = Constants_1.T_ARRAY;
419
+ propMeta.items = { $ref: '#/definitions/' + targetRef.name };
420
+ }
421
+ else {
422
+ propMeta.$ref = '#/definitions/' + targetRef.name;
423
+ }
424
+ if (this.data && !this.data.definitions[targetRef.name]) {
425
+ this.describeClassRef(targetRef);
426
+ }
427
+ }
428
+ }
429
+ propertyPostproces(opt) {
430
+ if (opt.type === 'date') {
431
+ opt.type = Constants_1.T_STRING;
432
+ opt.format = 'date-time';
433
+ }
434
+ }
435
+ }
436
+ exports.JsonSchema7Serializer = JsonSchema7Serializer;
437
+
438
+ //# sourceMappingURL=JsonSchema7Serializer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/lib/json-schema/JsonSchema7Serializer.ts"],"names":[],"mappings":";;;AAAA,mCAagB;AAChB,0CAA8D;AAC9D,+CAA8G;AAC9G,4CAQsB;AACtB,mDAA0D;AAC1D,gDAA2C;AAG3C,qDAA6D;AAE7D,2CAAqC;AACrC,mEAA8D;AAK9D,MAAa,qBAAqB;IAQhC,YAAY,IAAiC;QAC3C,IAAI,CAAC,OAAO,GAAG,IAAI,IAAI,EAAE,CAAC;QAC1B,iBAAQ,CAAC,IAAI,CAAC,OAAO,EAA+B;YAClD,UAAU,EAAE,+BAAmB;YAC/B,WAAW,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,UAAU,EAAE,EAAE;gBACpC,IAAI,GAAG,IAAI,GAAG,CAAC,QAAQ,KAAK,6BAAiB,IAAI,YAAG,CAAC,GAAG,EAAE,aAAa,CAAC,EAAE;oBACxE,MAAM,WAAW,GAAG,GAAG,CAAC,aAAa,CAAC,CAAC;oBACvC,OAAO,GAAG,CAAC,aAAa,CAAC,CAAC;oBAC1B,wDAAwD;oBACxD,IAAI,WAAW,KAAK,CAAC,EAAE;qBAEtB;yBAAM;wBACL,4BAA4B;qBAC7B;iBAEF;YACH,CAAC;SACF,CAAC,CAAC;QACH,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,4BAA4B,EAAE,CAAC;IAClD,CAAC;IAGD,GAAG;QACD,OAAO,0BAA0B,oBAAQ,SAAS,CAAC;IACrD,CAAC;IAGO,cAAc,CAAC,CAAS;QAC9B,OAAO,kBAAS,CAAC,IAAI,CAAC,OAA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AC,CAAC;aACnC;SACF;aAAM;YACL,IAAI,IAAI,KAAK,YAAY,EAAE;gBACzB,QAAQ,CAAC,IAAI,GAAG,mBAAO,CAAC;gBACxB,QAAQ,CAAC,KAAK,GAAG,EAAC,IAAI,EAAE,gBAAgB,GAAG,SAAS,CAAC,IAAI,EAAC,CAAC;aAC5D;iBAAM;gBACL,QAAQ,CAAC,IAAI,GAAG,gBAAgB,GAAG,SAAS,CAAC,IAAI,CAAC;aACnD;YACD,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE;gBACvD,IAAI,CAAC,gBAAgB,CAAC,SAAS,CAAC,CAAC;aAClC;SACF;IACH,CAAC;IAED,kBAAkB,CAAC,GAAiB;QAClC,IAAK,GAAG,CAAC,IAAY,KAAK,MAAM,EAAE;YAChC,GAAG,CAAC,IAAI,GAAG,oBAAQ,CAAC;YACpB,GAAG,CAAC,MAAM,GAAG,WAAW,CAAC;SAC1B;IAEH,CAAC;CAGF;AA7dD,sDA6dC","file":"JsonSchema7Serializer.js","sourcesContent":["import {\n assign,\n defaults,\n get,\n has,\n isEmpty,\n isFunction,\n isNull,\n isString,\n isUndefined,\n keys,\n snakeCase,\n uniqBy\n} from 'lodash';\nimport {ClassUtils, NotSupportedError} from '@allgemein/base';\nimport {IJsonSchema7, IJsonSchema7Definition, IJsonSchema7TypeName, JSON_SCHEMA_7_TYPES} from './JsonSchema7';\nimport {\n DEFAULT_KEY_TO_SKIP,\n K_PATTERN_PROPERTY,\n METATYPE_PROPERTY,\n REFLECT_DESIGN_TYPE,\n T_ARRAY,\n T_OBJECT,\n T_STRING\n} from '../Constants';\nimport {IClassRef, isClassRef} from '../../api/IClassRef';\nimport {SchemaUtils} from '../SchemaUtils';\nimport {IPropertyRef} from '../../api/IPropertyRef';\nimport {IJsonSchemaSerializer} from './IJsonSchemaSerializer';\nimport {IEntityRef, isEntityRef} from '../../api/IEntityRef';\nimport {IJsonSchemaSerializeOptions} from './IJsonSchemaSerializeOptions';\nimport {DRAFT_07} from './Constants';\nimport {MetadataRegistry} from '../registry/MetadataRegistry';\nimport {IEntityOptions} from '../options/IEntityOptions';\nimport {IPropertyOptions} from '../options/IPropertyOptions';\n\n\nexport class JsonSchema7Serializer implements IJsonSchemaSerializer {\n\n options: IJsonSchemaSerializeOptions;\n\n data: IJsonSchema7;\n\n current: string;\n\n constructor(opts: IJsonSchemaSerializeOptions) {\n this.options = opts || {};\n defaults(this.options, <IJsonSchemaSerializeOptions>{\n keysToSkip: DEFAULT_KEY_TO_SKIP,\n postProcess: (src, dst, serializer) => {\n if (src && src.metaType === METATYPE_PROPERTY && has(dst, 'cardinality')) {\n const cardinality = dst['cardinality'];\n delete dst['cardinality'];\n // TODO handle value corretly minItem/maxItem when item!\n if (cardinality === 0) {\n\n } else {\n //delete dst['cardinality'];\n }\n\n }\n }\n });\n this.data = this.getOrCreateSchemaDefinitions();\n }\n\n\n uri(): string {\n return `http://json-schema.org/${DRAFT_07}/schema`;\n }\n\n\n private isCurrentClass(x: string) {\n return snakeCase(this.current) === snakeCase(x);\n }\n\n serialize(klass: IClassRef | IEntityRef | Function | object): IJsonSchema7 {\n if (isFunction(klass)) {\n this.current = ClassUtils.getClassName(klass);\n this.describeClass(klass);\n } else if (isClassRef(klass)) {\n this.current = klass.name;\n this.describeClassRef(klass);\n } else if (isEntityRef(klass)) {\n this.current = klass.name;\n this.describeEntityRef(klass);\n } else {\n throw new NotSupportedError('class or function can\\'t be used');\n }\n return this.getJsonSchema();\n }\n\n getJsonSchema() {\n return this.data;\n }\n\n private getOrCreateSchemaDefinitions(schema?: IJsonSchema7) {\n if (!schema) {\n schema = {\n $schema: this.uri() + '#',\n definitions: {}\n };\n } else if (schema && !schema.definitions) {\n schema.definitions = {};\n }\n return schema;\n }\n\n\n private getOrCreateRoot(className: string, klass: Function | IClassRef | IEntityRef) {\n if (this.data.definitions[className]) {\n // definition is present and\n this.applyRef(className);\n return null;\n }\n\n const appendTarget = this.isAppendTargetSet();\n const root: IJsonSchema7Definition = this.data.definitions[className] = {\n title: className,\n type: T_OBJECT,\n };\n\n if (isEntityRef(klass)) {\n // when an entity mark with $id!\n root.$id = '#' + klass.name;\n } else if (isClassRef(klass) && klass.hasEntityRef()) {\n root.$id = '#' + klass.getEntityRef().name;\n }\n\n if (isEntityRef(klass) || isClassRef(klass)) {\n const data = klass.getOptions();\n this.appendAdditionalOptions(root, data);\n }\n\n if (appendTarget) {\n root.$target = SchemaUtils.getFunction(klass);\n }\n\n this.applyRef(className);\n\n if (this.options.postProcess) {\n this.options.postProcess(klass, root, this);\n }\n return root;\n }\n\n\n applyRef(className: string) {\n if (!this.data.$ref && !this.data.anyOf) {\n this.data.$ref = '#/definitions/' + className;\n } else if (this.data.$ref && !this.data.anyOf) {\n if (this.isCurrentClass(className)) {\n this.data.anyOf = [\n {$ref: this.data.$ref},\n {$ref: '#/definitions/' + className}\n ];\n delete this.data['$ref'];\n }\n } else if (!this.data.$ref && this.data.anyOf) {\n if (this.isCurrentClass(className)) {\n this.data.anyOf.push({$ref: '#/definitions/' + className});\n }\n }\n\n if (this.data.anyOf) {\n this.data.anyOf = uniqBy(this.data.anyOf, x => JSON.stringify(x));\n }\n }\n\n\n describeEntityRef(klass: IEntityRef): IJsonSchema7 {\n return this.describeRef(klass);\n }\n\n\n describeClassRef(klass: IClassRef): IJsonSchema7 {\n return this.describeRef(klass);\n }\n\n private describeRef(klass: IEntityRef | IClassRef): IJsonSchema7 {\n const clsRef = isEntityRef(klass) ? klass.getClassRef() : klass;\n const className = isEntityRef(klass) ? klass.name : clsRef.name;\n\n // schema = this.getOrCreateSchemaDefinitions(schema);\n const root = this.getOrCreateRoot(className, klass);\n if (!root) {\n return null;\n }\n\n const rootProps = this.describePropertiesForRef(clsRef);\n this.appendProperties(root, rootProps);\n\n const proto = clsRef.getExtend();\n if (proto) {\n this.describeInheritedClass(root, proto);\n }\n return root;\n }\n\n\n describeInheritedClass(root: IJsonSchema7, proto: IClassRef) {\n const inheritedClassName = proto.name;\n root.allOf = [{$ref: '#/definitions/' + inheritedClassName}];\n if (!this.data.definitions[inheritedClassName]) {\n const inheritedClass = this.getOrCreateRoot(inheritedClassName, proto);\n const props = this.describePropertiesForRef(proto);\n this.appendProperties(inheritedClass, props);\n }\n }\n\n\n describeClass(klass: Function): IJsonSchema7 {\n const className = ClassUtils.getClassName(klass);\n\n const root = this.getOrCreateRoot(className, klass);\n if (!root) {\n return null;\n }\n\n const rootProps = this.describePropertiesForFunction(klass);\n this.appendProperties(root, rootProps);\n\n const proto = SchemaUtils.getInherited(klass);\n if (proto) {\n const inheritedClassName = ClassUtils.getClassName(proto);\n root.allOf = [{$ref: '#/definitions/' + inheritedClassName}];\n if (this.data && !this.data.definitions[inheritedClassName]) {\n const inheritedClass = this.getOrCreateRoot(inheritedClassName, proto);\n const props = this.describePropertiesForFunction(proto);\n this.appendProperties(inheritedClass, props);\n }\n }\n return root;\n }\n\n appendProperties(data: any, properties: { [k: string]: IJsonSchema7Definition }) {\n data.properties = {};\n for (const k of keys(properties)) {\n const p = properties[k];\n if (p[K_PATTERN_PROPERTY]) {\n if (!data.patternProperties) {\n data.patternProperties = {};\n }\n data.patternProperties[k] = p;\n } else {\n data.properties[k] = p;\n }\n delete p[K_PATTERN_PROPERTY];\n }\n }\n\n\n describePropertiesForFunction(klass: Function) {\n const properties: { [k: string]: IJsonSchema7Definition } = {};\n const instance = Reflect.construct(klass, []);\n const _properties = Reflect.ownKeys(instance);\n for (const p of _properties) {\n if (isString(p)) {\n const result = this.describePropertyForFunction(klass, p, instance);\n properties[p] = result;\n }\n }\n return properties;\n }\n\n\n describePropertiesForRef(klass: IClassRef) {\n const properties: { [k: string]: IJsonSchema7Definition } = {};\n\n for (const prop of klass.getPropertyRefs()) {\n const result = this.describePropertyForRef(klass, prop);\n properties[prop.name] = result;\n }\n\n const instance = klass.create<object>(false);\n // TODO own key\n const _properties = Reflect.ownKeys(instance);\n for (const p of _properties) {\n if (isString(p) && !has(properties, p)) {\n const result = this.describePropertyForFunction(klass, p, instance);\n properties[p] = result;\n } else if (isString(p) && has(properties, p)) {\n const value = instance[p];\n if (!isNull(value) || !isUndefined(value)) {\n // add default value if exists\n (properties[p] as any).default = SchemaUtils.normValue(value);\n }\n }\n }\n return properties;\n }\n\n\n isAppendTargetSet() {\n return get(this.options, 'appendTarget', false);\n }\n\n\n describePropertyForFunction(klass: Function | IClassRef, propertyName: string, instance?: any): IJsonSchema7Definition {\n const clazz = isClassRef(klass) ? klass.getClass() : klass;\n const propMeta: IJsonSchema7Definition = {};\n\n // check if property is object\n let value = instance[propertyName];\n let typeHint: any = typeof value;\n if (typeHint === T_OBJECT) {\n typeHint = Reflect.getPrototypeOf(value)?.constructor;\n if (typeHint.name === Object.name) {\n typeHint = T_OBJECT;\n } else if (typeHint.name === Array.name) {\n typeHint = T_ARRAY;\n }\n }\n\n let target = null;\n if (typeHint) {\n\n if (isString(typeHint)) {\n propMeta.type = typeHint as IJsonSchema7TypeName;\n if (propMeta.type === T_ARRAY) {\n this.setDefaultArray(propMeta);\n }\n } else if (isFunction(typeHint)) {\n if (typeHint === Date) {\n // propMeta.type = 'date' as any;\n propMeta.type = T_STRING;\n propMeta.format = 'date-time';\n } else {\n const name = ClassUtils.getClassName(typeHint);\n\n if (name === '' || name === Function.name) {\n // Function passing the parameter type\n // propMeta.$target = typeHint();\n target = typeHint();\n } else {\n // propMeta.$target = typeHint as Function;\n target = typeHint as Function;\n }\n\n // maybe follow the object here\n if (target.name === Array.name) {\n this.setDefaultArray(propMeta);\n } else {\n propMeta.type = T_OBJECT;\n }\n }\n }\n }\n\n\n if (!propMeta.type || (isString(propMeta.type) && isEmpty(propMeta.type))) {\n const reflectMetadataType = Reflect && Reflect.getMetadata ?\n Reflect.getMetadata(REFLECT_DESIGN_TYPE, clazz, propertyName) : undefined;\n\n if (reflectMetadataType) {\n const className = ClassUtils.getClassName(reflectMetadataType);\n if (JSON_SCHEMA_7_TYPES.includes(className.toLowerCase() as any)) {\n propMeta.type = className.toLowerCase() as IJsonSchema7TypeName;\n } else if (className === Array.name) {\n this.setDefaultArray(propMeta);\n } else {\n propMeta.type = T_OBJECT;\n target = reflectMetadataType;\n }\n } else {\n // not reflection data\n propMeta.type = T_STRING;\n }\n }\n\n if (target) {\n if (this.isAppendTargetSet()) {\n propMeta.$target = target;\n }\n\n const className = ClassUtils.getClassName(target);\n let ref = '#/definitions/' + className;\n if (propMeta.type === T_ARRAY) {\n propMeta.items = {\n $ref: ref\n };\n } else {\n propMeta['$ref'] = ref;\n delete propMeta['type'];\n }\n if (this.data && !this.data.definitions[className]) {\n this.describeClass(target);\n }\n }\n\n if (!isUndefined(value) || !isNull(value)) {\n propMeta.default = SchemaUtils.normValue(value);\n }\n\n this.propertyPostproces(propMeta);\n const data = MetadataRegistry.$().find(METATYPE_PROPERTY, (x: any) => x.target === clazz && x.propertyName === propertyName);\n this.appendAdditionalOptions(propMeta, data);\n return propMeta;\n }\n\n\n appendAdditionalOptions(propMeta: any, data: IEntityOptions | IPropertyOptions) {\n if (data && keys(data).length > 0) {\n const metaType = data.metaType;\n if (metaType === METATYPE_PROPERTY) {\n if (isClassRef(data.type) || isEntityRef(data.type)) {\n // if reference ignore adding\n if (get(this.options, 'deleteReferenceKeys', true)) {\n return;\n }\n }\n }\n const _keys = keys(data);\n\n for (const k of _keys) {\n if (this.options.keysToSkip.includes(k)) {\n continue;\n }\n\n if (!propMeta[k] || get(this.options, 'allowKeyOverride', false)) {\n propMeta[k] = data[k];\n }\n }\n }\n }\n\n\n setDefaultArray(schema: IJsonSchema7) {\n assign(schema, {\n type: T_ARRAY,\n items: {\n type: T_OBJECT\n }\n });\n }\n\n\n describePropertyForRef(klass: IClassRef, property: IPropertyRef): IJsonSchema7Definition {\n const propMeta: IJsonSchema7Definition = {};\n\n if (property.isCollection()) {\n this.setDefaultArray(propMeta);\n if (property.isReference()) {\n this.describeTargetRef(property, propMeta, 'collection');\n } else {\n propMeta.items = {\n type: property.getType() as any\n };\n this.propertyPostproces(propMeta.items);\n }\n } else {\n propMeta.type = T_OBJECT;\n if (property.isReference()) {\n this.describeTargetRef(property, propMeta, 'single');\n } else {\n propMeta.type = property.getType() as any;\n this.propertyPostproces(propMeta);\n }\n }\n\n const data = property.getOptions();\n this.appendAdditionalOptions(propMeta, data);\n // pass data pattern property for later correct selection\n if (data[K_PATTERN_PROPERTY]) {\n (propMeta as any)[K_PATTERN_PROPERTY] = true;\n }\n if (this.options.postProcess) {\n this.options.postProcess(property, propMeta, this);\n }\n return propMeta;\n }\n\n\n describeTargetRef(property: IPropertyRef, propMeta: any, mode: 'collection' | 'single') {\n let targetRef: any = property.getTargetRef();\n if (get(this.options, 'deleteReferenceKeys', true)) {\n keys(propMeta).map(k => delete propMeta[k]);\n } else {\n this.options.keysToSkip.map(x => delete propMeta[x]);\n }\n\n if (targetRef.hasEntityRef()) {\n targetRef = targetRef.getEntityRef();\n if (mode === 'collection') {\n propMeta.type = T_ARRAY;\n propMeta.items = {$ref: '#/definitions/' + targetRef.name};\n } else {\n propMeta.$ref = '#/definitions/' + targetRef.name;\n }\n if (this.data && !this.data.definitions[targetRef.name]) {\n this.describeEntityRef(targetRef);\n }\n } else {\n if (mode === 'collection') {\n propMeta.type = T_ARRAY;\n propMeta.items = {$ref: '#/definitions/' + targetRef.name};\n } else {\n propMeta.$ref = '#/definitions/' + targetRef.name;\n }\n if (this.data && !this.data.definitions[targetRef.name]) {\n this.describeClassRef(targetRef);\n }\n }\n }\n\n propertyPostproces(opt: IJsonSchema7) {\n if ((opt.type as any) === 'date') {\n opt.type = T_STRING;\n opt.format = 'date-time';\n }\n\n }\n\n\n}\n"],"sourceRoot":"../.."}
@@ -0,0 +1,60 @@
1
+ import { IJsonSchema7, IJsonSchema7Definition } from './JsonSchema7';
2
+ import { IClassRef } from '../../api/IClassRef';
3
+ import { IJsonSchemaUnserializeOptions } from './IJsonSchemaUnserializeOptions';
4
+ import { IJsonSchemaUnserializer } from './IJsonSchemaUnserializer';
5
+ import { IEntityRef } from '../../api/IEntityRef';
6
+ import { ClassRef } from '../ClassRef';
7
+ import { IParseOptions } from './IParseOptions';
8
+ import { IPropertyOptions } from '../options/IPropertyOptions';
9
+ export declare class JsonSchema7Unserializer implements IJsonSchemaUnserializer {
10
+ version: string;
11
+ options: IJsonSchemaUnserializeOptions;
12
+ data: IJsonSchema7;
13
+ classRefs: IClassRef[];
14
+ fetched: {
15
+ [k: string]: IJsonSchema7;
16
+ };
17
+ reference: {
18
+ [k: string]: any;
19
+ };
20
+ constructor(opts: IJsonSchemaUnserializeOptions);
21
+ uri(): string;
22
+ unserialize(data: any): Promise<IClassRef | IEntityRef | (IClassRef | IEntityRef)[]>;
23
+ /**
24
+ * Return namespace if set in passed options or if used in json-schema object.
25
+ * If no match return default namespace
26
+ */
27
+ getNamespace(): any;
28
+ /**
29
+ * Get registry containing elements
30
+ */
31
+ getRegistry(): import("../..").ILookupRegistry;
32
+ /**
33
+ * If collectors are defined use them before default
34
+ *
35
+ *
36
+ * @param type
37
+ * @param key
38
+ * @param data
39
+ * @param options
40
+ * @private
41
+ */
42
+ private collectOptions;
43
+ getClassRef(className: string | Function, namespace?: string): ClassRef;
44
+ parse(data: IJsonSchema7, options?: IParseOptions): Promise<IClassRef | IEntityRef | (IClassRef | IEntityRef)[]>;
45
+ parseRef(data: any, options: IParseOptions): Promise<any>;
46
+ getDefinitionsKey(data: object): string;
47
+ followRef($ref: string, data: IJsonSchema7): Promise<any>;
48
+ parseProperties(classRef: IClassRef | IEntityRef, properties: {
49
+ [propertyName: string]: IJsonSchema7Definition;
50
+ }, options?: IParseOptions): Promise<void>;
51
+ parseProperty(classRef: IClassRef | IEntityRef, propertyName: string, data: IJsonSchema7Definition, options?: IParseOptions): Promise<void>;
52
+ hasProperties(datapointer: any): boolean;
53
+ hasPatternProperties(datapointer: any): boolean;
54
+ onTypes(dataPointer: IJsonSchema7, propOptions: IPropertyOptions, options: IParseOptions): Promise<void>;
55
+ onTypeString(dataPointer: any): any;
56
+ parseInherits(classRef: IClassRef, data: IJsonSchema7, key: 'allOf' | 'anyOf'): Promise<void>;
57
+ parseTypeObject(data: IJsonSchema7, options?: IParseOptions): Promise<IEntityRef | IClassRef>;
58
+ collectAndProcess(data: IJsonSchema7, collectingObject: any, skipKeys: string[], collectorOptions: IParseOptions): void;
59
+ getClassNameFromRef(data: string): string;
60
+ }