@hestia-earth/schema-validation 7.6.0 → 7.6.1
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/package.json +1 -1
- package/validate-jsonld.js +29 -21
- package/validate.d.ts +13 -6
- package/validate.js +61 -4
package/package.json
CHANGED
package/validate-jsonld.js
CHANGED
|
@@ -75,28 +75,36 @@ exports.run = function () { return __awaiter(void 0, void 0, void 0, function ()
|
|
|
75
75
|
var contentValidator, jsonldFiles, results;
|
|
76
76
|
return __generator(this, function (_a) {
|
|
77
77
|
switch (_a.label) {
|
|
78
|
-
case 0:
|
|
79
|
-
|
|
80
|
-
contentValidator = _a.sent();
|
|
78
|
+
case 0:
|
|
79
|
+
contentValidator = validate_1.validator(domain, strictMode === 'true');
|
|
81
80
|
jsonldFiles = findFiles(folder);
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
81
|
+
return [4 /*yield*/, Promise.all(jsonldFiles.map(function (filepath) { return __awaiter(void 0, void 0, void 0, function () {
|
|
82
|
+
var content, nodes, allErrors;
|
|
83
|
+
return __generator(this, function (_a) {
|
|
84
|
+
switch (_a.label) {
|
|
85
|
+
case 0:
|
|
86
|
+
content = loadFile(filepath);
|
|
87
|
+
nodes = Array.isArray(content) ? content : ('nodes' in content ? content.nodes : [content]);
|
|
88
|
+
return [4 /*yield*/, Promise.all(nodes
|
|
89
|
+
.map(function (node) { return node['@type'] || node.type ? node : null; })
|
|
90
|
+
.filter(Boolean)
|
|
91
|
+
.map(contentValidator))];
|
|
92
|
+
case 1:
|
|
93
|
+
allErrors = (_a.sent())
|
|
94
|
+
.map(function (_a) {
|
|
95
|
+
var errors = _a.errors;
|
|
96
|
+
return errors;
|
|
97
|
+
});
|
|
98
|
+
return [2 /*return*/, {
|
|
99
|
+
filepath: filepath,
|
|
100
|
+
success: !allErrors.some(function (v) { return v.length; }),
|
|
101
|
+
errors: allErrors
|
|
102
|
+
}];
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}); }))];
|
|
106
|
+
case 1:
|
|
107
|
+
results = (_a.sent())
|
|
100
108
|
.filter(function (_a) {
|
|
101
109
|
var success = _a.success;
|
|
102
110
|
return !success;
|
package/validate.d.ts
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
import * as Ajv from 'ajv';
|
|
2
2
|
import { SchemaType, JSON as HestiaJson } from '@hestia-earth/schema';
|
|
3
3
|
import { definitions } from '@hestia-earth/json-schema';
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Validate a single node. Function is asynchronous to return a list of `success` and a list of `errors`.
|
|
6
|
+
*
|
|
7
|
+
* @param ajv The AJV object. Use `initAjv()` or pass your own.
|
|
8
|
+
* @param schemas The schema definitions. Use `loadSchemas` from `@hestia-earth/json-schema` or pass your own.
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
export declare const validateContent: (ajv: Ajv.Ajv, schemas: definitions) => (content: HestiaJson<SchemaType>) => Promise<{
|
|
12
|
+
success: any;
|
|
6
13
|
errors: Ajv.ErrorObject[];
|
|
7
|
-
}
|
|
14
|
+
}>;
|
|
8
15
|
declare function uniqueArrayItemValidate(keys: string[], items: any[], schema?: any, dataPath?: string): boolean;
|
|
9
16
|
export { uniqueArrayItemValidate };
|
|
10
17
|
export declare const initAjv: () => Ajv.Ajv;
|
|
@@ -15,7 +22,7 @@ export declare const initAjv: () => Ajv.Ajv;
|
|
|
15
22
|
* @param strictMode Allow validating non-existing nodes, i.e. without unique `@id`.
|
|
16
23
|
* @returns Function to validate a Node. Use `await validator()(node)`
|
|
17
24
|
*/
|
|
18
|
-
export declare const validator: (domain?: string, strictMode?: boolean) => (content: HestiaJson<SchemaType>) => {
|
|
19
|
-
success:
|
|
25
|
+
export declare const validator: (domain?: string, strictMode?: boolean) => (content: HestiaJson<SchemaType>) => Promise<{
|
|
26
|
+
success: any;
|
|
20
27
|
errors: Ajv.ErrorObject[];
|
|
21
|
-
}
|
|
28
|
+
}>;
|
package/validate.js
CHANGED
|
@@ -10,6 +10,42 @@ var __assign = (this && this.__assign) || function () {
|
|
|
10
10
|
};
|
|
11
11
|
return __assign.apply(this, arguments);
|
|
12
12
|
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (_) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
13
49
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
50
|
exports.validator = exports.initAjv = exports.uniqueArrayItemValidate = exports.validateContent = void 0;
|
|
15
51
|
var Ajv = require("ajv");
|
|
@@ -24,9 +60,30 @@ var validateSchemaType = function (schemas, content) {
|
|
|
24
60
|
}
|
|
25
61
|
return true;
|
|
26
62
|
};
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
63
|
+
/**
|
|
64
|
+
* Validate a single node. Function is asynchronous to return a list of `success` and a list of `errors`.
|
|
65
|
+
*
|
|
66
|
+
* @param ajv The AJV object. Use `initAjv()` or pass your own.
|
|
67
|
+
* @param schemas The schema definitions. Use `loadSchemas` from `@hestia-earth/json-schema` or pass your own.
|
|
68
|
+
* @returns
|
|
69
|
+
*/
|
|
70
|
+
exports.validateContent = function (ajv, schemas) { return function (content) { return __awaiter(void 0, void 0, void 0, function () {
|
|
71
|
+
var _a, _b;
|
|
72
|
+
return __generator(this, function (_c) {
|
|
73
|
+
switch (_c.label) {
|
|
74
|
+
case 0:
|
|
75
|
+
_a = validateSchemaType(schemas, content);
|
|
76
|
+
if (!_a) return [3 /*break*/, 2];
|
|
77
|
+
_b = {};
|
|
78
|
+
return [4 /*yield*/, ajv.validate(schemas[content['@type'] || content.type], content)];
|
|
79
|
+
case 1:
|
|
80
|
+
_a = (_b.success = _c.sent(),
|
|
81
|
+
_b.errors = ajv.errors || [],
|
|
82
|
+
_b);
|
|
83
|
+
_c.label = 2;
|
|
84
|
+
case 2: return [2 /*return*/, _a];
|
|
85
|
+
}
|
|
86
|
+
});
|
|
30
87
|
}); }; };
|
|
31
88
|
var getByKey = function (prev, curr) { return !curr || !prev ? prev : (Array.isArray(prev) ? prev.map(function (item) { return getItemValue(item, curr); }) : (prev ? prev[curr] : undefined)); };
|
|
32
89
|
var getItemValue = function (item, key) {
|
|
@@ -86,7 +143,7 @@ exports.validator = function (domain, strictMode) {
|
|
|
86
143
|
type: 'string'
|
|
87
144
|
} }, schema.properties);
|
|
88
145
|
schema.oneOf = strictMode && schema_1.isTypeNode(schemaName) ? [existingNodeRequired] : schema.oneOf;
|
|
89
|
-
ajv.addSchema(schema, domain + "/schema/json-schema
|
|
146
|
+
ajv.addSchema(schema, domain + "/schema/json-schema/" + schemaName + "#");
|
|
90
147
|
});
|
|
91
148
|
return exports.validateContent(ajv, schemas);
|
|
92
149
|
};
|