@hestia-earth/schema-validation 7.6.6 → 7.7.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hestia-earth/schema-validation",
3
- "version": "7.6.6",
3
+ "version": "7.7.0",
4
4
  "description": "Hestia Schema Validation",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
@@ -51,6 +51,17 @@ var __read = (this && this.__read) || function (o, n) {
51
51
  }
52
52
  return ar;
53
53
  };
54
+ var __values = (this && this.__values) || function(o) {
55
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
56
+ if (m) return m.call(o);
57
+ if (o && typeof o.length === "number") return {
58
+ next: function () {
59
+ if (o && i >= o.length) o = void 0;
60
+ return { value: o && o[i++], done: !o };
61
+ }
62
+ };
63
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
64
+ };
54
65
  Object.defineProperty(exports, "__esModule", { value: true });
55
66
  exports.run = void 0;
56
67
  var fs_1 = require("fs");
@@ -72,43 +83,64 @@ var findFiles = function (directory) { return fs_1.readdirSync(directory)
72
83
  .flat()
73
84
  .filter(Boolean); };
74
85
  exports.run = function () { return __awaiter(void 0, void 0, void 0, function () {
75
- var contentValidator, jsonldFiles, results;
76
- return __generator(this, function (_a) {
77
- switch (_a.label) {
86
+ var contentValidator, jsonldFiles, results, jsonldFiles_1, jsonldFiles_1_1, filepath, content, nodes, allErrors, success, e_1_1;
87
+ var e_1, _a;
88
+ return __generator(this, function (_b) {
89
+ switch (_b.label) {
78
90
  case 0:
79
91
  contentValidator = validate_1.validator(domain, strictMode === 'true');
80
92
  jsonldFiles = findFiles(folder);
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
- }); }))];
93
+ results = [];
94
+ _b.label = 1;
106
95
  case 1:
107
- results = (_a.sent())
108
- .filter(function (_a) {
109
- var success = _a.success;
110
- return !success;
96
+ _b.trys.push([1, 6, 7, 8]);
97
+ jsonldFiles_1 = __values(jsonldFiles), jsonldFiles_1_1 = jsonldFiles_1.next();
98
+ _b.label = 2;
99
+ case 2:
100
+ if (!!jsonldFiles_1_1.done) return [3 /*break*/, 5];
101
+ filepath = jsonldFiles_1_1.value;
102
+ console.log('Validating', filepath);
103
+ content = loadFile(filepath);
104
+ nodes = Array.isArray(content) ? content : ('nodes' in content ? content.nodes : [content]);
105
+ return [4 /*yield*/, Promise.all(nodes
106
+ .map(function (node) { return node['@type'] || node.type ? node : null; })
107
+ .filter(Boolean)
108
+ .map(function (content) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
109
+ switch (_a.label) {
110
+ case 0: return [4 /*yield*/, contentValidator(content)];
111
+ case 1: return [2 /*return*/, _a.sent()];
112
+ }
113
+ }); }); }))];
114
+ case 3:
115
+ allErrors = (_b.sent())
116
+ .map(function (_a) {
117
+ var errors = _a.errors;
118
+ return errors;
111
119
  });
120
+ success = !allErrors.some(function (v) { return v.length; });
121
+ if (!success) {
122
+ results.push({
123
+ filepath: filepath,
124
+ success: success,
125
+ errors: allErrors
126
+ });
127
+ }
128
+ _b.label = 4;
129
+ case 4:
130
+ jsonldFiles_1_1 = jsonldFiles_1.next();
131
+ return [3 /*break*/, 2];
132
+ case 5: return [3 /*break*/, 8];
133
+ case 6:
134
+ e_1_1 = _b.sent();
135
+ e_1 = { error: e_1_1 };
136
+ return [3 /*break*/, 8];
137
+ case 7:
138
+ try {
139
+ if (jsonldFiles_1_1 && !jsonldFiles_1_1.done && (_a = jsonldFiles_1.return)) _a.call(jsonldFiles_1);
140
+ }
141
+ finally { if (e_1) throw e_1.error; }
142
+ return [7 /*endfinally*/];
143
+ case 8:
112
144
  if (results.length) {
113
145
  throw new Error("Validation errors: " + JSON.stringify(results, null, 2));
114
146
  }
