@unispechq/unispec-core 0.3.2 → 0.3.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 (265) hide show
  1. package/dist/cjs/src/cache/cache-factory.js +1 -1
  2. package/dist/cjs/src/cache/cache-manager.js +2 -2
  3. package/dist/cjs/src/cache/hash-utils.js +1 -1
  4. package/dist/cjs/src/cache/hashing.js +1 -1
  5. package/dist/cjs/src/cache/index.js +5 -5
  6. package/dist/cjs/src/cache/lru-cache.js +1 -1
  7. package/dist/cjs/src/diff/core.js +2 -2
  8. package/dist/cjs/src/diff/enhanced-diff.js +5 -5
  9. package/dist/cjs/src/diff/index.js +8 -8
  10. package/dist/cjs/src/errors/config-error.js +1 -1
  11. package/dist/cjs/src/errors/error-factory.js +7 -7
  12. package/dist/cjs/src/errors/index.js +8 -8
  13. package/dist/cjs/src/errors/loader-error.js +1 -1
  14. package/dist/cjs/src/errors/reference-error.js +1 -1
  15. package/dist/cjs/src/errors/schema-error.js +1 -1
  16. package/dist/cjs/src/errors/security-error.js +1 -1
  17. package/dist/cjs/src/errors/semantic-error.js +1 -1
  18. package/dist/cjs/src/index.js +13 -13
  19. package/dist/cjs/src/loader/index.js +4 -4
  20. package/dist/cjs/src/loader/security-validator.js +1 -1
  21. package/dist/cjs/src/loader/unispec-loader.js +3 -3
  22. package/dist/cjs/src/loader/yaml-loader.js +1 -1
  23. package/dist/cjs/src/normalizer/core.js +4 -4
  24. package/dist/cjs/src/normalizer/index.js +1 -1
  25. package/dist/cjs/src/optimizer/core.js +2 -2
  26. package/dist/cjs/src/optimizer/index.js +3 -3
  27. package/dist/cjs/src/optimizer/optimization-functions.js +2 -2
  28. package/dist/cjs/src/schemas/dedupe.js +1 -1
  29. package/dist/cjs/src/schemas/index.js +3 -3
  30. package/dist/cjs/src/schemas/resolver.js +1 -1
  31. package/dist/cjs/src/validator/ajv-validator.js +1 -1
  32. package/dist/cjs/src/validator/config-validator-main.js +4 -4
  33. package/dist/cjs/src/validator/config-validator.js +1 -1
  34. package/dist/cjs/src/validator/index.js +7 -7
  35. package/dist/cjs/src/validator/reference-validator.js +1 -1
  36. package/dist/cjs/src/validator/tests-validator.js +2 -2
  37. package/dist/cjs/src/validator/unispec-validator.js +5 -5
  38. package/dist/cjs/src/validator/validator-factory.js +1 -1
  39. package/dist/cjs/tests/cache/cache.test.js +4 -4
  40. package/dist/cjs/tests/cache/utils.js +1 -1
  41. package/dist/cjs/tests/diff/diff-annotators.test.js +2 -2
  42. package/dist/cjs/tests/diff/diff-comprehensive.test.js +2 -2
  43. package/dist/cjs/tests/diff/diff-extended.test.js +2 -2
  44. package/dist/cjs/tests/diff/diff.test.js +2 -2
  45. package/dist/cjs/tests/diff/utils.js +1 -1
  46. package/dist/cjs/tests/errors/errors-integration.test.js +3 -3
  47. package/dist/cjs/tests/errors/errors.test.js +2 -2
  48. package/dist/cjs/tests/errors/utils.js +1 -1
  49. package/dist/cjs/tests/loader/integration.test.js +2 -2
  50. package/dist/cjs/tests/loader/loader.test.js +2 -2
  51. package/dist/cjs/tests/normalizer/normalizer-comprehensive.test.js +2 -2
  52. package/dist/cjs/tests/normalizer/normalizer-invalid.test.js +2 -2
  53. package/dist/cjs/tests/normalizer/normalizer-valid.test.js +2 -2
  54. package/dist/cjs/tests/normalizer/utils.js +1 -1
  55. package/dist/cjs/tests/optimizer/compress-references.test.js +2 -2
  56. package/dist/cjs/tests/optimizer/deduplication.test.js +2 -2
  57. package/dist/cjs/tests/optimizer/integration.test.js +3 -3
  58. package/dist/cjs/tests/optimizer/optimization-report.test.js +2 -2
  59. package/dist/cjs/tests/optimizer/optimize-document.test.js +2 -2
  60. package/dist/cjs/tests/optimizer/orphaned-schemas.test.js +2 -2
  61. package/dist/cjs/tests/optimizer/sort-schemas.test.js +2 -2
  62. package/dist/cjs/tests/optimizer/utils.js +1 -1
  63. package/dist/cjs/tests/schemas/schemas-edge-cases.test.js +3 -3
  64. package/dist/cjs/tests/schemas/schemas.test.js +3 -3
  65. package/dist/cjs/tests/schemas/utils.js +1 -1
  66. package/dist/cjs/tests/validator/config-validator.test.js +2 -2
  67. package/dist/cjs/tests/validator/unispec-validator.test.js +2 -2
  68. package/dist/cjs/tests/validator/utils.js +1 -1
  69. package/package.json +1 -1
  70. package/dist/cache/cache-factory.d.ts +0 -31
  71. package/dist/cache/cache-factory.js +0 -65
  72. package/dist/cache/cache-manager.d.ts +0 -62
  73. package/dist/cache/cache-manager.js +0 -122
  74. package/dist/cache/constants.d.ts +0 -21
  75. package/dist/cache/constants.js +0 -22
  76. package/dist/cache/hash-utils.d.ts +0 -21
  77. package/dist/cache/hash-utils.js +0 -35
  78. package/dist/cache/hashing.d.ts +0 -19
  79. package/dist/cache/hashing.js +0 -197
  80. package/dist/cache/index.d.ts +0 -6
  81. package/dist/cache/index.js +0 -10
  82. package/dist/cache/lru-cache.d.ts +0 -56
  83. package/dist/cache/lru-cache.js +0 -161
  84. package/dist/cache/types.d.ts +0 -24
  85. package/dist/cache/types.js +0 -4
  86. package/dist/cjs/cache/cache-factory.js +0 -72
  87. package/dist/cjs/cache/cache-manager.js +0 -126
  88. package/dist/cjs/cache/constants.js +0 -25
  89. package/dist/cjs/cache/hash-utils.js +0 -41
  90. package/dist/cjs/cache/hashing.js +0 -236
  91. package/dist/cjs/cache/index.js +0 -26
  92. package/dist/cjs/cache/lru-cache.js +0 -165
  93. package/dist/cjs/cache/types.js +0 -5
  94. package/dist/cjs/diff/annotators.js +0 -159
  95. package/dist/cjs/diff/change-reports.js +0 -369
  96. package/dist/cjs/diff/core.js +0 -158
  97. package/dist/cjs/diff/enhanced-diff.js +0 -79
  98. package/dist/cjs/diff/impact-strategies-refactored.js +0 -230
  99. package/dist/cjs/diff/impact-strategies.js +0 -219
  100. package/dist/cjs/diff/index.js +0 -27
  101. package/dist/cjs/diff/metrics-calculator.js +0 -69
  102. package/dist/cjs/diff/risk-calculator.js +0 -58
  103. package/dist/cjs/diff/suggestion-generator.js +0 -78
  104. package/dist/cjs/diff/types.js +0 -11
  105. package/dist/cjs/errors/base-error.js +0 -33
  106. package/dist/cjs/errors/config-error.js +0 -11
  107. package/dist/cjs/errors/error-factory.js +0 -48
  108. package/dist/cjs/errors/index.js +0 -19
  109. package/dist/cjs/errors/loader-error.js +0 -11
  110. package/dist/cjs/errors/reference-error.js +0 -11
  111. package/dist/cjs/errors/schema-error.js +0 -11
  112. package/dist/cjs/errors/security-error.js +0 -11
  113. package/dist/cjs/errors/semantic-error.js +0 -11
  114. package/dist/cjs/generated-schemas.js +0 -2100
  115. package/dist/cjs/index.js +0 -59
  116. package/dist/cjs/loader/index.js +0 -13
  117. package/dist/cjs/loader/security-validator.js +0 -53
  118. package/dist/cjs/loader/types.js +0 -11
  119. package/dist/cjs/loader/unispec-loader.js +0 -84
  120. package/dist/cjs/loader/yaml-loader.js +0 -76
  121. package/dist/cjs/normalizer/core.js +0 -32
  122. package/dist/cjs/normalizer/graphql-normalizer.js +0 -67
  123. package/dist/cjs/normalizer/index.js +0 -7
  124. package/dist/cjs/normalizer/rest-normalizer.js +0 -51
  125. package/dist/cjs/normalizer/types.js +0 -2
  126. package/dist/cjs/normalizer/utils.js +0 -33
  127. package/dist/cjs/normalizer/websocket-normalizer.js +0 -81
  128. package/dist/cjs/optimizer/core.js +0 -115
  129. package/dist/cjs/optimizer/index.js +0 -17
  130. package/dist/cjs/optimizer/optimization-functions.js +0 -185
  131. package/dist/cjs/optimizer/types.js +0 -2
  132. package/dist/cjs/optimizer/utils.js +0 -32
  133. package/dist/cjs/schemas/dedupe.js +0 -100
  134. package/dist/cjs/schemas/index.js +0 -14
  135. package/dist/cjs/schemas/resolver.js +0 -41
  136. package/dist/cjs/schemas/utils.js +0 -53
  137. package/dist/cjs/types/index.js +0 -2
  138. package/dist/cjs/validator/ajv-validator.js +0 -82
  139. package/dist/cjs/validator/config-validator-main.js +0 -34
  140. package/dist/cjs/validator/config-validator.js +0 -17
  141. package/dist/cjs/validator/index.js +0 -23
  142. package/dist/cjs/validator/object-traversal.js +0 -112
  143. package/dist/cjs/validator/reference-validator.js +0 -233
  144. package/dist/cjs/validator/schema-references.js +0 -116
  145. package/dist/cjs/validator/semantic-validator.js +0 -328
  146. package/dist/cjs/validator/tests-validator.js +0 -16
  147. package/dist/cjs/validator/types.js +0 -2
  148. package/dist/cjs/validator/unispec-validator.js +0 -84
  149. package/dist/cjs/validator/validator-factory.js +0 -77
  150. package/dist/cjs/versions.js +0 -147
  151. package/dist/diff/annotators.d.ts +0 -4
  152. package/dist/diff/annotators.js +0 -154
  153. package/dist/diff/change-reports.d.ts +0 -37
  154. package/dist/diff/change-reports.js +0 -366
  155. package/dist/diff/core.d.ts +0 -26
  156. package/dist/diff/core.js +0 -155
  157. package/dist/diff/enhanced-diff.d.ts +0 -51
  158. package/dist/diff/enhanced-diff.js +0 -76
  159. package/dist/diff/impact-strategies-refactored.d.ts +0 -69
  160. package/dist/diff/impact-strategies-refactored.js +0 -223
  161. package/dist/diff/impact-strategies.d.ts +0 -41
  162. package/dist/diff/impact-strategies.js +0 -212
  163. package/dist/diff/index.d.ts +0 -8
  164. package/dist/diff/index.js +0 -11
  165. package/dist/diff/metrics-calculator.d.ts +0 -23
  166. package/dist/diff/metrics-calculator.js +0 -65
  167. package/dist/diff/risk-calculator.d.ts +0 -23
  168. package/dist/diff/risk-calculator.js +0 -55
  169. package/dist/diff/suggestion-generator.d.ts +0 -18
  170. package/dist/diff/suggestion-generator.js +0 -74
  171. package/dist/diff/types.d.ts +0 -24
  172. package/dist/diff/types.js +0 -8
  173. package/dist/errors/base-error.d.ts +0 -20
  174. package/dist/errors/base-error.js +0 -29
  175. package/dist/errors/config-error.d.ts +0 -4
  176. package/dist/errors/config-error.js +0 -7
  177. package/dist/errors/error-factory.d.ts +0 -22
  178. package/dist/errors/error-factory.js +0 -45
  179. package/dist/errors/index.d.ts +0 -8
  180. package/dist/errors/index.js +0 -8
  181. package/dist/errors/loader-error.d.ts +0 -4
  182. package/dist/errors/loader-error.js +0 -7
  183. package/dist/errors/reference-error.d.ts +0 -4
  184. package/dist/errors/reference-error.js +0 -7
  185. package/dist/errors/schema-error.d.ts +0 -4
  186. package/dist/errors/schema-error.js +0 -7
  187. package/dist/errors/security-error.d.ts +0 -4
  188. package/dist/errors/security-error.js +0 -7
  189. package/dist/errors/semantic-error.d.ts +0 -4
  190. package/dist/errors/semantic-error.js +0 -7
  191. package/dist/generated-schemas.d.ts +0 -2073
  192. package/dist/generated-schemas.js +0 -2097
  193. package/dist/index.cjs +0 -59
  194. package/dist/index.d.ts +0 -13
  195. package/dist/index.js +0 -43
  196. package/dist/loader/index.d.ts +0 -5
  197. package/dist/loader/index.js +0 -5
  198. package/dist/loader/security-validator.d.ts +0 -5
  199. package/dist/loader/security-validator.js +0 -50
  200. package/dist/loader/types.d.ts +0 -30
  201. package/dist/loader/types.js +0 -8
  202. package/dist/loader/unispec-loader.d.ts +0 -10
  203. package/dist/loader/unispec-loader.js +0 -81
  204. package/dist/loader/yaml-loader.d.ts +0 -10
  205. package/dist/loader/yaml-loader.js +0 -39
  206. package/dist/normalizer/core.d.ts +0 -24
  207. package/dist/normalizer/core.js +0 -29
  208. package/dist/normalizer/graphql-normalizer.d.ts +0 -8
  209. package/dist/normalizer/graphql-normalizer.js +0 -64
  210. package/dist/normalizer/index.d.ts +0 -2
  211. package/dist/normalizer/index.js +0 -3
  212. package/dist/normalizer/rest-normalizer.d.ts +0 -8
  213. package/dist/normalizer/rest-normalizer.js +0 -48
  214. package/dist/normalizer/types.d.ts +0 -7
  215. package/dist/normalizer/types.js +0 -1
  216. package/dist/normalizer/utils.d.ts +0 -14
  217. package/dist/normalizer/utils.js +0 -29
  218. package/dist/normalizer/websocket-normalizer.d.ts +0 -8
  219. package/dist/normalizer/websocket-normalizer.js +0 -78
  220. package/dist/optimizer/core.d.ts +0 -17
  221. package/dist/optimizer/core.js +0 -111
  222. package/dist/optimizer/index.d.ts +0 -4
  223. package/dist/optimizer/index.js +0 -7
  224. package/dist/optimizer/optimization-functions.d.ts +0 -32
  225. package/dist/optimizer/optimization-functions.js +0 -179
  226. package/dist/optimizer/types.d.ts +0 -28
  227. package/dist/optimizer/types.js +0 -1
  228. package/dist/optimizer/utils.d.ts +0 -7
  229. package/dist/optimizer/utils.js +0 -29
  230. package/dist/schemas/dedupe.d.ts +0 -9
  231. package/dist/schemas/dedupe.js +0 -97
  232. package/dist/schemas/index.d.ts +0 -3
  233. package/dist/schemas/index.js +0 -6
  234. package/dist/schemas/resolver.d.ts +0 -19
  235. package/dist/schemas/resolver.js +0 -37
  236. package/dist/schemas/utils.d.ts +0 -20
  237. package/dist/schemas/utils.js +0 -49
  238. package/dist/types/index.d.ts +0 -433
  239. package/dist/types/index.js +0 -1
  240. package/dist/validator/ajv-validator.d.ts +0 -15
  241. package/dist/validator/ajv-validator.js +0 -75
  242. package/dist/validator/config-validator-main.d.ts +0 -10
  243. package/dist/validator/config-validator-main.js +0 -31
  244. package/dist/validator/config-validator.d.ts +0 -5
  245. package/dist/validator/config-validator.js +0 -14
  246. package/dist/validator/index.d.ts +0 -10
  247. package/dist/validator/index.js +0 -11
  248. package/dist/validator/object-traversal.d.ts +0 -52
  249. package/dist/validator/object-traversal.js +0 -104
  250. package/dist/validator/reference-validator.d.ts +0 -31
  251. package/dist/validator/reference-validator.js +0 -230
  252. package/dist/validator/schema-references.d.ts +0 -23
  253. package/dist/validator/schema-references.js +0 -111
  254. package/dist/validator/semantic-validator.d.ts +0 -26
  255. package/dist/validator/semantic-validator.js +0 -325
  256. package/dist/validator/tests-validator.d.ts +0 -9
  257. package/dist/validator/tests-validator.js +0 -13
  258. package/dist/validator/types.d.ts +0 -29
  259. package/dist/validator/types.js +0 -1
  260. package/dist/validator/unispec-validator.d.ts +0 -15
  261. package/dist/validator/unispec-validator.js +0 -81
  262. package/dist/validator/validator-factory.d.ts +0 -10
  263. package/dist/validator/validator-factory.js +0 -73
  264. package/dist/versions.d.ts +0 -10
  265. package/dist/versions.js +0 -143
