@hestia-earth/api 0.20.3 → 0.20.5
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.
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { NodeType, ICycleJSONLD, JSONLD, JSON, SchemaType, ITermJSONLD, Term } from '@hestia-earth/schema';
|
|
2
|
-
export type Node = (JSONLD<NodeType> |
|
|
3
|
-
site?:
|
|
4
|
-
cycle?:
|
|
1
|
+
import { NodeType, ICycleJSONLD, JSONLD, JSON as HestiaJson, SchemaType, ITermJSONLD, Term } from '@hestia-earth/schema';
|
|
2
|
+
export type Node = (JSONLD<NodeType> | HestiaJson<SchemaType>) & {
|
|
3
|
+
site?: HestiaJson<SchemaType.Site> | JSONLD<NodeType.Site>;
|
|
4
|
+
cycle?: HestiaJson<SchemaType.Cycle> | JSONLD<NodeType.Cycle>;
|
|
5
5
|
};
|
|
6
6
|
export declare const notPrivateError = "expected-not-private";
|
|
7
7
|
export declare const EXTRANEOUS = "extraneous";
|
|
8
|
-
export declare const validateExpectedNodes: (nodes: Node[]) =>
|
|
9
|
-
export declare const validateImpactAssessmentLinks: (nodes: Node[]) =>
|
|
10
|
-
export declare const validatePrivate: (cycle: ICycleJSONLD) =>
|
|
11
|
-
export declare const validateProductCorrespondence: (expectedProducts: (ITermJSONLD | Term)[], recalculatedProducts: (ITermJSONLD | Term)[], idRecalculated: string) =>
|
|
8
|
+
export declare const validateExpectedNodes: (nodes: Node[]) => string[];
|
|
9
|
+
export declare const validateImpactAssessmentLinks: (nodes: Node[]) => string[];
|
|
10
|
+
export declare const validatePrivate: (cycle: ICycleJSONLD) => string[];
|
|
11
|
+
export declare const validateProductCorrespondence: (expectedProducts: (ITermJSONLD | Term)[], recalculatedProducts: (ITermJSONLD | Term)[], idRecalculated: string) => string[];
|
|
12
12
|
export declare const getMainErrorMessage: (errors: any) => "expected-not-private" | "recalculation-error" | "invalid-nodes";
|
|
@@ -15,17 +15,19 @@ exports.getMainErrorMessage = exports.validateProductCorrespondence = exports.va
|
|
|
15
15
|
var schema_1 = require("@hestia-earth/schema");
|
|
16
16
|
exports.notPrivateError = 'expected-not-private';
|
|
17
17
|
exports.EXTRANEOUS = 'extraneous';
|
|
18
|
+
var isExtraneousError = function (error) { var _a; return (_a = (typeof error === 'string' ? error : error === null || error === void 0 ? void 0 : error.message)) === null || _a === void 0 ? void 0 : _a.includes(exports.EXTRANEOUS); };
|
|
19
|
+
var isNotPrivateError = function (error) { return (typeof error === 'string' ? error : error === null || error === void 0 ? void 0 : error.message) === exports.notPrivateError; };
|
|
18
20
|
var getType = function (node) { return (node === null || node === void 0 ? void 0 : node['@type']) || (node === null || node === void 0 ? void 0 : node.type); };
|
|
19
21
|
var getId = function (node) { return (node === null || node === void 0 ? void 0 : node['@id']) || (node === null || node === void 0 ? void 0 : node.id); };
|
|
20
22
|
var getErrorsFromValidationsObj = function (validations) {
|
|
21
23
|
return Object.entries(validations)
|
|
22
24
|
.filter(function (_a) {
|
|
23
25
|
var _errorMsg = _a[0], validationFailed = _a[1];
|
|
24
|
-
return validationFailed;
|
|
26
|
+
return !!validationFailed;
|
|
25
27
|
})
|
|
26
28
|
.map(function (_a) {
|
|
27
29
|
var errorMsg = _a[0];
|
|
28
|
-
return
|
|
30
|
+
return errorMsg;
|
|
29
31
|
});
|
|
30
32
|
};
|
|
31
33
|
var validateExpectedNodes = function (nodes) {
|
|
@@ -37,13 +39,11 @@ var validateExpectedNodes = function (nodes) {
|
|
|
37
39
|
var cycleId = getId(cycles === null || cycles === void 0 ? void 0 : cycles[0]);
|
|
38
40
|
var cycleSiteId = getId((_b = cycles === null || cycles === void 0 ? void 0 : cycles[0]) === null || _b === void 0 ? void 0 : _b.site);
|
|
39
41
|
var siteId = getId(sites === null || sites === void 0 ? void 0 : sites[0]);
|
|
40
|
-
|
|
42
|
+
return getErrorsFromValidationsObj(__assign((_a = {}, _a['More than one Cycle found in expected nodes'] = cycles.length > 1, _a['No Cycle found in expected nodes'] = cycles.length < 1, _a['Cycle missing name field in expected nodes'] = cycles.length && !cycles[0].name, _a['More than one Site found in expected nodes'] = sites.length > 1, _a["Cycle ".concat(cycleId, " is not linked to Site ").concat(siteId, " in expected nodes")] = cycleSiteId !== siteId, _a), impactAssessments.reduce(function (acc, ia) {
|
|
41
43
|
acc["ImpactAssessment ".concat(getId(ia), " is not linked to Cycle ").concat(cycleId, " in expected nodes")] =
|
|
42
44
|
getId(ia === null || ia === void 0 ? void 0 : ia.cycle) !== cycleId;
|
|
43
45
|
return acc;
|
|
44
|
-
}, {}));
|
|
45
|
-
var validationErrors = getErrorsFromValidationsObj(validations);
|
|
46
|
-
return validationErrors;
|
|
46
|
+
}, {})));
|
|
47
47
|
};
|
|
48
48
|
exports.validateExpectedNodes = validateExpectedNodes;
|
|
49
49
|
var matchedProduct = function (impacts) { return function (product) {
|
|
@@ -55,13 +55,11 @@ var validateImpactAssessmentLinks = function (nodes) {
|
|
|
55
55
|
var impactAssessments = nodes.filter(function (node) { return getType(node) === schema_1.NodeType.ImpactAssessment; });
|
|
56
56
|
var cycle = nodes.find(function (node) { return getType(node) === schema_1.NodeType.Cycle; });
|
|
57
57
|
var cycleId = getId(cycle);
|
|
58
|
-
|
|
58
|
+
return getErrorsFromValidationsObj((_a = {},
|
|
59
59
|
_a["Cycle ".concat(cycleId, " has no associated impact assessment")] = !impactAssessments.length,
|
|
60
60
|
_a["Cycle ".concat(cycleId, " has no products")] = !((_b = cycle.products) === null || _b === void 0 ? void 0 : _b.length),
|
|
61
61
|
_a["Cycle ".concat(cycleId, " has a product with more than one linked impact assessment")] = (_c = cycle.products) === null || _c === void 0 ? void 0 : _c.some(matchedProduct(impactAssessments)),
|
|
62
|
-
_a);
|
|
63
|
-
var validationErrors = getErrorsFromValidationsObj(validations);
|
|
64
|
-
return validationErrors;
|
|
62
|
+
_a));
|
|
65
63
|
};
|
|
66
64
|
exports.validateImpactAssessmentLinks = validateImpactAssessmentLinks;
|
|
67
65
|
var validatePrivate = function (cycle) {
|
|
@@ -79,18 +77,16 @@ var validateProductCorrespondence = function (expectedProducts, recalculatedProd
|
|
|
79
77
|
.map(function (_a) {
|
|
80
78
|
var products = _a[0], errorType = _a[1];
|
|
81
79
|
return products.length
|
|
82
|
-
?
|
|
83
|
-
.map(function (p) { return p['@id']; })
|
|
84
|
-
.join(', ')))
|
|
80
|
+
? "Cycle ".concat(idRecalculated, " has ").concat(errorType, " impact assessment for products: ").concat(products.map(function (p) { return p['@id']; }).join(', '))
|
|
85
81
|
: null;
|
|
86
82
|
})
|
|
87
83
|
.filter(Boolean);
|
|
88
84
|
};
|
|
89
85
|
exports.validateProductCorrespondence = validateProductCorrespondence;
|
|
90
86
|
var getMainErrorMessage = function (errors) {
|
|
91
|
-
return errors
|
|
87
|
+
return (errors === null || errors === void 0 ? void 0 : errors.length) === 1 && isExtraneousError(errors[0])
|
|
92
88
|
? 'recalculation-error'
|
|
93
|
-
: errors.some(
|
|
89
|
+
: errors.some(isNotPrivateError)
|
|
94
90
|
? exports.notPrivateError
|
|
95
91
|
: 'invalid-nodes';
|
|
96
92
|
};
|