@pdtf/schemas 1.1.2 → 2.0.0-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 (30) hide show
  1. package/index.js +86 -19
  2. package/package.json +5 -3
  3. package/src/examples/v2/exampleTransaction.json +899 -0
  4. package/src/schemas/v1/freehold-information.json +2051 -0
  5. package/src/schemas/v1/material-facts.json +3 -3
  6. package/src/schemas/v2/baspiNotes.html +102 -0
  7. package/src/schemas/v2/combined.json +27796 -0
  8. package/src/schemas/v2/core.json +26224 -0
  9. package/src/schemas/v2/overlays/baspi.json +8857 -0
  10. package/src/schemas/v2/overlays/con29DW.json +656 -0
  11. package/src/schemas/v2/overlays/con29R.json +3115 -0
  12. package/src/schemas/v2/overlays/fme1.json +1610 -0
  13. package/src/schemas/v2/overlays/llc1.json +46 -0
  14. package/src/schemas/v2/overlays/lpe1.json +2424 -0
  15. package/src/schemas/v2/overlays/nts.json +162 -0
  16. package/src/schemas/v2/overlays/oc1.json +2799 -0
  17. package/src/schemas/v2/overlays/rds.json +3746 -0
  18. package/src/schemas/v2/overlays/ta10.json +4 -0
  19. package/src/schemas/v2/overlays/ta6.json +2311 -0
  20. package/src/schemas/v2/overlays/ta7.json +901 -0
  21. package/src/tests/{pdtfVerifiedClaims.test.js → v1/pdtfVerifiedClaims.test.js} +4 -4
  22. package/src/tests/{transactionSchema.test.js → v1/transactionSchema.test.js} +3 -3
  23. package/src/tests/{verifiedClaimsValidator.test.js → v1/verifiedClaimsValidator.test.js} +3 -3
  24. package/src/tests/v2/transactionSchema.test.js +255 -0
  25. package/src/utils/extractOverlay.js +100 -0
  26. /package/src/examples/{exampleAddParticipantVouch.json → v1/exampleAddParticipantVouch.json} +0 -0
  27. /package/src/examples/{exampleDocumentedVouch.json → v1/exampleDocumentedVouch.json} +0 -0
  28. /package/src/examples/{exampleElectronicRecord.json → v1/exampleElectronicRecord.json} +0 -0
  29. /package/src/examples/{exampleTransaction.json → v1/exampleTransaction.json} +0 -0
  30. /package/src/examples/{exampleVouch.json → v1/exampleVouch.json} +0 -0
package/index.js CHANGED
@@ -1,7 +1,17 @@
1
1
  const { dereference } = require("@jdw/jst");
2
+ const merge = require("deepmerge");
2
3
  const Ajv = require("ajv");
3
4
  const addFormats = require("ajv-formats");
5
+ const ajv = new Ajv({
6
+ allErrors: true,
7
+ // schema contains additional baspiRef and RDSRef metadata which is not strictly valid
8
+ strictSchema: false,
9
+ discriminator: true,
10
+ });
11
+ // Adds date formats among other types to the validator.
12
+ addFormats(ajv);
4
13
 
14
+ // v1, deprecated direct access
5
15
  const pdtfTransaction = require("./src/schemas/v1/pdtf-transaction.json");
6
16
  const materialFacts = require("./src/schemas/v1/material-facts.json");
7
17
  const legalInformation = require("./src/schemas/v1/legal-information.json");
@@ -30,23 +40,74 @@ const subSchemas = {
30
40
  "https://trust.propdata.org.uk/schemas/v1/searches/drainage-and-water.json":
31
41
  drainageAndWater,
32
42
  };
33
-
34
43
  const transactionSchema = dereference(pdtfTransaction, (id) => subSchemas[id]);
44
+ const validator = ajv.compile(transactionSchema);
35
45
 
36
- const ajv = new Ajv({
37
- allErrors: true,
38
- // schema contains additional baspiRef and RDSRef metadata which is not strictly valid
39
- strictSchema: false,
40
- discriminator: true,
41
- });
42
- // Adds date formats among other types to the validator.
43
- addFormats(ajv);
46
+ // v2, via accessor functions and overlays
47
+ const combinedSchema = require("./src/schemas/v2/combined.json");
48
+ const coreSchema = require("./src/schemas/v2/core.json");
49
+
50
+ const baspiOverlay = require("./src/schemas/v2/overlays/baspi.json");
51
+ // const ta6Overlay = require("./src/schemas/v2/overlays/ta6.json");
52
+ // const ta7Overlay = require("./src/schemas/v2/overlays/ta7.json");
53
+ // const lpe1Overlay = require("./src/schemas/v2/overlays/lpe1.json");
54
+ // const fme1Overlay = require("./src/schemas/v2/overlays/fme1.json");
55
+ // const llc1Overlay = require("./src/schemas/v2/overlays/llc1.json");
56
+ // const con29ROverlay = require("./src/schemas/v2/overlays/con29R.json");
57
+ // const con29DWOverlay = require("./src/schemas/v2/overlays/con29DW.json");
58
+ // const rdsOverlay = require("./src/schemas/v2/overlays/rds.json");
59
+ // const oc1Overlay = require("./src/schemas/v2/overlays/oc1.json");
60
+
61
+ const overlays = { baspiV4: baspiOverlay };
62
+
63
+ const transactionSchemas = {
64
+ "https://trust.propdata.org.uk/schemas/v1/pdtf-transaction.json":
65
+ transactionSchema,
66
+ "https://trust.propdata.org.uk/schemas/v2/pdtf-transaction.json": coreSchema,
67
+ };
44
68
 
