@pdtf/schemas 3.6.0-dev.1 → 3.6.0-dev.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 (132) hide show
  1. package/package.json +1 -1
  2. package/pensive-tesla-d0d355/CLAUDE.md +101 -0
  3. package/pensive-tesla-d0d355/LICENSE +21 -0
  4. package/pensive-tesla-d0d355/README.md +435 -0
  5. package/pensive-tesla-d0d355/docs/branch-release-workflow.md +138 -0
  6. package/pensive-tesla-d0d355/docs/sef25-extensions-ui-spec.md +355 -0
  7. package/pensive-tesla-d0d355/index.js +654 -0
  8. package/pensive-tesla-d0d355/package-lock.json +7297 -0
  9. package/pensive-tesla-d0d355/package.json +44 -0
  10. package/pensive-tesla-d0d355/src/examples/v1/exampleAddParticipantVouch.json +32 -0
  11. package/pensive-tesla-d0d355/src/examples/v1/exampleDocumentedVouch.json +40 -0
  12. package/pensive-tesla-d0d355/src/examples/v1/exampleElectronicRecord.json +30 -0
  13. package/pensive-tesla-d0d355/src/examples/v1/exampleTransaction.json +878 -0
  14. package/pensive-tesla-d0d355/src/examples/v1/exampleVouch.json +25 -0
  15. package/pensive-tesla-d0d355/src/examples/v2/exampleTransaction.json +901 -0
  16. package/pensive-tesla-d0d355/src/examples/v3/exampleAddParticipantVouch.json +38 -0
  17. package/pensive-tesla-d0d355/src/examples/v3/exampleCustomOverlay.json +55 -0
  18. package/pensive-tesla-d0d355/src/examples/v3/exampleCustomOverlay2.json +93 -0
  19. package/pensive-tesla-d0d355/src/examples/v3/exampleDocumentedVouch.json +47 -0
  20. package/pensive-tesla-d0d355/src/examples/v3/exampleElectronicRecord.json +36 -0
  21. package/pensive-tesla-d0d355/src/examples/v3/exampleTransaction.json +1038 -0
  22. package/pensive-tesla-d0d355/src/examples/v3/exampleVouch.json +32 -0
  23. package/pensive-tesla-d0d355/src/mappings/SendPropertyDetailsRequestSchema.json +1588 -0
  24. package/pensive-tesla-d0d355/src/mappings/adf.js +198 -0
  25. package/pensive-tesla-d0d355/src/schemas/v1/GeoJSON.json +1279 -0
  26. package/pensive-tesla-d0d355/src/schemas/v1/baspiNotes.html +102 -0
  27. package/pensive-tesla-d0d355/src/schemas/v1/energy-performance-certificate.json +124 -0
  28. package/pensive-tesla-d0d355/src/schemas/v1/freehold-information.json +2051 -0
  29. package/pensive-tesla-d0d355/src/schemas/v1/leasehold-information.json +2064 -0
  30. package/pensive-tesla-d0d355/src/schemas/v1/legal-information.json +1045 -0
  31. package/pensive-tesla-d0d355/src/schemas/v1/material-facts.json +2109 -0
  32. package/pensive-tesla-d0d355/src/schemas/v1/pdtf-transaction-search-results.json +40 -0
  33. package/pensive-tesla-d0d355/src/schemas/v1/pdtf-transaction.json +191 -0
  34. package/pensive-tesla-d0d355/src/schemas/v1/searches/drainage-and-water.json +261 -0
  35. package/pensive-tesla-d0d355/src/schemas/v1/searches/local-land-charges.json +69 -0
  36. package/pensive-tesla-d0d355/src/schemas/v1/searches/local-searches-required.json +857 -0
  37. package/pensive-tesla-d0d355/src/schemas/v1/searches.json +237 -0
  38. package/pensive-tesla-d0d355/src/schemas/v1/title-deed.json +841 -0
  39. package/pensive-tesla-d0d355/src/schemas/v2/baspiNotes.html +102 -0
  40. package/pensive-tesla-d0d355/src/schemas/v2/combined.json +37731 -0
  41. package/pensive-tesla-d0d355/src/schemas/v2/overlays/baspi.json +8132 -0
  42. package/pensive-tesla-d0d355/src/schemas/v2/overlays/con29DW.json +705 -0
  43. package/pensive-tesla-d0d355/src/schemas/v2/overlays/con29R.json +2956 -0
  44. package/pensive-tesla-d0d355/src/schemas/v2/overlays/fme1.json +1259 -0
  45. package/pensive-tesla-d0d355/src/schemas/v2/overlays/llc1.json +25 -0
  46. package/pensive-tesla-d0d355/src/schemas/v2/overlays/lpe1.json +2171 -0
  47. package/pensive-tesla-d0d355/src/schemas/v2/overlays/nts.json +326 -0
  48. package/pensive-tesla-d0d355/src/schemas/v2/overlays/oc1.json +2799 -0
  49. package/pensive-tesla-d0d355/src/schemas/v2/overlays/piq.json +3419 -0
  50. package/pensive-tesla-d0d355/src/schemas/v2/overlays/rds.json +3736 -0
  51. package/pensive-tesla-d0d355/src/schemas/v2/overlays/ta10.json +5306 -0
  52. package/pensive-tesla-d0d355/src/schemas/v2/overlays/ta6.json +4533 -0
  53. package/pensive-tesla-d0d355/src/schemas/v2/overlays/ta7.json +1045 -0
  54. package/pensive-tesla-d0d355/src/schemas/v2/pdtf-transaction.json +34351 -0
  55. package/pensive-tesla-d0d355/src/schemas/v2/skeleton.json +3973 -0
  56. package/pensive-tesla-d0d355/src/schemas/v3/combined.json +48469 -0
  57. package/pensive-tesla-d0d355/src/schemas/v3/compactSkeleton.txt +4519 -0
  58. package/pensive-tesla-d0d355/src/schemas/v3/overlays/README.md +299 -0
  59. package/pensive-tesla-d0d355/src/schemas/v3/overlays/baspi4.json +8025 -0
  60. package/pensive-tesla-d0d355/src/schemas/v3/overlays/baspi5.json +8175 -0
  61. package/pensive-tesla-d0d355/src/schemas/v3/overlays/con29DW.json +716 -0
  62. package/pensive-tesla-d0d355/src/schemas/v3/overlays/con29R.json +2981 -0
  63. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/ac.json +333 -0
  64. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/as.json +74 -0
  65. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/dk.json +32 -0
  66. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/dr.json +73 -0
  67. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/er.json +85 -0
  68. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/fd.json +75 -0
  69. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/hi.json +51 -0
  70. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/hs.json +73 -0
  71. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/ic.json +59 -0
  72. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/jk.json +96 -0
  73. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/la.json +85 -0
  74. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/lc.json +75 -0
  75. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/ma.json +90 -0
  76. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/mc.json +38 -0
  77. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/mi.json +69 -0
  78. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/nd.json +43 -0
  79. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/oa.json +24 -0
  80. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/oc.json +57 -0
  81. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/pc.json +37 -0
  82. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/ph.json +194 -0
  83. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/rw.json +68 -0
  84. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/sb.json +73 -0
  85. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/sc.json +129 -0
  86. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/sd.json +63 -0
  87. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/sf.json +74 -0
  88. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/sl.json +68 -0
  89. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/ta.json +252 -0
  90. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/tf.json +103 -0
  91. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/tr.json +73 -0
  92. package/pensive-tesla-d0d355/src/schemas/v3/overlays/extensions/wg.json +350 -0
  93. package/pensive-tesla-d0d355/src/schemas/v3/overlays/fme1.json +1266 -0
  94. package/pensive-tesla-d0d355/src/schemas/v3/overlays/llc1.json +25 -0
  95. package/pensive-tesla-d0d355/src/schemas/v3/overlays/lpe1.json +2199 -0
  96. package/pensive-tesla-d0d355/src/schemas/v3/overlays/nts.json +2157 -0
  97. package/pensive-tesla-d0d355/src/schemas/v3/overlays/nts2.json +2742 -0
  98. package/pensive-tesla-d0d355/src/schemas/v3/overlays/ntsl.json +1813 -0
  99. package/pensive-tesla-d0d355/src/schemas/v3/overlays/ntsl2.json +2217 -0
  100. package/pensive-tesla-d0d355/src/schemas/v3/overlays/oc1.json +2817 -0
  101. package/pensive-tesla-d0d355/src/schemas/v3/overlays/piq.json +3322 -0
  102. package/pensive-tesla-d0d355/src/schemas/v3/overlays/rds.json +3665 -0
  103. package/pensive-tesla-d0d355/src/schemas/v3/overlays/sef25.json +1639 -0
  104. package/pensive-tesla-d0d355/src/schemas/v3/overlays/sr24.json +38 -0
  105. package/pensive-tesla-d0d355/src/schemas/v3/overlays/ta10.json +4654 -0
  106. package/pensive-tesla-d0d355/src/schemas/v3/overlays/ta6.json +4568 -0
  107. package/pensive-tesla-d0d355/src/schemas/v3/overlays/ta6ed6.json +6534 -0
  108. package/pensive-tesla-d0d355/src/schemas/v3/overlays/ta7.json +1537 -0
  109. package/pensive-tesla-d0d355/src/schemas/v3/overlays/ta7ed5.json +1551 -0
  110. package/pensive-tesla-d0d355/src/schemas/v3/pdtf-transaction.json +41573 -0
  111. package/pensive-tesla-d0d355/src/schemas/v3/skeleton.json +5767 -0
  112. package/pensive-tesla-d0d355/src/schemas/verifiedClaims/README.md +21 -0
  113. package/pensive-tesla-d0d355/src/schemas/verifiedClaims/pdtf-verified-claims.json +447 -0
  114. package/pensive-tesla-d0d355/src/utils/compactSkeleton.js +84 -0
  115. package/pensive-tesla-d0d355/src/utils/countTokens.js +67 -0
  116. package/pensive-tesla-d0d355/src/utils/extractExtensionOverlays.js +624 -0
  117. package/pensive-tesla-d0d355/src/utils/extractOverlay.js +381 -0
  118. package/pensive-tesla-d0d355/src/utils/extractProperties.js +44 -0
  119. package/pensive-tesla-d0d355/src/utils/listPathTitles.js +36 -0
  120. package/pensive-tesla-d0d355/src/utils/minimalSkeleton.js +85 -0
  121. package/pensive-tesla-d0d355/src/utils/pathList.csv +903 -0
  122. package/pensive-tesla-d0d355/src/utils/pathSkeleton.js +181 -0
  123. package/src/schemas/v3/combined.json +323 -5
  124. package/src/schemas/v3/compactSkeleton.txt +63 -0
  125. package/src/schemas/v3/overlays/baspi4.json +2 -1
  126. package/src/schemas/v3/overlays/baspi5.json +2 -1
  127. package/src/schemas/v3/overlays/extensions/ta.json +0 -48
  128. package/src/schemas/v3/overlays/piq.json +2 -1
  129. package/src/schemas/v3/overlays/ta6ed6.json +2 -1
  130. package/src/schemas/v3/pdtf-transaction.json +336 -2
  131. package/src/schemas/v3/skeleton.json +68 -3
  132. package/docs/backend-lms-profile-filtering-spec.md +0 -180
