@hestia-earth/schema-validation 7.5.1 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hestia-earth/schema-validation",
3
- "version": "7.5.1",
3
+ "version": "7.6.1",
4
4
  "description": "Hestia Schema Validation",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
@@ -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: return [4 /*yield*/, validate_1.validator(domain, strictMode === 'true')];
79
- case 1:
80
- contentValidator = _a.sent();
78
+ case 0:
79
+ contentValidator = validate_1.validator(domain, strictMode === 'true');
81
80
  jsonldFiles = findFiles(folder);
82
- results = jsonldFiles
83
- .map(function (filepath) {
84
- var content = loadFile(filepath);
85
- var nodes = Array.isArray(content) ? content : ('nodes' in content ? content.nodes : [content]);
86
- var allErrors = nodes
87
- .map(function (node) { return node['@type'] || node.type ? node : null; })
88
- .filter(Boolean)
89
- .map(contentValidator)
90
- .map(function (_a) {
91
- var errors = _a.errors;
92
- return errors;
93
- });
94
- return {
95
- filepath: filepath,
96
- success: !allErrors.some(function (v) { return v.length; }),
97
- errors: allErrors
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
- export declare const validateContent: (ajv: Ajv.Ajv, schemas: definitions) => (content: HestiaJson<SchemaType>) => {
5
- success: boolean | PromiseLike<any>;
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) => Promise<(content: HestiaJson<SchemaType>) => {
19
- success: boolean | PromiseLike<any>;
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
@@ -60,9 +60,30 @@ var validateSchemaType = function (schemas, content) {
60
60
  }
61
61
  return true;
62
62
  };
63
- exports.validateContent = function (ajv, schemas) { return function (content) { return validateSchemaType(schemas, content) && ({
64
- success: ajv.validate(schemas[content['@type'] || content.type], content),
65
- errors: ajv.errors || []
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
+ });
66
87
  }); }; };
67
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)); };
68
89
  var getItemValue = function (item, key) {
@@ -114,25 +135,15 @@ exports.initAjv = function () {
114
135
  exports.validator = function (domain, strictMode) {
115
136
  if (domain === void 0) { domain = 'https://www.hestia.earth'; }
116
137
  if (strictMode === void 0) { strictMode = true; }
117
- return __awaiter(void 0, void 0, void 0, function () {
118
- var ajv, schemas;
119
- return __generator(this, function (_a) {
120
- switch (_a.label) {
121
- case 0:
122
- ajv = exports.initAjv();
123
- return [4 /*yield*/, json_schema_1.loadSchemas()];
124
- case 1:
125
- schemas = _a.sent();
126
- Object.keys(schemas).map(function (schemaName) {
127
- var schema = schemas[schemaName];
128
- schema.properties = __assign({ '@context': {
129
- type: 'string'
130
- } }, schema.properties);
131
- schema.oneOf = strictMode && schema_1.isTypeNode(schemaName) ? [existingNodeRequired] : schema.oneOf;
132
- ajv.addSchema(schema, domain + "/schema/json-schema}/" + schemaName + "#");
133
- });
134
- return [2 /*return*/, exports.validateContent(ajv, schemas)];
135
- }
136
- });
138
+ var ajv = exports.initAjv();
139
+ var schemas = json_schema_1.loadSchemas();
140
+ Object.keys(schemas).map(function (schemaName) {
141
+ var schema = schemas[schemaName];
142
+ schema.properties = __assign({ '@context': {
143
+ type: 'string'
144
+ } }, schema.properties);
145
+ schema.oneOf = strictMode && schema_1.isTypeNode(schemaName) ? [existingNodeRequired] : schema.oneOf;
146
+ ajv.addSchema(schema, domain + "/schema/json-schema/" + schemaName + "#");
137
147
  });
148
+ return exports.validateContent(ajv, schemas);
138
149
  };