@hestia-earth/schema-convert 25.0.1 → 25.1.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.
Files changed (3) hide show
  1. package/json.d.ts +11 -1
  2. package/json.js +102 -63
  3. package/package.json +1 -1
package/json.d.ts CHANGED
@@ -24,7 +24,17 @@ export interface ICSVError {
24
24
  headers?: ICSVHeader[];
25
25
  suggestions?: string[];
26
26
  }
27
- export declare const throwCSVError: <T extends ICSVError>(error: T) => never;
27
+ export interface ICSVErrors {
28
+ errors: ICSVError[];
29
+ }
30
+ export declare enum ErrorKeys {
31
+ SchemaNotFound = "schema-not-found",
32
+ PropertyNotFound = "property-not-found",
33
+ PropertyInvalidFormat = "property-invalid-format",
34
+ DuplicatedIdFields = "duplicated-id-fields",
35
+ ObjectArrayInvalid = "object-array-invalid"
36
+ }
37
+ export declare const throwCSVErrors: <T extends ICSVError>(errors: T[]) => never;
28
38
  export declare const cleanStringValue: (value: string) => string;
29
39
  export declare const formatNode: (schemas: definitions, type: SchemaType, data: ICSVContent, ignoreInternal: boolean, top?: boolean) => any;
30
40
  export declare const filterEmptyNode: (schemas: definitions, node: any) => boolean;
package/json.js CHANGED
@@ -84,7 +84,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
84
84
  };
85
85
  var _a;
86
86
  Object.defineProperty(exports, "__esModule", { value: true });
87
- exports.toJson = exports.filterEmptyNode = exports.formatNode = exports.cleanStringValue = exports.throwCSVError = exports.acceptedNodeTypes = void 0;
87
+ exports.toJson = exports.filterEmptyNode = exports.formatNode = exports.cleanStringValue = exports.throwCSVErrors = exports.ErrorKeys = exports.acceptedNodeTypes = void 0;
88
88
  var csvtojson = require("csvtojson");
89
89
  var levenshtein = require("fast-levenshtein");
90
90
  var wkt_1 = require("@terraformer/wkt");
@@ -93,6 +93,14 @@ var types_1 = require("@hestia-earth/json-schema/types");
93
93
  var utils_1 = require("./utils");
94
94
  var default_values_1 = require("./default-values");
95
95
  exports.acceptedNodeTypes = Object.values(schema_1.NodeType);
96
+ var ErrorKeys;
97
+ (function (ErrorKeys) {
98
+ ErrorKeys["SchemaNotFound"] = "schema-not-found";
99
+ ErrorKeys["PropertyNotFound"] = "property-not-found";
100
+ ErrorKeys["PropertyInvalidFormat"] = "property-invalid-format";
101
+ ErrorKeys["DuplicatedIdFields"] = "duplicated-id-fields";
102
+ ErrorKeys["ObjectArrayInvalid"] = "object-array-invalid";
103
+ })(ErrorKeys = exports.ErrorKeys || (exports.ErrorKeys = {}));
96
104
  var IGNORE_FIELD_KEY = '-';
97
105
  var VALUE_TYPE_KEY = 'valueType';
98
106
  var DEFAULT_ARRAY_DELIMITER = ';';
@@ -162,17 +170,19 @@ var propertyRequiredValue = function (value, required) {
162
170
  var val = typeof value === 'object' && required[0] in value ? value[required[0]] : value;
163
171
  return nonEmptyCell(val) ? (_a = {}, _a[required[0]] = val, _a) : {};
164
172
  };
165
- var parseError = function (err) { return safeParseJSON(err.message); };
173
+ var parseErrors = function (err) { var _a; return ((_a = safeParseJSON(err.message)) === null || _a === void 0 ? void 0 : _a.errors) || []; };
166
174
  var throwError = function (error) {
167
175
  throw new Error(error);
168
176
  };
169
- var throwCSVError = function (error) { return throwError(JSON.stringify(error)); };
170
- exports.throwCSVError = throwCSVError;
177
+ var throwCSVErrors = function (errors) { return throwError(JSON.stringify({ errors: errors })); };
178
+ exports.throwCSVErrors = throwCSVErrors;
171
179
  var schemaNotFoundError = function (schema) {
172
- return (0, exports.throwCSVError)({
173
- message: 'schema-not-found',
174
- schema: schema
175
- });
180
+ return (0, exports.throwCSVErrors)([
181
+ {
182
+ message: ErrorKeys.SchemaNotFound,
183
+ schema: schema
184
+ }
185
+ ]);
176
186
  };