@@ -1,115 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.optimizeDocument = optimizeDocument;
4
- exports.generateOptimizationReport = generateOptimizationReport;
5
- const optimization_functions_1 = require("./optimization-functions.js");
6
- /**
7
- * Optimize a UniSpec document with various optimization strategies.
8
- *
9
- * @param doc - The UniSpec document to optimize
10
- * @param options - Optimization options
11
- * @returns Optimization result with statistics and changes
12
- */
13
- function optimizeDocument(doc, options = {}) {
14
- const changes = [];
15
- const originalSchemaCount = Object.keys(doc.service?.schemas || {}).length;
16
- // Create a deep copy to avoid mutating the original
17
- const optimizedDoc = JSON.parse(JSON.stringify(doc));
18
- // Ensure service and schemas exist
19
- if (!optimizedDoc.service) {
20
- optimizedDoc.service = { name: "" };
21
- }
22
- if (!optimizedDoc.service.schemas) {
23
- optimizedDoc.service.schemas = {};
24
- }
25
- let deduplicatedCount = 0;
26
- // Apply optimizations in order
27
- if (options.removeUnusedSchemas === true) {
28
- const removed = (0, optimization_functions_1.removeOrphanedSchemas)(optimizedDoc);
29
- if (removed.length > 0) {
30
- changes.push(`Removed ${removed.length} orphaned schemas: ${removed.join(", ")}`);
31
- }
32
- }
33
- if (options.dedupeSchemas !== false) {
34
- const dedupResult = (0, optimization_functions_1.deduplicateSchemas)(optimizedDoc);
35
- if (dedupResult.removedSchemas.length > 0) {
36
- changes.push(`Deduplicated ${dedupResult.removedSchemas.length} schemas: ${dedupResult.removedSchemas.join(", ")}`);
37
- changes.push(`Updated ${dedupResult.updatedReferences} schema references`);
38
- deduplicatedCount = dedupResult.removedSchemas.length;
39
- }
40
- }
41
- if (options.sortSchemas !== false) {
42
- const sortedCount = (0, optimization_functions_1.sortSchemas)(optimizedDoc);
43
- if (sortedCount > 0) {
44
- changes.push(`Sorted ${sortedCount} schemas alphabetically`);
45
- }
46
- }
47
- if (options.compressReferences !== false) {
48
- const compressedCount = (0, optimization_functions_1.compressSchemaReferences)(optimizedDoc);
49
- if (compressedCount > 0) {
50
- changes.push(`Compressed ${compressedCount} schema references`);
51
- }
52
- }
53
- const finalSchemaCount = Object.keys(optimizedDoc.service.schemas).length;
54
- return {
55
- document: optimizedDoc,
56
- statistics: {
57
- originalSchemas: originalSchemaCount,
58
- optimizedSchemas: finalSchemaCount,
59
- removedSchemas: originalSchemaCount - finalSchemaCount,
60
- deduplicatedSchemas: deduplicatedCount,
61
- },
62
- changes,
63
- };
64
- }
65
- /**
66
- * Generate optimization report for a UniSpec document.
67
- *
68
- * @param doc - The UniSpec document to analyze
69
- * @returns Optimization recommendations
70
- */
71
- function generateOptimizationReport(doc) {
72
- const recommendations = [];
73
- // Create copies for analysis to avoid mutating original
74
- const docForDedup = JSON.parse(JSON.stringify(doc));
75
- const _docForOrphaned = JSON.parse(JSON.stringify(doc));
76
- // First find duplicates
77
- const dedupResult = (0, optimization_functions_1.deduplicateSchemas)(docForDedup);
78
- // Find orphaned schemas that are NOT duplicates
79
- const allSchemas = Object.keys(doc.service?.schemas || {});
80
- const duplicateNames = new Set(dedupResult.removedSchemas);
81
- // Find referenced schemas
82
- const referencedSchemas = new Set();
83
- function findReferences(obj) {
84
- if (!obj || typeof obj !== "object")
85
- return;
86
- for (const [key, value] of Object.entries(obj)) {
87
- if (key === "schemaRef" && typeof value === "string") {
88
- const schemaName = value.split("/").pop(); // Simple extraction for analysis
89
- if (schemaName)
90
- referencedSchemas.add(schemaName);
91
- }
92
- else if (typeof value === "object" && value !== null) {
93
- findReferences(value);
94
- }
95
- }
96
- }
97
- findReferences(doc);
98
- // Find orphaned schemas that are not duplicates
99
- const orphaned = allSchemas.filter((name) => !referencedSchemas.has(name) && !duplicateNames.has(name));
100
- if (orphaned.length > 0) {
101
- recommendations.push(`Remove ${orphaned.length} orphaned schemas to reduce document size`);
102
- }
103
- if (dedupResult.removedSchemas.length > 0) {
104
- recommendations.push(`Deduplicate ${dedupResult.removedSchemas.length} identical schemas`);
105
- }
106
- const totalSchemas = Object.keys(doc.service?.schemas || {}).length;
107
- const potentialReduction = orphaned.length + dedupResult.removedSchemas.length;
108
- return {
109
- recommendations,
110
- potentialSavings: {
111
- schemaReduction: potentialReduction,
112
- referenceCompression: totalSchemas * 0.1, // Estimate 10% compression
113
- },
114
- };
115
- }
@@ -1,17 +0,0 @@
1
- "use strict";
2
- // Export types
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.compressReference = exports.sortSchemas = exports.removeOrphanedSchemas = exports.deduplicateSchemas = exports.compressSchemaReferences = exports.optimizeDocument = exports.generateOptimizationReport = void 0;
5
- // Export core functions
6
- var core_1 = require("./core.js");
7
- Object.defineProperty(exports, "generateOptimizationReport", { enumerable: true, get: function () { return core_1.generateOptimizationReport; } });
8
- Object.defineProperty(exports, "optimizeDocument", { enumerable: true, get: function () { return core_1.optimizeDocument; } });
9
- // Export optimization functions
10
- var optimization_functions_1 = require("./optimization-functions.js");
11
- Object.defineProperty(exports, "compressSchemaReferences", { enumerable: true, get: function () { return optimization_functions_1.compressSchemaReferences; } });
12
- Object.defineProperty(exports, "deduplicateSchemas", { enumerable: true, get: function () { return optimization_functions_1.deduplicateSchemas; } });
13
- Object.defineProperty(exports, "removeOrphanedSchemas", { enumerable: true, get: function () { return optimization_functions_1.removeOrphanedSchemas; } });
14
- Object.defineProperty(exports, "sortSchemas", { enumerable: true, get: function () { return optimization_functions_1.sortSchemas; } });
15
- // Export utilities
16
- var utils_1 = require("./utils.js");
17
- Object.defineProperty(exports, "compressReference", { enumerable: true, get: function () { return utils_1.compressReference; } });
@@ -1,185 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.deduplicateSchemas = deduplicateSchemas;
4
- exports.removeOrphanedSchemas = removeOrphanedSchemas;
5
- exports.sortSchemas = sortSchemas;
6
- exports.compressSchemaReferences = compressSchemaReferences;
7
- const schemas_1 = require("../schemas/index.js");
8
- const utils_1 = require("./utils.js");
9
- /**
10
- * Remove duplicate schemas from a UniSpec document.
11
- * Duplicates are identified by identical JSON Schema content.
12
- * Updates all schema references to point to the retained schema.
13
- *
14
- * @param doc - The UniSpec document to deduplicate
15
- * @returns Object with deduplication statistics
16
- */
17
- function deduplicateSchemas(doc) {
18
- if (!doc.service?.schemas) {
19
- return { removedSchemas: [], updatedReferences: 0 };
20
- }
21
- // First, find all referenced schemas
22
- const referencedSchemas = new Set();
23
- function findReferences(obj) {
24
- if (!obj || typeof obj !== "object")
25
- return;
26
- for (const [key, value] of Object.entries(obj)) {
27
- if (key === "schemaRef" && typeof value === "string") {
28
- const schemaName = (0, schemas_1.normalizeSchemaRef)(value);
29
- if (schemaName)
30
- referencedSchemas.add(schemaName);
31
- }
32
- else if (typeof value === "object" && value !== null) {
33
- findReferences(value);
34
- }
35
- }
36
- }
37
- findReferences(doc);
38
- const names = Object.keys(doc.service.schemas);
39
- const hashToName = new Map();
40
- const renameMap = {};
41
- const removedSchemas = [];
42
- for (const name of names) {
43
- const schema = doc.service.schemas[name];
44
- // Only hash the jsonSchema content, not the name field
45
- const hash = (0, schemas_1.stableStringify)(schema?.jsonSchema ?? null);
46
- const existing = hashToName.get(hash);
47
- if (!existing) {
48
- hashToName.set(hash, name);
49
- continue;
50
- }
51
- // Found duplicate - prefer referenced schema over unreferenced one
52
- const existingIsReferenced = referencedSchemas.has(existing);
53
- const currentIsReferenced = referencedSchemas.has(name);
54
- if (existingIsReferenced && !currentIsReferenced) {
55
- // Keep existing, remove current
56
- renameMap[name] = existing;
57
- removedSchemas.push(name);
58
- }
59
- else if (!existingIsReferenced && currentIsReferenced) {
60
- // Keep current, remove existing
61
- renameMap[existing] = name;
62
- removedSchemas.push(existing);
63
- hashToName.set(hash, name); // Update to current as preferred
64
- }
65
- else {
66
- // Both referenced or both unreferenced - keep first encountered
67
- renameMap[name] = existing;
68
- removedSchemas.push(name);
69
- }
70
- }
71
- if (removedSchemas.length === 0) {
72
- return { removedSchemas: [], updatedReferences: 0 };
73
- }
74
- // Update all schema references
75
- let updatedReferences = 0;
76
- function updateReferences(obj, path = "") {
77
- if (!obj || typeof obj !== "object")
78
- return;
79
- const objRecord = obj;
80
- for (const [key, value] of Object.entries(objRecord)) {
81
- const currentPath = path ? `${path}.${key}` : key;
82
- if (key === "schemaRef" && typeof value === "string") {
83
- const schemaName = (0, schemas_1.normalizeSchemaRef)(value);
84
- if (renameMap[schemaName]) {
85
- objRecord[key] = renameMap[schemaName];
86
- updatedReferences++;
87
- }
88
- }
89
- else if (typeof value === "object" && value !== null) {
90
- updateReferences(value, currentPath);
91
- }
92
- }
93
- }
94
- updateReferences(doc);
95
- // Remove duplicate schemas
96
- for (const dup of removedSchemas) {
97
- delete doc.service.schemas[dup];
98
- }
99
- return { removedSchemas, updatedReferences };
100
- }
101
- /**
102
- * Remove orphaned schemas (not referenced anywhere).
103
- *
104
- * @param doc - The UniSpec document to optimize
105
- * @returns Array of removed schema names
106
- */
107
- function removeOrphanedSchemas(doc) {
108
- if (!doc.service?.schemas)
109
- return [];
110
- const referencedSchemas = new Set();
111
- // Find all referenced schemas
112
- function findReferences(obj) {
113
- if (!obj || typeof obj !== "object")
114
- return;
115
- for (const [key, value] of Object.entries(obj)) {
116
- if (key === "schemaRef" && typeof value === "string") {
117
- const schemaName = (0, schemas_1.normalizeSchemaRef)(value);
118
- if (schemaName)
119
- referencedSchemas.add(schemaName);
120
- }
121
- else if (typeof value === "object" && value !== null) {
122
- findReferences(value);
123
- }
124
- }
125
- }
126
- findReferences(doc);
127
- // Remove orphaned schemas
128
- const allSchemas = Object.keys(doc.service.schemas);
129
- const orphaned = allSchemas.filter((schema) => !referencedSchemas.has(schema));
130
- for (const orphan of orphaned) {
131
- delete doc.service.schemas[orphan];
132
- }
133
- return orphaned;
134
- }
135
- /**
136
- * Sort schemas alphabetically by name for consistent output.
137
- *
138
- * @param doc - The UniSpec document to optimize
139
- * @returns Number of schemas sorted
140
- */
141
- function sortSchemas(doc) {
142
- if (!doc.service?.schemas)
143
- return 0;
144
- const schemaNames = Object.keys(doc.service.schemas);
145
- if (schemaNames.length === 0)
146
- return 0;
147
- const sortedSchemas = {};
148
- schemaNames.sort().forEach((name) => {
149
- const schema = doc.service?.schemas?.[name];
150
- if (schema) {
151
- sortedSchemas[name] = schema;
152
- }
153
- });
154
- doc.service.schemas = sortedSchemas;
155
- return schemaNames.length;
156
- }
157
- /**
158
- * Compress schema references by using shortest possible format.
159
- *
160
- * @param doc - The UniSpec document to optimize
161
- * @returns Number of references compressed
162
- */
163
- function compressSchemaReferences(doc) {
164
- let compressedCount = 0;
165
- function compressReferences(obj) {
166
- if (!obj || typeof obj !== "object")
167
- return;
168
- const objRecord = obj;
169
- for (const [key, value] of Object.entries(objRecord)) {
170
- if (key === "schemaRef" && typeof value === "string") {
171
- const original = value;
172
- const compressed = (0, utils_1.compressReference)(original);
173
- if (compressed !== original) {
174
- objRecord[key] = compressed;
175
- compressedCount++;
176
- }
177
- }
178
- else if (typeof value === "object" && value !== null) {
179
- compressReferences(value);
180
- }
181
- }
182
- }
183
- compressReferences(doc);
184
- return compressedCount;
185
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,32 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.compressReference = compressReference;
4
- /**
5
- * Compress a single schema reference to its shortest form.
6
- *
7
- * @param ref - The schema reference to compress
8
- * @returns Compressed reference
9
- */
10
- function compressReference(ref) {
11
- const trimmed = ref.trim();
12
- if (trimmed.length === 0)
13
- return "";
14
- // If it's already a simple name, keep it
15
- if (!trimmed.startsWith("#") && !trimmed.includes("/")) {
16
- return trimmed;
17
- }
18
- // Convert JSON Pointer to simple name
19
- if (trimmed.startsWith("#/service/schemas/")) {
20
- const parts = trimmed.split("/");
21
- return parts[parts.length - 1];
22
- }
23
- // Convert hash format to simple name
24
- if (trimmed.startsWith("#")) {
25
- const withoutHash = trimmed.slice(1);
26
- const parts = withoutHash.split("/").filter(Boolean);
27
- return parts.length > 0 ? parts[parts.length - 1] : withoutHash;
28
- }
29
- // Convert path format to simple name
30
- const parts = trimmed.split("/").filter(Boolean);
31
- return parts.length > 0 ? parts[parts.length - 1] : trimmed;
32
- }
@@ -1,100 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.dedupeSchemas = dedupeSchemas;
4
- const utils_1 = require("./utils.js");
5
- function updateSchemaRefs(doc, mapping) {
6
- const rest = doc.service?.protocols?.rest;
7
- const websocket = doc.service?.protocols?.websocket;
8
- if (rest?.routes) {
9
- for (const route of rest.routes) {
10
- for (const params of [
11
- route.pathParams,
12
- route.queryParams,
13
- route.headers,
14
- ]) {
15
- if (!params)
16
- continue;
17
- for (const p of params) {
18
- if (!p.schemaRef)
19
- continue;
20
- const key = (0, utils_1.normalizeSchemaRef)(p.schemaRef);
21
- const next = mapping[key];
22
- if (next && next !== key)
23
- p.schemaRef = next;
24
- }
25
- }
26
- const requestContent = route.requestBody?.content;
27
- if (requestContent) {
28
- for (const media of Object.values(requestContent)) {
29
- if (!media.schemaRef)
30
- continue;
31
- const key = (0, utils_1.normalizeSchemaRef)(media.schemaRef);
32
- const next = mapping[key];
33
- if (next && next !== key)
34
- media.schemaRef = next;
35
- }
36
- }
37
- const responses = route.responses;
38
- if (responses) {
39
- for (const resp of Object.values(responses)) {
40
- const respContent = resp.content;
41
- if (!respContent)
42
- continue;
43
- for (const media of Object.values(respContent)) {
44
- if (!media.schemaRef)
45
- continue;
46
- const key = (0, utils_1.normalizeSchemaRef)(media.schemaRef);
47
- const next = mapping[key];
48
- if (next && next !== key)
49
- media.schemaRef = next;
50
- }
51
- }
52
- }
53
- }
54
- }
55
- if (websocket?.channels) {
56
- for (const channel of websocket.channels) {
57
- if (!channel.messages)
58
- continue;
59
- for (const message of channel.messages) {
60
- if (!message.schemaRef)
61
- continue;
62
- const key = (0, utils_1.normalizeSchemaRef)(message.schemaRef);
63
- const next = mapping[key];
64
- if (next && next !== key)
65
- message.schemaRef = next;
66
- }
67
- }
68
- }
69
- }
70
- /**
71
- * Remove duplicate schemas from a UniSpec document.
72
- * Duplicates are identified by identical JSON Schema content.
73
- * Updates all schema references to point to the retained schema.
74
- *
75
- * @param doc - The UniSpec document to deduplicate
76
- */
77
- function dedupeSchemas(doc) {
78
- if (!doc.service?.schemas)
79
- return;
80
- const names = Object.keys(doc.service.schemas);
81
- const hashToName = new Map();
82
- const renameMap = {};
83
- for (const name of names) {
84
- const schema = doc.service.schemas[name];
85
- const hash = (0, utils_1.stableStringify)(schema?.jsonSchema ?? null);
86
- const existing = hashToName.get(hash);
87
- if (!existing) {
88
- hashToName.set(hash, name);
89
- continue;
90
- }
91
- renameMap[name] = existing;
92
- }
93
- const duplicates = Object.keys(renameMap);
94
- if (duplicates.length === 0)
95
- return;
96
- updateSchemaRefs(doc, renameMap);
97
- for (const dup of duplicates) {
98
- delete doc.service.schemas[dup];
99
- }
100
- }
@@ -1,14 +0,0 @@
1
- "use strict";
2
- // Export utility functions
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.stableStringify = exports.normalizeSchemaRef = exports.resolveSchemaRef = exports.registerSchema = exports.dedupeSchemas = void 0;
5
- // Export deduplication functions
6
- var dedupe_1 = require("./dedupe.js");
7
- Object.defineProperty(exports, "dedupeSchemas", { enumerable: true, get: function () { return dedupe_1.dedupeSchemas; } });
8
- // Export resolver functions
9
- var resolver_1 = require("./resolver.js");
10
- Object.defineProperty(exports, "registerSchema", { enumerable: true, get: function () { return resolver_1.registerSchema; } });
11
- Object.defineProperty(exports, "resolveSchemaRef", { enumerable: true, get: function () { return resolver_1.resolveSchemaRef; } });
12
- var utils_1 = require("./utils.js");
13
- Object.defineProperty(exports, "normalizeSchemaRef", { enumerable: true, get: function () { return utils_1.normalizeSchemaRef; } });
14
- Object.defineProperty(exports, "stableStringify", { enumerable: true, get: function () { return utils_1.stableStringify; } });
@@ -1,41 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resolveSchemaRef = resolveSchemaRef;
4
- exports.registerSchema = registerSchema;
5
- const utils_1 = require("./utils.js");
6
- /**
7
- * Resolve a schema reference to its definition in a UniSpec document.
8
- *
9
- * @param doc - The UniSpec document containing schemas
10
- * @param ref - The schema reference to resolve
11
- * @returns The schema definition or undefined if not found
12
- */
13
- function resolveSchemaRef(doc, ref) {
14
- const name = (0, utils_1.normalizeSchemaRef)(ref);
15
- if (!name)
16
- return undefined;
17
- return doc.service?.schemas?.[name];
18
- }
19
- /**
20
- * Register a new schema definition in a UniSpec document.
21
- * Creates the schemas container if it doesn't exist.
22
- *
23
- * @param doc - The UniSpec document to modify
24
- * @param name - The schema name
25
- * @param jsonSchema - The JSON Schema definition
26
- * @returns The created schema definition
27
- */
28
- function registerSchema(doc, name, jsonSchema) {
29
- if (!doc.service) {
30
- doc.service = { name: "" };
31
- }
32
- if (!doc.service.schemas) {
33
- doc.service.schemas = {};
34
- }
35
- const definition = {
36
- name,
37
- jsonSchema,
38
- };
39
- doc.service.schemas[name] = definition;
40
- return definition;
41
- }
@@ -1,53 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.normalizeSchemaRef = normalizeSchemaRef;
4
- exports.stableStringify = stableStringify;
5
- /**
6
- * Normalize a schema reference to extract the schema name.
7
- * Supports multiple formats:
8
- * - Simple name: "User"
9
- * - JSON Pointer: "#/service/schemas/User"
10
- * - Hash format: "#User"
11
- * - Path format: "path/to/User"
12
- *
13
- * @param ref - The schema reference to normalize
14
- * @returns The normalized schema name
15
- */
16
- function normalizeSchemaRef(ref) {
17
- const trimmed = ref.trim();
18
- if (trimmed.length === 0)
19
- return "";
20
- // Handle JSON Pointer format: #/service/schemas/SchemaName
21
- if (trimmed.startsWith("#/service/schemas/")) {
22
- const parts = trimmed.split("/");
23
- return parts[parts.length - 1];
24
- }
25
- // Handle simple name format: SchemaName
26
- const withoutHash = trimmed.startsWith("#") ? trimmed.slice(1) : trimmed;
27
- const parts = withoutHash.split("/").filter(Boolean);
28
- return parts.length > 0 ? parts[parts.length - 1] : withoutHash;
29
- }
30
- /**
31
- * Create a stable string representation of a value for comparison.
32
- * Used for schema deduplication by creating consistent JSON strings.
33
- *
34
- * @param value - The value to stringify
35
- * @returns A stable JSON string representation
36
- */
37
- function stableStringify(value) {
38
- if (value === null)
39
- return "null";
40
- const t = typeof value;
41
- if (t === "number" || t === "boolean")
42
- return JSON.stringify(value);
43
- if (t === "string")
44
- return JSON.stringify(value);
45
- if (Array.isArray(value))
46
- return `[${value.map(stableStringify).join(",")}]`;
47
- if (t !== "object")
48
- return JSON.stringify(value);
49
- const obj = value;
50
- const keys = Object.keys(obj).sort();
51
- const entries = keys.map((k) => `${JSON.stringify(k)}:${stableStringify(obj[k])}`);
52
- return `{${entries.join(",")}}`;
53
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,82 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getValidator = getValidator;
7
- exports.clearValidatorCache = clearValidatorCache;
8
- const _2020_js_1 = __importDefault(require("ajv/dist/2020.js"));
9
- const generated_schemas_1 = require("../generated-schemas.js");
10
- let cached = null;
11
- /**
12
- * Load default schemas from generated schemas.
13
- */
14
- async function loadDefaultSchemas() {
15
- // Always use generated schemas - they work in all environments
16
- return {
17
- unispec: generated_schemas_1.GENERATED_SCHEMAS.unispec,
18
- unispecTests: generated_schemas_1.GENERATED_SCHEMAS.unispecTests,
19
- unispecConfig: generated_schemas_1.GENERATED_SCHEMAS.unispecConfig,
20
- subschemas: generated_schemas_1.GENERATED_SCHEMAS.subschemas,
21
- };
22
- }
23
- /**
24
- * Create AJV instance with custom formats and options.
25
- */
26
- function createAjv(options) {
27
- const ajv = new _2020_js_1.default({
28
- allErrors: true,
29
- strict: false, // Disabled to support discriminator keyword in new schema
30
- ...(options.ajvOptions ?? {}),
31
- });
32
- // Add format validators
33
- ajv.addFormat("uri", true);
34
- ajv.addFormat("email", {
35
- type: "string",
36
- validate: (data) => {
37
- // Basic email validation regex
38
- const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
39
- return typeof data === "string" && emailRegex.test(data);
40
- },
41
- });
42
- return ajv;
43
- }
44
- /**
45
- * Get compiled AJV validators for different document types.
46
- */
47
- async function getValidator(options = {}) {
48
- if (cached &&
49
- !options.schemas &&
50
- !options.schemaProvider &&
51
- !options.ajvOptions)
52
- return cached;
53
- const schemas = options.schemas ?? (await loadDefaultSchemas());
54
- const ajv = createAjv(options);
55
- if (schemas.subschemas?.length) {
56
- ajv.addSchema(schemas.subschemas);
57
- }
58
- const validateUniSpecFn = ajv.compile(schemas.unispec);
59
- let validateUniSpecTestsFn;
60
- if (schemas.unispecTests) {
61
- validateUniSpecTestsFn = ajv.compile(schemas.unispecTests);
62
- }
63
- let validateUniSpecConfigFn;
64
- if (schemas.unispecConfig) {
65
- validateUniSpecConfigFn = ajv.compile(schemas.unispecConfig);
66
- }
67
- const result = {
68
- validateUniSpecFn,
69
- validateUniSpecTestsFn,
70
- validateUniSpecConfigFn,
71
- };
72
- if (!options.schemas && !options.schemaProvider && !options.ajvOptions) {
73
- cached = result;
74
- }
75
- return result;
76
- }
77
- /**
78
- * Clear cached validators (useful for testing or when schemas change).
79
- */
80
- function clearValidatorCache() {
81
- cached = null;
82
- }