package/validate.d.ts CHANGED
@@ -12,8 +12,9 @@ export declare const validateContent: (ajv: Ajv.Ajv, schemas: definitions) => (c
12
12
  success: any;
13
13
  errors: Ajv.ErrorObject[];
14
14
  }>;
15
- declare function uniqueArrayItemValidate(keys: string[], items: any[], schema?: any, dataPath?: string): boolean;
16
- export { uniqueArrayItemValidate };
15
+ declare function uniqueArrayItemValidate(keys: string[], items: any[], _schema?: any, dataPath?: string): boolean;
16
+ declare function arraySameSizeValidate(keys: string[], items: any[], _schema: any, dataPath: any, item: any): boolean;
17
+ export { uniqueArrayItemValidate, arraySameSizeValidate };
17
18
  export declare const initAjv: () => Ajv.Ajv;
18
19
  /**
19
20
  * Create a validator instance. Call with a Node to validate it.
package/validate.js CHANGED
@@ -47,7 +47,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
47
47
  }
48
48
  };
49
49
  Object.defineProperty(exports, "__esModule", { value: true });
50
- exports.validator = exports.initAjv = exports.uniqueArrayItemValidate = exports.validateContent = void 0;
50
+ exports.validator = exports.initAjv = exports.arraySameSizeValidate = exports.uniqueArrayItemValidate = exports.validateContent = void 0;
51
51
  var Ajv = require("ajv");
52
52
  var schema_1 = require("@hestia-earth/schema");
53
53
  var json_schema_1 = require("@hestia-earth/json-schema");
@@ -90,7 +90,7 @@ var getItemValue = function (item, key) {
90
90
  return key.split('.').reduce(getByKey, item);
91
91
  };
92
92
  var uniqueArrayItemKeyword = 'uniqueArrayItem';
93
- function uniqueArrayItemValidate(keys, items, schema, dataPath) {
93
+ function uniqueArrayItemValidate(keys, items, _schema, dataPath) {
94
94
  if (dataPath === void 0) { dataPath = ''; }
95
95
  var values = items.map(function (item) { return JSON.stringify(keys.reduce(function (prev, key) {
96
96
  var _a;
@@ -111,6 +111,25 @@ function uniqueArrayItemValidate(keys, items, schema, dataPath) {
111
111
  return indexes.length === 0;
112
112
  }
113
113
  exports.uniqueArrayItemValidate = uniqueArrayItemValidate;
114
+ var arraySameSizeKeyword = 'arraySameSize';
115
+ function arraySameSizeValidate(keys, items, _schema, dataPath, item) {
116
+ var current = items.length;
117
+ var errors = keys
118
+ .filter(function (key) { return key in item; })
119
+ .map(function (key) {
120
+ var expected = item[key].length;
121
+ return current !== expected ? {
122
+ dataPath: dataPath,
123
+ keyword: arraySameSizeKeyword,
124
+ message: "must contain as many items as " + key + "s",
125
+ params: { keyword: arraySameSizeKeyword, current: current, expected: expected }
126
+ } : null;
127
+ })
128
+ .filter(Boolean);
129
+ arraySameSizeValidate.errors = errors;
130
+ return errors.length === 0;
131
+ }
132
+ exports.arraySameSizeValidate = arraySameSizeValidate;
114
133
  exports.initAjv = function () {
115
134
  var ajv = new Ajv({
116
135
  schemaId: 'auto',
@@ -123,6 +142,11 @@ exports.initAjv = function () {
123
142
  errors: 'full',
124
143
  validate: uniqueArrayItemValidate
125
144
  });
145
+ ajv.addKeyword(arraySameSizeKeyword, {
146
+ type: 'array',
147
+ errors: 'full',
148
+ validate: arraySameSizeValidate
149
+ });
126
150
  return ajv;
127
151
  };
128
152
  /**