177
187
  var computeSuggestions = function (_a, key) {
178
188
  var title = _a.title, properties = _a.properties;
@@ -183,42 +193,49 @@ var computeSuggestions = function (_a, key) {
183
193
  : allKeys.filter(function (v) { return levenshtein.get(v, key) <= 3; });
184
194
  };
185
195
  var propertyNotFoundError = function (schema, key, value) {
186
- return (0, exports.throwCSVError)({
187
- schema: schema.title,
188
- message: 'property-not-found',
189
- schemaKey: key,
190
- key: key,
191
- value: value,
192
- suggestions: computeSuggestions(schema, key)
193
- });
196
+ return (0, exports.throwCSVErrors)([
197
+ {
198
+ schema: schema.title,
199
+ message: ErrorKeys.PropertyNotFound,
200
+ schemaKey: key,
201
+ key: key,
202
+ value: value,
203
+ suggestions: computeSuggestions(schema, key)
204
+ }
205
+ ]);
194
206
  };
195
207
  var propertyInvalidFormat = function (schema, key, value, func) {
196
208
  try {
197
209
  return func();
198
210
  }
199
211
  catch (err) {
200
- var data = parseError(err);
212
+ var errors = parseErrors(err);
201
213
  // throw already handled error or throw a generic invalid-format error
202
- return (0, exports.throwCSVError)(data
203
- ? __assign(__assign({}, data), { key: compileFullKey(key, data.key) }) : {
204
- schema: schema.title,
205
- message: 'property-invalid-format',
206
- schemaKey: key,
207
- key: key,
208
- value: value,
209
- error: err === null || err === void 0 ? void 0 : err.message
210
- });
214
+ return (0, exports.throwCSVErrors)(errors.length
215
+ ? errors.map(function (data) { return (__assign(__assign({}, data), { key: compileFullKey(key, data.key) })); })
216
+ : [
217
+ {
218
+ schema: schema.title,
219
+ message: ErrorKeys.PropertyInvalidFormat,
220
+ schemaKey: key,
221
+ key: key,
222
+ value: value,
223
+ error: err === null || err === void 0 ? void 0 : err.message
224
+ }
225
+ ]);
211
226
  }
212
227
  };
213
228
  var validateDuplicatedIdFields = function (schema, key, value) {
214
229
  return typeof value === 'object' && [value.id, value['@id']].every(function (v) { return !!v && nonEmptyCell(v); })
215
- ? (0, exports.throwCSVError)({
216
- schema: schema.title,
217
- message: 'duplicated-id-fields',
218
- schemaKey: key,
219
- key: key,
220
- value: value
221
- })
230
+ ? (0, exports.throwCSVErrors)([
231
+ {
232
+ schema: schema.title,
233
+ message: ErrorKeys.DuplicatedIdFields,
234
+ schemaKey: key,
235
+ key: key,
236
+ value: value
237
+ }
238
+ ])
222
239
  : null;
223
240
  };
224
241
  var handleArrayError = function (func) { return function (value, index) {
@@ -226,10 +243,10 @@ var handleArrayError = function (func) { return function (value, index) {
226
243
  return func(value, index);
227
244
  }
228
245
  catch (err) {
229
- var data = parseError(err);
246
+ var errors = parseErrors(err);
230
247
  // throw already handled error or throw a generic invalid-format error
231
- return data
232
- ? (0, exports.throwCSVError)(__assign(__assign({}, data), { key: compileFullKey("".concat(index), data.key) }))
248
+ return errors.length
249
+ ? (0, exports.throwCSVErrors)(errors.map(function (data) { return (__assign(__assign({}, data), { key: compileFullKey("".concat(index), data.key) })); }))
233
250
  : (function () {
234
251
  throw err;
235
252
  })();
@@ -385,7 +402,9 @@ var propertyTypeToValue = {
385
402
  ? '$ref' in items
386
403
  ? propertyTypeToValue.object(val, schemas, items, _i)
387
404
  : Array.isArray(items.type)
388
- ? propertyTypeToValue.auto(val, schemas, items, _i)
405
+ ? val === IGNORE_FIELD_KEY
406
+ ? null
407
+ : propertyTypeToValue.auto(val, schemas, items, _i)
389
408
  : propertyTypeToValue[items.type](val, schemas, items, _i)
390
409
  : val;
391
410
  }))
@@ -487,35 +506,55 @@ var validateArrayType = function (schema, def, key, value) {
487
506
  return typeof value !== 'object' ||
488
507
  def.type !== 'array' ||
489
508
  Array.isArray(value) ||
490
- (0, exports.throwCSVError)({
491
- schema: schema.title,
492
- message: 'object-array-invalid',
493
- schemaKey: key,
494
- key: key,
495
- value: value
496
- });
509
+ (0, exports.throwCSVErrors)([
510
+ {
511
+ schema: schema.title,
512
+ message: ErrorKeys.ObjectArrayInvalid,
513
+ schemaKey: key,
514
+ key: key,
515
+ value: value
516
+ }
517
+ ]);
497
518
  };
498
519
  var mapContent = function (schemas, schema, ignoreInternal) { return function (json) {
499
- return Object.keys(json)
520
+ var values = Object.keys(json)
500
521
  .filter(nonEmptyCell)
501
- .reduce(function (prev, key) {
502
- var _a;
503
- var value = json[key];
504
- // make sure we are not using both `id` and `@id` fields
505
- validateDuplicatedIdFields(schema, key, value);
506
- var propertyDefinition = getPropertyDefinition(schema, key, false, json);
507
- var valueType = getValueType(schema, json);
508
- var type = getPropertyType(propertyDefinition, key, valueType);
509
- validateArrayType(schema, propertyDefinition, key, value);
510
- var newValue = key === VALUE_TYPE_KEY
511
- ? ''
512
- : propertyInvalidFormat(schema, key, value, function () {
513
- return Array.isArray(type)
514
- ? value
515
- : propertyTypeToValue[type](value, schemas, propertyDefinition, ignoreInternal);
516
- });
517
- return __assign(__assign(__assign({}, prev), (schema.$id ? { type: schema.title } : {})), (type !== 'null' && isEmptyValue(newValue, schemas) ? {} : (_a = {}, _a[key] = newValue, _a)));
518
- }, {});
522
+ .map(function (key) {
523
+ try {
524
+ var value_1 = json[key];
525
+ // make sure we are not using both `id` and `@id` fields
526
+ validateDuplicatedIdFields(schema, key, value_1);
527
+ var propertyDefinition_1 = getPropertyDefinition(schema, key, false, json);
528
+ var valueType = getValueType(schema, json);
529
+ var type_1 = getPropertyType(propertyDefinition_1, key, valueType);
530
+ validateArrayType(schema, propertyDefinition_1, key, value_1);
531
+ var newValue = key === VALUE_TYPE_KEY
532
+ ? ''
533
+ : propertyInvalidFormat(schema, key, value_1, function () {
534
+ return Array.isArray(type_1)
535
+ ? value_1
536
+ : propertyTypeToValue[type_1](value_1, schemas, propertyDefinition_1, ignoreInternal);
537
+ });
538
+ return { errors: [], value: type_1 !== 'null' && isEmptyValue(newValue, schemas) ? undefined : [key, newValue] };
539
+ }
540
+ catch (err) {
541
+ var errors_1 = JSON.parse(err.message).errors;
542
+ return { errors: errors_1 };
543
+ }
544
+ });
545
+ var errors = values.filter(function (_a) {
546
+ var errors = _a.errors;
547
+ return (errors === null || errors === void 0 ? void 0 : errors.length) > 0;
548
+ }).flatMap(function (_a) {
549
+ var errors = _a.errors;
550
+ return errors;
551
+ });
552
+ return (errors === null || errors === void 0 ? void 0 : errors.length)
553
+ ? throwError(JSON.stringify({ errors: errors }))
554
+ : Object.fromEntries(__spreadArray([schema.$id ? ['type', schema.title] : null], __read(values.map(function (_a) {
555
+ var value = _a.value;
556
+ return value;
557
+ })), false).filter(Boolean));
519
558
  }; };
520
559
  var formatNode = function (schemas, type, data, ignoreInternal, top) {
521
560
  if (top === void 0) { top = false; }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hestia-earth/schema-convert",
3
- "version": "25.0.1",
3
+ "version": "25.1.0",
4
4
  "description": "Hestia Schema Converters",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",