@hestia-earth/schema-validation 15.0.0 → 15.2.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.
- package/bin/bin-validate-jsonld.js +1 -1
- package/index.js +6 -2
- package/package.json +3 -3
- package/validate-jsonld.js +10 -9
- package/validate.js +17 -13
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"use strict";
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
var validate_jsonld_1 = require("../validate-jsonld");
|
|
5
|
-
validate_jsonld_1.run().then(function () { return process.exit(0); }).catch(function (err) {
|
|
5
|
+
(0, validate_jsonld_1.run)().then(function () { return process.exit(0); }).catch(function (err) {
|
|
6
6
|
console.error(err);
|
|
7
7
|
process.exit(1);
|
|
8
8
|
});
|
package/index.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
8
12
|
}));
|
|
9
13
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
-
for (var p in m) if (p !== "default" && !
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
15
|
};
|
|
12
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
17
|
__exportStar(require("./validate"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hestia-earth/schema-validation",
|
|
3
|
-
"version": "15.
|
|
3
|
+
"version": "15.2.0",
|
|
4
4
|
"description": "Hestia Schema Validation",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
@@ -17,8 +17,8 @@
|
|
|
17
17
|
"json-schema",
|
|
18
18
|
"ajv"
|
|
19
19
|
],
|
|
20
|
-
"author": "Guillaume Royer <
|
|
21
|
-
"license": "
|
|
20
|
+
"author": "Guillaume Royer <guillaume@hestia.earth>",
|
|
21
|
+
"license": "MIT",
|
|
22
22
|
"bugs": {
|
|
23
23
|
"url": "https://gitlab.com/hestia-earth/hestia-schema/issues"
|
|
24
24
|
},
|
package/validate-jsonld.js
CHANGED
|
@@ -14,7 +14,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
14
14
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
15
|
function step(op) {
|
|
16
16
|
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (_) try {
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
18
|
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;
|
|
19
19
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
20
|
switch (op[0]) {
|
|
@@ -68,27 +68,27 @@ var fs_1 = require("fs");
|
|
|
68
68
|
var path_1 = require("path");
|
|
69
69
|
var validate_1 = require("./validate");
|
|
70
70
|
var _a = __read(process.argv.slice(2), 3), DOMAIN = _a[0], folder = _a[1], strictMode = _a[2];
|
|
71
|
-
var domain = "https://"
|
|
72
|
-
var loadFile = function (filename) { return JSON.parse(fs_1.readFileSync(filename, 'utf8')); };
|
|
71
|
+
var domain = "https://".concat(DOMAIN || process.env.DOMAIN || 'www.hestia.earth');
|
|
72
|
+
var loadFile = function (filename) { return JSON.parse((0, fs_1.readFileSync)(filename, 'utf8')); };
|
|
73
73
|
var extensions = [
|
|
74
74
|
'jsonld',
|
|
75
75
|
'json',
|
|
76
76
|
'hestia'
|
|
77
77
|
];
|
|
78
|
-
var findFiles = function (directory) { return fs_1.readdirSync(directory)
|
|
78
|
+
var findFiles = function (directory) { return (0, fs_1.readdirSync)(directory)
|
|
79
79
|
.map(function (path) {
|
|
80
|
-
var isDirectory = fs_1.lstatSync(path_1.join(directory, path)).isDirectory();
|
|
81
|
-
return isDirectory ? findFiles(path_1.join(directory, path)) : (extensions.includes(path.split('.')[1]) ? path_1.join(directory, path) : null);
|
|
80
|
+
var isDirectory = (0, fs_1.lstatSync)((0, path_1.join)(directory, path)).isDirectory();
|
|
81
|
+
return isDirectory ? findFiles((0, path_1.join)(directory, path)) : (extensions.includes(path.split('.')[1]) ? (0, path_1.join)(directory, path) : null);
|
|
82
82
|
})
|
|
83
83
|
.flat()
|
|
84
84
|
.filter(Boolean); };
|
|
85
|
-
|
|
85
|
+
var run = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
86
86
|
var contentValidator, jsonldFiles, results, jsonldFiles_1, jsonldFiles_1_1, filepath, content, nodes, allErrors, nodes_1, nodes_1_1, node, errors, e_1_1, success, e_2_1;
|
|
87
87
|
var e_2, _a, e_1, _b;
|
|
88
88
|
return __generator(this, function (_c) {
|
|
89
89
|
switch (_c.label) {
|
|
90
90
|
case 0:
|
|
91
|
-
contentValidator = validate_1.validator(domain, strictMode === 'true');
|
|
91
|
+
contentValidator = (0, validate_1.validator)(domain, strictMode === 'true');
|
|
92
92
|
jsonldFiles = findFiles(folder);
|
|
93
93
|
results = [];
|
|
94
94
|
_c.label = 1;
|
|
@@ -158,9 +158,10 @@ exports.run = function () { return __awaiter(void 0, void 0, void 0, function ()
|
|
|
158
158
|
return [7 /*endfinally*/];
|
|
159
159
|
case 15:
|
|
160
160
|
if (results.length) {
|
|
161
|
-
throw new Error("Validation errors: "
|
|
161
|
+
throw new Error("Validation errors: ".concat(JSON.stringify(results, null, 2)));
|
|
162
162
|
}
|
|
163
163
|
return [2 /*return*/];
|
|
164
164
|
}
|
|
165
165
|
});
|
|
166
166
|
}); };
|
|
167
|
+
exports.run = run;
|
package/validate.js
CHANGED
|
@@ -25,7 +25,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
25
25
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
26
|
function step(op) {
|
|
27
27
|
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (_) try {
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
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
30
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
31
|
switch (op[0]) {
|
|
@@ -56,7 +56,7 @@ var existingNodeRequired = Object.freeze({ required: ['@id', '@type'] });
|
|
|
56
56
|
var validateSchemaType = function (schemas, content) {
|
|
57
57
|
var type = content['@type'] || content.type;
|
|
58
58
|
if (!(type in schemas)) {
|
|
59
|
-
throw new Error("Unknown or invalid type \""
|
|
59
|
+
throw new Error("Unknown or invalid type \"".concat(type, "\""));
|
|
60
60
|
}
|
|
61
61
|
return true;
|
|
62
62
|
};
|
|
@@ -67,8 +67,9 @@ var validateSchemaType = function (schemas, content) {
|
|
|
67
67
|
* @param schemas The schema definitions. Use `loadSchemas` from `@hestia-earth/json-schema` or pass your own.
|
|
68
68
|
* @returns
|
|
69
69
|
*/
|
|
70
|
-
|
|
71
|
-
var _a
|
|
70
|
+
var validateContent = function (ajv, schemas) { return function (content) { return __awaiter(void 0, void 0, void 0, function () {
|
|
71
|
+
var _a;
|
|
72
|
+
var _b;
|
|
72
73
|
return __generator(this, function (_c) {
|
|
73
74
|
switch (_c.label) {
|
|
74
75
|
case 0:
|
|
@@ -85,6 +86,7 @@ exports.validateContent = function (ajv, schemas) { return function (content) {
|
|
|
85
86
|
}
|
|
86
87
|
});
|
|
87
88
|
}); }; };
|
|
89
|
+
exports.validateContent = validateContent;
|
|
88
90
|
var getByKey = function (prev, curr) { return !curr || !prev ? prev : (Array.isArray(prev) ? prev.map(function (item) { return getItemValue(item, curr); }) : (prev ? prev[curr] : undefined)); };
|
|
89
91
|
var getItemValue = function (item, key) {
|
|
90
92
|
return key.split('.').reduce(getByKey, item);
|
|
@@ -103,7 +105,7 @@ function uniqueArrayItemValidate(keys, items, _schema, dataPath) {
|
|
|
103
105
|
.filter(function (index) { return index >= 0; })
|
|
104
106
|
.sort();
|
|
105
107
|
uniqueArrayItemValidate.errors = indexes.map(function (index) { return ({
|
|
106
|
-
dataPath: dataPath
|
|
108
|
+
dataPath: "".concat(dataPath, "[").concat(index, "]"),
|
|
107
109
|
keyword: uniqueArrayItemKeyword,
|
|
108
110
|
message: 'every item in the list should be unique',
|
|
109
111
|
params: { keyword: uniqueArrayItemKeyword, keys: keys }
|
|
@@ -121,7 +123,7 @@ function arraySameSizeValidate(keys, items, _schema, dataPath, item) {
|
|
|
121
123
|
return current !== expected ? {
|
|
122
124
|
dataPath: dataPath,
|
|
123
125
|
keyword: arraySameSizeKeyword,
|
|
124
|
-
message: "must contain as many items as "
|
|
126
|
+
message: "must contain as many items as ".concat(key, "s"),
|
|
125
127
|
params: { keyword: arraySameSizeKeyword, current: current, expected: expected }
|
|
126
128
|
} : null;
|
|
127
129
|
})
|
|
@@ -130,7 +132,7 @@ function arraySameSizeValidate(keys, items, _schema, dataPath, item) {
|
|
|
130
132
|
return errors.length === 0;
|
|
131
133
|
}
|
|
132
134
|
exports.arraySameSizeValidate = arraySameSizeValidate;
|
|
133
|
-
|
|
135
|
+
var initAjv = function () {
|
|
134
136
|
var ajv = new Ajv({
|
|
135
137
|
schemaId: 'auto',
|
|
136
138
|
allErrors: true
|
|
@@ -149,6 +151,7 @@ exports.initAjv = function () {
|
|
|
149
151
|
});
|
|
150
152
|
return ajv;
|
|
151
153
|
};
|
|
154
|
+
exports.initAjv = initAjv;
|
|
152
155
|
/**
|
|
153
156
|
* Create a validator instance. Call with a Node to validate it.
|
|
154
157
|
*
|
|
@@ -156,18 +159,19 @@ exports.initAjv = function () {
|
|
|
156
159
|
* @param strictMode Allow validating non-existing nodes, i.e. without unique `@id`.
|
|
157
160
|
* @returns Function to validate a Node. Use `await validator()(node)`
|
|
158
161
|
*/
|
|
159
|
-
|
|
162
|
+
var validator = function (domain, strictMode) {
|
|
160
163
|
if (domain === void 0) { domain = 'https://www.hestia.earth'; }
|
|
161
164
|
if (strictMode === void 0) { strictMode = true; }
|
|
162
|
-
var ajv = exports.initAjv();
|
|
163
|
-
var schemas = json_schema_1.loadSchemas();
|
|
165
|
+
var ajv = (0, exports.initAjv)();
|
|
166
|
+
var schemas = (0, json_schema_1.loadSchemas)();
|
|
164
167
|
Object.keys(schemas).map(function (schemaName) {
|
|
165
168
|
var schema = schemas[schemaName];
|
|
166
169
|
schema.properties = __assign({ '@context': {
|
|
167
170
|
type: 'string'
|
|
168
171
|
} }, schema.properties);
|
|
169
|
-
schema.oneOf = strictMode && schema_1.isTypeNode(schemaName) ? [existingNodeRequired] : schema.oneOf;
|
|
170
|
-
ajv.addSchema(schema, domain
|
|
172
|
+
schema.oneOf = strictMode && (0, schema_1.isTypeNode)(schemaName) ? [existingNodeRequired] : schema.oneOf;
|
|
173
|
+
ajv.addSchema(schema, "".concat(domain, "/schema/json-schema/").concat(schemaName, "#"));
|
|
171
174
|
});
|
|
172
|
-
return exports.validateContent(ajv, schemas);
|
|
175
|
+
return (0, exports.validateContent)(ajv, schemas);
|
|
173
176
|
};
|
|
177
|
+
exports.validator = validator;
|