@@ -0,0 +1,654 @@
1
+ const merge = require("deepmerge");
2
+ const Ajv = require("ajv");
3
+ const addFormats = require("ajv-formats");
4
+ const ajv = new Ajv({
5
+ allErrors: true,
6
+ // schema contains additional baspiRef and RDSRef metadata which is not strictly valid
7
+ strictSchema: false,
8
+ discriminator: true,
9
+ });
10
+ // Adds date formats among other types to the validator.
11
+ addFormats(ajv);
12
+
13
+ // Enhanced caching structure for subschemas and validators
14
+ const schemaCache = new Map();
15
+
16
+ // Cache performance tracking
17
+ const cacheStats = {
18
+ hits: 0,
19
+ misses: 0,
20
+ totalQueries: 0,
21
+ cacheStartTime: Date.now(),
22
+ };
23
+
24
+ const verifiedClaimsSchema = require("./src/schemas/verifiedClaims/pdtf-verified-claims.json");
25
+ const v2CoreSchema = require("./src/schemas/v2/pdtf-transaction.json");
26
+ const v3CoreSchema = require("./src/schemas/v3/pdtf-transaction.json");
27
+
28
+ // Extension overlays for v3
29
+ const extensionOverlays = {
30
+ // Specialist Issues Extensions
31
+ as: require("./src/schemas/v3/overlays/extensions/as.json"),
32
+ dr: require("./src/schemas/v3/overlays/extensions/dr.json"),
33
+ jk: require("./src/schemas/v3/overlays/extensions/jk.json"),
34
+ sb: require("./src/schemas/v3/overlays/extensions/sb.json"),
35
+ hs: require("./src/schemas/v3/overlays/extensions/hs.json"),
36
+
37
+ // Property Features Extensions
38
+ oa: require("./src/schemas/v3/overlays/extensions/oa.json"),
39
+ la: require("./src/schemas/v3/overlays/extensions/la.json"),
40
+ sf: require("./src/schemas/v3/overlays/extensions/sf.json"),
41
+ mc: require("./src/schemas/v3/overlays/extensions/mc.json"),
42
+
43
+ // Ownership & Financial Extensions
44
+ er: require("./src/schemas/v3/overlays/extensions/er.json"),
45
+ ma: require("./src/schemas/v3/overlays/extensions/ma.json"),
46
+ tf: require("./src/schemas/v3/overlays/extensions/tf.json"),
47
+
48
+ // Utilities & Services Extensions
49
+ sl: require("./src/schemas/v3/overlays/extensions/sl.json"),
50
+ hi: require("./src/schemas/v3/overlays/extensions/hi.json"),
51
+ fd: require("./src/schemas/v3/overlays/extensions/fd.json"),
52
+
53
+ // Transaction Extensions
54
+ oc: require("./src/schemas/v3/overlays/extensions/oc.json"),
55
+
56
+ // SEF25 Extensions
57
+ sc: require("./src/schemas/v3/overlays/extensions/sc.json"),
58
+ pc: require("./src/schemas/v3/overlays/extensions/pc.json"),
59
+ ph: require("./src/schemas/v3/overlays/extensions/ph.json"),
60
+ dk: require("./src/schemas/v3/overlays/extensions/dk.json"),
61
+ rw: require("./src/schemas/v3/overlays/extensions/rw.json"),
62
+ sd: require("./src/schemas/v3/overlays/extensions/sd.json"),
63
+ lc: require("./src/schemas/v3/overlays/extensions/lc.json"),
64
+ wg: require("./src/schemas/v3/overlays/extensions/wg.json"),
65
+ ic: require("./src/schemas/v3/overlays/extensions/ic.json"),
66
+ nd: require("./src/schemas/v3/overlays/extensions/nd.json"),
67
+ mi: require("./src/schemas/v3/overlays/extensions/mi.json"),
68
+ tr: require("./src/schemas/v3/overlays/extensions/tr.json"),
69
+ ac: require("./src/schemas/v3/overlays/extensions/ac.json"),
70
+
71
+ // Compatibility Extensions
72
+ ta: require("./src/schemas/v3/overlays/extensions/ta.json"),
73
+ };
74
+
75
+ const overlaysMap = {
76
+ "https://trust.propdata.org.uk/schemas/v2/pdtf-transaction.json": {
77
+ baspiV4: require("./src/schemas/v2/overlays/baspi.json"),
78
+ ta6ed4: require("./src/schemas/v2/overlays/ta6.json"),
79
+ ta7ed3: require("./src/schemas/v2/overlays/ta7.json"),
80
+ ta10ed3: require("./src/schemas/v2/overlays/ta10.json"),
81
+ lpe1ed4: require("./src/schemas/v2/overlays/lpe1.json"),
82
+ fme1ed2: require("./src/schemas/v2/overlays/fme1.json"),
83
+ llc1v2: require("./src/schemas/v2/overlays/llc1.json"),
84
+ nts2023: require("./src/schemas/v2/overlays/nts.json"),
85
+ con29R2019: require("./src/schemas/v2/overlays/con29R.json"),
86
+ con29DW: require("./src/schemas/v2/overlays/con29DW.json"),
87
+ rdsV333: require("./src/schemas/v2/overlays/rds.json"),
88
+ oc1v21: require("./src/schemas/v2/overlays/oc1.json"),
89
+ piqV3: require("./src/schemas/v2/overlays/piq.json"),
90
+ null: {},
91
+ },
92
+ "https://trust.propdata.org.uk/schemas/v3/pdtf-transaction.json": {
93
+ baspiV4: require("./src/schemas/v3/overlays/baspi4.json"),
94
+ baspiV5: require("./src/schemas/v3/overlays/baspi5.json"),
95
+ ta6ed4: require("./src/schemas/v3/overlays/ta6.json"),
96
+ ta6ed6: require("./src/schemas/v3/overlays/ta6ed6.json"),
97
+ ta7ed3: require("./src/schemas/v3/overlays/ta7.json"),
98
+ ta7ed5: require("./src/schemas/v3/overlays/ta7ed5.json"),
99
+ ta10ed3: require("./src/schemas/v3/overlays/ta10.json"),
100
+ lpe1ed4: require("./src/schemas/v3/overlays/lpe1.json"),
101
+ fme1ed2: require("./src/schemas/v3/overlays/fme1.json"),
102
+ llc1v2: require("./src/schemas/v3/overlays/llc1.json"),
103
+ nts2023: require("./src/schemas/v3/overlays/nts.json"),
104
+ ntsl2023: require("./src/schemas/v3/overlays/ntsl.json"),
105
+ nts2025: require("./src/schemas/v3/overlays/nts2.json"),
106
+ ntsl2025: require("./src/schemas/v3/overlays/ntsl2.json"),
107
+ con29R2019: require("./src/schemas/v3/overlays/con29R.json"),
108
+ con29DW: require("./src/schemas/v3/overlays/con29DW.json"),
109
+ rdsV333: require("./src/schemas/v3/overlays/rds.json"),
110
+ oc1v21: require("./src/schemas/v3/overlays/oc1.json"),
111
+ piqV3: require("./src/schemas/v3/overlays/piq.json"),
112
+ sr24: require("./src/schemas/v3/overlays/sr24.json"),
113
+ // Include extension overlays for v3
114
+ ...extensionOverlays,
115
+ null: {},
116
+ },
117
+ };
118
+
119
+ const transactionSchemas = {
120
+ "https://trust.propdata.org.uk/schemas/v2/pdtf-transaction.json":
121
+ v2CoreSchema,
122
+ "https://trust.propdata.org.uk/schemas/v3/pdtf-transaction.json":
123
+ v3CoreSchema,
124
+ };
125
+
126
+ // Custom array merge function for oneOf arrays
127
+ const arrayMerge = (target, source, options) => {
128
+ // For string arrays (enum, required, etc.)
129
+ if (
130
+ target.length > 0 &&
131
+ source.length > 0 &&
132
+ target[0] &&
133
+ typeof target[0] === "string" &&
134
+ source[0] &&
135
+ typeof source[0] === "string"
136
+ ) {
137
+ // Detect if this is a required array by checking the parent key
138
+ // Required arrays are property names (camelCase, no spaces)
139
+ // Enum arrays are values (can contain spaces, capitals, etc.)
140
+ const isRequiredArray =
141
+ target.every(
142
+ (item) =>
143
+ typeof item === "string" &&
144
+ item.length > 0 &&
145
+ // Required field names are typically camelCase without spaces
146
+ !item.includes(" ") &&
147
+ // First character is usually lowercase for field names
148
+ (item[0] === item[0].toLowerCase() ||
149
+ item.startsWith("is") ||
150
+ item.startsWith("has")),
151
+ ) &&
152
+ source.every(
153
+ (item) =>
154
+ typeof item === "string" &&
155
+ item.length > 0 &&
156
+ !item.includes(" ") &&
157
+ (item[0] === item[0].toLowerCase() ||
158
+ item.startsWith("is") ||
159
+ item.startsWith("has")),
160
+ );
161
+
162
+ if (isRequiredArray) {
163
+ // Combine required arrays
164
+ const combined = [...target];
165
+ source.forEach((item) => {
166
+ if (!combined.includes(item)) {
167
+ combined.push(item);
168
+ }
169
+ });
170
+ return combined;
171
+ }
172
+
173
+ // For other string arrays (like enum), replace with source
174
+ return source.length > 0 ? source : target;
175
+ }
176
+
177
+ // If target is empty but source has values, use source
178
+ if (target.length === 0 && source.length > 0) {
179
+ return source;
180
+ }
181
+
182
+ // Ensure we only process arrays of the same length
183
+ // If source is shorter, pad with nulls
184
+ // If source is longer, truncate to target length
185
+ const paddedSource = [...source];
186
+ while (paddedSource.length < target.length) {
187
+ paddedSource.push(null);
188
+ }
189
+ paddedSource.splice(target.length);
190
+
191
+ return target.map((item, index) => {
192
+ if (paddedSource[index] === null || paddedSource[index] === undefined) {
193
+ return item;
194
+ }
195
+ if (item === null || item === undefined) {
196
+ return paddedSource[index];
197
+ }
198
+ // Recursively merge nested objects and arrays
199
+ return merge(item, paddedSource[index], { arrayMerge });
200
+ });
201
+ };
202
+
203
+ const getTransactionSchema = (
204
+ schemaId = "https://trust.propdata.org.uk/schemas/v3/pdtf-transaction.json",
205
+ overlays,
206
+ ) => {
207
+ const sourceSchema = transactionSchemas[schemaId];
208
+ if (!overlays || overlays.length < 1) return sourceSchema;
209
+
210
+ let mergedSchema = JSON.parse(JSON.stringify(sourceSchema)); // Deep copy
211
+ overlays.forEach((overlay) => {
212
+ const overlaySchema = JSON.parse(
213
+ JSON.stringify(overlaysMap[schemaId][overlay] || {}),
214
+ ); // Deep copy
215
+ mergedSchema = merge(mergedSchema, overlaySchema, { arrayMerge });
216
+ });
217
+
218
+ return mergedSchema;
219
+ };
220
+
221
+ // Add helper function to generate cache key
222
+ const generateOverlayKey = (overlays) => {
223
+ if (!overlays) return "";
224
+ return overlays.join(".");
225
+ };
226
+
227
+ // Enhanced caching function that stores both subschemas and validators
228
+ const getCachedSchemaData = (path, schemaId, overlays) => {
229
+ const overlayKey = generateOverlayKey(overlays);
230
+ const cacheKey = `${path}-${schemaId}-${overlayKey}`;
231
+
232
+ cacheStats.totalQueries++;
233
+ let cached = schemaCache.get(cacheKey);
234
+ if (!cached) {
235
+ cacheStats.misses++;
236
+ // Compute subschema using the original logic
237
+ const sourceSchema = getTransactionSchema(schemaId, overlays);
238
+ const pathArray = path.split("/").slice(1);
239
+ let subSchema = sourceSchema;
240
+
241
+ if (pathArray.length >= 1) {
242
+ subSchema = pathArray.reduce((schema, pathElement) => {
243
+ if (!schema) return undefined;
244
+ const { type, items, properties, patternProperties, oneOf } = schema;
245
+
246
+ // Check array type
247
+ if (type === "array") return items;
248
+
249
+ // Check explicit properties first
250
+ if (properties?.[pathElement]) return properties[pathElement];
251
+
252
+ // Check patternProperties
253
+ if (patternProperties) {
254
+ for (const pattern in patternProperties) {
255
+ try {
256
+ const regex = new RegExp(pattern);
257
+ if (regex.test(pathElement)) {
258
+ return patternProperties[pattern];
259
+ }
260
+ } catch (e) {
261
+ // Invalid regex pattern in schema - skip it
262
+ console.warn(
263
+ `Invalid regex pattern in patternProperties: ${pattern}`,
264
+ e,
265
+ );
266
+ }
267
+ }
268
+ }
269
+
270
+ // Check oneOf discriminators (recursively for nested oneOf)
271
+ if (oneOf) {
272
+ const findInOneOf = (branches) => {
273
+ let arrayFallback;
274
+ for (const branch of branches) {
275
+ if (!branch) continue;
276
+ // Track array matches as fallback (prefer property matches)
277
+ if (
278
+ branch.type === "array" &&
279
+ !Number.isNaN(pathElement) &&
280
+ !arrayFallback
281
+ ) {
282
+ arrayFallback = branch.items;
283
+ }
284
+ if (branch.properties?.[pathElement]) {
285
+ return branch.properties[pathElement];
286
+ }
287
+ if (branch.patternProperties) {
288
+ for (const pattern in branch.patternProperties) {
289
+ try {
290
+ const regex = new RegExp(pattern);
291
+ if (regex.test(pathElement)) {
292
+ return branch.patternProperties[pattern];
293
+ }
294
+ } catch (e) {
295
+ // Invalid regex pattern - skip
296
+ }
297
+ }
298
+ }
299
+ // Recurse into nested oneOf branches
300
+ if (branch.oneOf) {
301
+ const nested = findInOneOf(branch.oneOf);
302
+ if (nested) return nested;
303
+ }
304
+ }
305
+ return arrayFallback;
306
+ };
307
+ const matchingProperty = findInOneOf(oneOf);
308
+ if (matchingProperty) return matchingProperty;
309
+ }
310
+
311
+ return undefined;
312
+ }, sourceSchema);
313
+ }
314
+
315
+ // Add schema to AJV and get validator
316
+ // Only add valid schemas to AJV
317
+ let validator;
318
+ if (subSchema && typeof subSchema === "object") {
319
+ // Check if schema already exists in AJV to avoid duplicates
320
+ validator = ajv.getSchema(cacheKey);
321
+ if (!validator) {
322
+ ajv.addSchema(subSchema, cacheKey);
323
+ validator = ajv.getSchema(cacheKey);
324
+ }
325
+ } else {
326
+ // For invalid paths, create a validator that always fails
327
+ validator = () => false;
328
+ validator.errors = [`Invalid path: schema is ${subSchema}`];
329
+ }
330
+
331
+ cached = {
332
+ subSchema,
333
+ validator,
334
+ cacheKey,
335
+ createdAt: Date.now(),
336
+ };
337
+ schemaCache.set(cacheKey, cached);
338
+ } else {
339
+ cacheStats.hits++;
340
+ }
341
+
342
+ return cached;
343
+ };
344
+
345
+ const getValidator = (schemaId, overlays) => {
346
+ return getSubschemaValidator("", schemaId, overlays);
347
+ };
348
+
349
+ // common functions for v1 and v2 - now with caching
350
+ const getSubschema = (path, schemaId, overlays) => {
351
+ const cached = getCachedSchemaData(path, schemaId, overlays);
352
+ return cached.subSchema;
353
+ };
354
+
355
+ const isPathValid = (path, schemaId, overlays) => {
356
+ try {
357
+ return getSubschema(path, schemaId, overlays) !== undefined;
358
+ } catch (err) {
359
+ return false;
360
+ }
361
+ };
362
+
363
+ const getSubschemaValidator = (path, schemaId, overlays) => {
364
+ const cached = getCachedSchemaData(path, schemaId, overlays);
365
+ return cached.validator;
366
+ };
367
+
368
+ // v1, deprecated
369
+ const getTitleAtPath = (schema, path, rootPath = path) => {
370
+ if (path === "") path = "/";
371
+ let pathArray = path.split("/").slice(1);
372
+ if (pathArray.length === 1 && pathArray[0] === "") {
373
+ if (schema.title) return schema.title;
374
+ if (schema.title === "") return ""; // deliberately blank
375
+ // no 'title' property present, so we use the property name to create a readable descriptor
376
+ const propertyName = rootPath
377
+ .split("/")
378
+ .pop()
379
+ .replace(/([A-Z])/g, " $1")
380
+ .toLowerCase()
381
+ .trim();
382
+ return propertyName.charAt(0).toUpperCase() + propertyName.slice(1);
383
+ }
384
+ const propertyName = pathArray.shift();
385
+ const subPath = "/" + pathArray.join("/");
386
+
387
+ let subSchema = schema.properties
388
+ ? schema.properties[propertyName]
389
+ : undefined;
390
+ if (subSchema) {
391
+ return getTitleAtPath(subSchema, subPath, rootPath);
392
+ }
393
+ if (schema.type === "array") {
394
+ subSchema = schema.items;
395
+ return getTitleAtPath(subSchema, subPath, rootPath);
396
+ }
397
+ const oneOfs = schema.oneOf;
398
+ if (oneOfs) {
399
+ // only single dependency discriminator, oneOf keyword is supported
400
+ const matchingOneOf = oneOfs.find(
401
+ (oneOf) => oneOf["properties"][propertyName],
402
+ );
403
+ if (matchingOneOf)
404
+ return getTitleAtPath(
405
+ matchingOneOf["properties"][propertyName],
406
+ subPath,
407
+ rootPath,
408
+ );
409
+ }
410
+ };
411
+
412
+ const validateVerifiedClaims = (verifiedClaims, schemaId, overlays) => {
413
+ const validatorVClaims = ajv.compile(verifiedClaimsSchema);
414
+
415
+ const validationErrorsArr = [];
416
+ const vClaimSchValidation = validatorVClaims({
417
+ verified_claims: verifiedClaims,
418
+ });
419
+
420
+ if (!vClaimSchValidation) {
421
+ validationErrorsArr.push(validatorVClaims.errors);
422
+ }
423
+
424
+ const verifiedClaimsArray = Array.isArray(verifiedClaims)
425
+ ? verifiedClaims
426
+ : [verifiedClaims];
427
+
428
+ verifiedClaimsArray.forEach((claim) => {
429
+ const paths = Object.keys(claim.claims);
430
+ for (const path of paths) {
431
+ const validPath = isPathValid(path, schemaId, overlays);
432
+ if (validPath) {
433
+ const subValidator = getSubschemaValidator(path, schemaId, overlays);
434
+ const isValid = subValidator(claim.claims[path]);
435
+ if (!isValid) {
436
+ validationErrorsArr.push(...subValidator.errors);
437
+ }
438
+ } else {
439
+ validationErrorsArr.push(
440
+ `Path ${path} is not a valid PDTF schema path`,
441
+ );
442
+ }
443
+ }
444
+ });
445
+
446
+ return validationErrorsArr;
447
+ };
448
+
449
+ // Cache management functions
450
+ const getCacheStats = () => {
451
+ const runtime = Date.now() - cacheStats.cacheStartTime;
452
+ const hitRate =
453
+ cacheStats.totalQueries > 0
454
+ ? (cacheStats.hits / cacheStats.totalQueries) * 100
455
+ : 0;
456
+
457
+ return {
458
+ totalEntries: schemaCache.size,
459
+ hits: cacheStats.hits,
460
+ misses: cacheStats.misses,
461
+ totalQueries: cacheStats.totalQueries,
462
+ hitRate: parseFloat(hitRate.toFixed(2)),
463
+ runtimeMs: runtime,
464
+ cacheKeys: Array.from(schemaCache.keys()),
465
+ memoryUsage: {
466
+ entriesCount: schemaCache.size,
467
+ // Rough estimate of memory usage per entry
468
+ estimatedSizeKB: Math.round(((schemaCache.size * 2) / 1024) * 100) / 100, // Rough estimate
469
+ },
470
+ };
471
+ };
472
+
473
+ const getDetailedCacheStats = () => {
474
+ const baseStats = getCacheStats();
475
+ const entries = Array.from(schemaCache.entries()).map(([key, value]) => ({
476
+ key,
477
+ createdAt: value.createdAt,
478
+ age: Date.now() - value.createdAt,
479
+ hasValidator: typeof value.validator === "function",
480
+ hasSubSchema: value.subSchema !== undefined,
481
+ }));
482
+
483
+ return {
484
+ ...baseStats,
485
+ entries: entries.sort((a, b) => b.createdAt - a.createdAt), // Most recent first
486
+ oldestEntry:
487
+ entries.length > 0 ? Math.max(...entries.map((e) => e.age)) : 0,
488
+ newestEntry:
489
+ entries.length > 0 ? Math.min(...entries.map((e) => e.age)) : 0,
490
+ };
491
+ };
492
+
493
+ const clearSchemaCache = (pattern) => {
494
+ if (pattern) {
495
+ // Clear entries matching pattern
496
+ const keysToDelete = [];
497
+ schemaCache.forEach((value, key) => {
498
+ if (key.includes(pattern)) {
499
+ keysToDelete.push(key);
500
+ }
501
+ });
502
+ keysToDelete.forEach((key) => schemaCache.delete(key));
503
+
504
+ // Also remove matching schemas from AJV
505
+ keysToDelete.forEach((key) => {
506
+ try {
507
+ ajv.removeSchema(key);
508
+ } catch (e) {
509
+ // Schema might not exist in AJV, ignore
510
+ }
511
+ });
512
+
513
+ return keysToDelete.length;
514
+ } else {
515
+ // Clear all cache
516
+ const entriesCleared = schemaCache.size;
517
+ schemaCache.clear();
518
+ // Reset stats
519
+ cacheStats.hits = 0;
520
+ cacheStats.misses = 0;
521
+ cacheStats.totalQueries = 0;
522
+ cacheStats.cacheStartTime = Date.now();
523
+
524
+ // Also clear AJV's internal cache to prevent duplicates
525
+ ajv.removeSchema();
526
+
527
+ return entriesCleared;
528
+ }
529
+ };
530
+
531
+ const warmupCache = (
532
+ paths,
533
+ schemaId = "https://trust.propdata.org.uk/schemas/v3/pdtf-transaction.json",
534
+ overlaysList = [],
535
+ ) => {
536
+ const warmupStats = {
537
+ totalAttempted: 0,
538
+ successful: 0,
539
+ failed: 0,
540
+ errors: [],
541
+ };
542
+
543
+ // Default common paths if none provided
544
+ const defaultPaths = [
545
+ "/propertyPack",
546
+ "/participants",
547
+ "/status",
548
+ "/propertyPack/surveys",
549
+ "/propertyPack/valuations",
550
+ "/propertyPack/waterAndDrainage",
551
+ "/propertyPack/ownership",
552
+ "/propertyPack/notices",
553
+ ];
554
+
555
+ // Default common overlays if none provided
556
+ const defaultOverlays = [
557
+ [],
558
+ ["baspiV5"],
559
+ ["ta6ed4"],
560
+ ["nts2023"],
561
+ ["baspiV5", "ta6ed4"],
562
+ ];
563
+
564
+ const pathsToWarm = paths || defaultPaths;
565
+ const overlaysToWarm =
566
+ overlaysList.length > 0 ? overlaysList : defaultOverlays;
567
+
568
+ pathsToWarm.forEach((path) => {
569
+ overlaysToWarm.forEach((overlays) => {
570
+ warmupStats.totalAttempted++;
571
+ try {
572
+ // This will populate the cache
573
+ getSubschema(path, schemaId, overlays);
574
+ getSubschemaValidator(path, schemaId, overlays);
575
+ warmupStats.successful++;
576
+ } catch (error) {
577
+ warmupStats.failed++;
578
+ warmupStats.errors.push({
579
+ path,
580
+ overlays,
581
+ error: error.message,
582
+ });
583
+ }
584
+ });
585
+ });
586
+
587
+ return warmupStats;
588
+ };
589
+
590
+ const pruneCacheByAge = (maxAgeMs) => {
591
+ const now = Date.now();
592
+ const keysToDelete = [];
593
+
594
+ schemaCache.forEach((value, key) => {
595
+ if (now - value.createdAt > maxAgeMs) {
596
+ keysToDelete.push(key);
597
+ }
598
+ });
599
+
600
+ keysToDelete.forEach((key) => {
601
+ schemaCache.delete(key);
602
+ try {
603
+ ajv.removeSchema(key);
604
+ } catch (e) {
605
+ // Schema might not exist in AJV, ignore
606
+ }
607
+ });
608
+
609
+ return keysToDelete.length;
610
+ };
611
+
612
+ const setCacheMaxSize = (maxSize) => {
613
+ if (schemaCache.size <= maxSize) return 0;
614
+
615
+ // Get entries sorted by creation time (oldest first)
616
+ const entries = Array.from(schemaCache.entries()).sort(
617
+ (a, b) => a[1].createdAt - b[1].createdAt,
618
+ );
619
+
620
+ const toRemove = schemaCache.size - maxSize;
621
+ const keysToDelete = entries.slice(0, toRemove).map(([key]) => key);
622
+
623
+ keysToDelete.forEach((key) => {
624
+ schemaCache.delete(key);
625
+ try {
626
+ ajv.removeSchema(key);
627
+ } catch (e) {
628
+ // Schema might not exist in AJV, ignore
629
+ }
630
+ });
631
+
632
+ return keysToDelete.length;
633
+ };
634
+
635
+ module.exports = {
636
+ ajv,
637
+ getTransactionSchema,
638
+ getValidator,
639
+ getSubschema,
640
+ isPathValid,
641
+ getSubschemaValidator,
642
+ getTitleAtPath,
643
+ verifiedClaimsSchema,
644
+ validateVerifiedClaims,
645
+ overlaysMap,
646
+ extensionOverlays,
647
+ // Enhanced cache management functions
648
+ getCacheStats,
649
+ getDetailedCacheStats,
650
+ clearSchemaCache,
651
+ warmupCache,
652
+ pruneCacheByAge,
653
+ setCacheMaxSize,
654
+ };