@pdtf/schemas 2.0.0-22 → 2.0.0-23
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.
- package/index.js +26 -20
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -60,7 +60,7 @@ const con29DWOverlay = require("./src/schemas/v2/overlays/con29DW.json");
|
|
|
60
60
|
const rdsOverlay = require("./src/schemas/v2/overlays/rds.json");
|
|
61
61
|
const oc1Overlay = require("./src/schemas/v2/overlays/oc1.json");
|
|
62
62
|
|
|
63
|
-
const
|
|
63
|
+
const overlaysMap = { baspiV4: baspiOverlay, ta6ed4: ta6Overlay, null: {} };
|
|
64
64
|
|
|
65
65
|
const transactionSchemas = {
|
|
66
66
|
"https://trust.propdata.org.uk/schemas/v1/pdtf-transaction.json":
|
|
@@ -85,22 +85,25 @@ const combineMerge = (target, source, options) => {
|
|
|
85
85
|
|
|
86
86
|
const getTransactionSchema = (
|
|
87
87
|
schemaId = "https://trust.propdata.org.uk/schemas/v2/pdtf-transaction.json",
|
|
88
|
-
|
|
88
|
+
overlays = ["baspiV4"]
|
|
89
89
|
) => {
|
|
90
90
|
const sourceSchema = transactionSchemas[schemaId];
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
91
|
+
if (!overlays || overlays.length < 1) return sourceSchema;
|
|
92
|
+
let mergedSchema = sourceSchema;
|
|
93
|
+
overlays.forEach((overlay) => {
|
|
94
|
+
const overlaySchema = overlaysMap[overlay] || {};
|
|
95
|
+
mergedSchema = merge(overlaySchema, mergedSchema, {
|
|
96
|
+
arrayMerge: combineMerge,
|
|
97
|
+
});
|
|
95
98
|
});
|
|
96
99
|
// console.log("mergedSchema", mergedSchema);
|
|
97
100
|
return mergedSchema;
|
|
98
101
|
};
|
|
99
102
|
|
|
100
|
-
const getValidator = (schemaId,
|
|
103
|
+
const getValidator = (schemaId, overlays) => {
|
|
101
104
|
let validator = ajv.getSchema(schemaId);
|
|
102
105
|
if (!validator) {
|
|
103
|
-
const schema = getTransactionSchema(schemaId,
|
|
106
|
+
const schema = getTransactionSchema(schemaId, overlays);
|
|
104
107
|
ajv.addSchema(schema, schemaId);
|
|
105
108
|
validator = ajv.getSchema(schemaId);
|
|
106
109
|
}
|
|
@@ -108,8 +111,8 @@ const getValidator = (schemaId, overlay = "baspiV4") => {
|
|
|
108
111
|
};
|
|
109
112
|
|
|
110
113
|
// common functions for v1 and v2
|
|
111
|
-
const getSubschema = (path, schemaId,
|
|
112
|
-
const sourceSchema = getTransactionSchema(schemaId,
|
|
114
|
+
const getSubschema = (path, schemaId, overlays) => {
|
|
115
|
+
const sourceSchema = getTransactionSchema(schemaId, overlays);
|
|
113
116
|
const pathArray = path.split("/").slice(1);
|
|
114
117
|
if (pathArray.length < 1) return sourceSchema;
|
|
115
118
|
return pathArray.reduce((schema, pathElement) => {
|
|
@@ -131,19 +134,21 @@ const getSubschema = (path, schemaId, overlay) => {
|
|
|
131
134
|
}, sourceSchema);
|
|
132
135
|
};
|
|
133
136
|
|
|
134
|
-
const isPathValid = (path, schemaId,
|
|
135
|
-
const schema = getTransactionSchema(schemaId,
|
|
137
|
+
const isPathValid = (path, schemaId, overlays) => {
|
|
138
|
+
const schema = getTransactionSchema(schemaId, overlays);
|
|
136
139
|
try {
|
|
137
|
-
return getSubschema(path, schemaId,
|
|
140
|
+
return getSubschema(path, schemaId, overlays) !== undefined;
|
|
138
141
|
} catch (err) {
|
|
139
142
|
return false;
|
|
140
143
|
}
|
|
141
144
|
};
|
|
142
145
|
|
|
143
|
-
const getSubschemaValidator = (path, schemaId,
|
|
144
|
-
const subSchema = getSubschema(path, schemaId,
|
|
145
|
-
|
|
146
|
-
|
|
146
|
+
const getSubschemaValidator = (path, schemaId, overlays = ["baspiV4"]) => {
|
|
147
|
+
const subSchema = getSubschema(path, schemaId, overlays);
|
|
148
|
+
const overlayKey = (overlays || []).join(".");
|
|
149
|
+
// see if we can retrieve the schema by path, schemaId and overlays
|
|
150
|
+
const cacheKey = `${path}-${schemaId}-${overlayKey}`;
|
|
151
|
+
console.log("cacheKey", cacheKey);
|
|
147
152
|
let validator = ajv.getSchema(cacheKey);
|
|
148
153
|
// retrieve whole schema by $id if available
|
|
149
154
|
if (!validator && subSchema.$id) validator = ajv.getSchema(cacheKey);
|
|
@@ -198,7 +203,7 @@ const getTitleAtPath = (schema, path, rootPath = path) => {
|
|
|
198
203
|
}
|
|
199
204
|
};
|
|
200
205
|
|
|
201
|
-
const validateVerifiedClaims = (verifiedClaims, schemaId,
|
|
206
|
+
const validateVerifiedClaims = (verifiedClaims, schemaId, overlays) => {
|
|
202
207
|
const validatorVClaims = ajv.compile(verifiedClaimsSchema);
|
|
203
208
|
|
|
204
209
|
const validationErrorsArr = [];
|
|
@@ -217,9 +222,10 @@ const validateVerifiedClaims = (verifiedClaims, schemaId, overlay) => {
|
|
|
217
222
|
verifiedClaimsArray.forEach((claim) => {
|
|
218
223
|
const paths = Object.keys(claim.claims);
|
|
219
224
|
for (const path of paths) {
|
|
220
|
-
const validPath = isPathValid(path, schemaId,
|
|
225
|
+
const validPath = isPathValid(path, schemaId, overlays);
|
|
221
226
|
if (validPath) {
|
|
222
|
-
|
|
227
|
+
console.log("overlays", path, overlays);
|
|
228
|
+
const subValidator = getSubschemaValidator(path, schemaId, overlays);
|
|
223
229
|
const isValid = subValidator(claim.claims[path]);
|
|
224
230
|
if (!isValid) {
|
|
225
231
|
validationErrorsArr.push(...subValidator.errors);
|