45
- const validator = ajv.compile(transactionSchema);
69
+ const combineMerge = (target, source, options) => {
70
+ const destination = target.slice();
71
+ source.forEach((item, index) => {
72
+ if (typeof destination[index] === "undefined") {
73
+ destination[index] = options.cloneUnlessOtherwiseSpecified(item, options);
74
+ } else if (options.isMergeableObject(item)) {
75
+ destination[index] = merge(target[index], item, options);
76
+ } else if (target.indexOf(item) === -1) {
77
+ destination.push(item);
78
+ }
79
+ });
80
+ return destination;
81
+ };
82
+
83
+ const getTransactionSchema = (
84
+ schemaId = "https://trust.propdata.org.uk/schemas/v2/pdtf-transaction.json",
85
+ overlay = "baspiV4"
86
+ ) => {
87
+ const sourceSchema = transactionSchemas[schemaId];
88
+ const overlaySchema = overlays[overlay];
89
+ if (!overlaySchema) return sourceSchema;
90
+ const mergedSchema = merge(overlaySchema, sourceSchema, {
91
+ arrayMerge: combineMerge,
92
+ });
93
+ // console.log("mergedSchema", mergedSchema);
94
+ return mergedSchema;
95
+ };
96
+
97
+ const getValidator = (schemaId, overlay = "baspiV4") => {
98
+ let validator = ajv.getSchema(schemaId);
99
+ if (!validator) {
100
+ const schema = getTransactionSchema(schemaId, overlay);
101
+ ajv.addSchema(schema, schemaId);
102
+ validator = ajv.getSchema(schemaId);
103
+ }
104
+ return validator;
105
+ };
46
106
 
47
- const getSubschema = (path) => {
107
+ // common functions for v1 and v2
108
+ const getSubschema = (path, sourceSchema = transactionSchema) => {
48
109
  const pathArray = path.split("/").slice(1);
49
- if (pathArray.length < 1) return transactionSchema;
110
+ if (pathArray.length < 1) return sourceSchema;
50
111
  return pathArray.reduce((schema, pathElement) => {
51
112
  const { type, items, properties, oneOf } = schema;
52
113
  if (type === "array") return items;
@@ -63,20 +124,22 @@ const getSubschema = (path) => {
63
124
  if (matchingProperty) return matchingProperty;
64
125
  }
65
126
  return undefined;
66
- }, transactionSchema);
127
+ }, sourceSchema);
67
128
  };
68
129
 
69
- const isPathValid = (path) => {
130
+ const isPathValid = (path, schema = transactionSchema) => {
70
131
  try {
71
- return getSubschema(path) !== undefined;
132
+ return getSubschema(path, schema) !== undefined;
72
133
  } catch (err) {
73
134
  return false;
74
135
  }
75
136
  };
76
137
 
77
- const getSubschemaValidator = (path) => {
78
- const subSchema = getSubschema(path);
138
+ const getSubschemaValidator = (path, schema = transactionSchema) => {
139
+ const subSchema = getSubschema(path, schema);
140
+ // see if we can retrieve the schema by path
79
141
  let validator = ajv.getSchema(path);
142
+ // retrieve whole schema by $id if available
80
143
  if (!validator && subSchema.$id) validator = ajv.getSchema(subSchema.$id);
81
144
  if (!validator) {
82
145
  ajv.addSchema(subSchema, path);
@@ -85,6 +148,7 @@ const getSubschemaValidator = (path) => {
85
148
  return validator;
86
149
  };
87
150
 
151
+ // v1, deprecated
88
152
  const getTitleAtPath = (schema, path, rootPath = path) => {
89
153
  if (path === "") path = "/";
90
154
  let pathArray = path.split("/").slice(1);
@@ -167,8 +231,11 @@ const validateVerifiedClaims = (verifiedClaims) => {
167
231
  };
168
232
 
169
233
  module.exports = {
170
- transactionSchema,
171
- validator,
234
+ ajv,
235
+ transactionSchema, // v1 deprecated
236
+ validator, // v1 deprecated
237
+ getTransactionSchema,
238
+ getValidator,
172
239
  getSubschema,
173
240
  isPathValid,
174
241
  getSubschemaValidator,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pdtf/schemas",
3
- "version": "1.1.2",
3
+ "version": "2.0.0-0",
4
4
  "description": "Property Data Trust Framework Schemas and Utilities",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -26,9 +26,11 @@
26
26
  "homepage": "https://github.com/Property-Data-Trust-Framework/schemas#readme",
27
27
  "dependencies": {
28
28
  "@jdw/jst": "^2.0.0-beta.15",
29
- "ajv": "^8.10.0",
29
+ "ajv": "^8.12.0",
30
30
  "ajv-formats": "^2.1.1",
31
- "jsonpointer": "^5.0.0"
31
+ "deepmerge": "^4.3.1",
32
+ "jsonpointer": "^5.0.0",
33
+ "traverse": "^0.6.7"
32
34
  },
33
35
  "devDependencies": {
34
36
  "jest": "^27.5.1"