@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.
- package/json.d.ts +11 -1
- package/json.js +102 -63
- 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
|
|
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.
|
|
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
|
|
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
|
|
170
|
-
exports.
|
|
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.
|
|
173
|
-
|
|
174
|
-
|
|
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.
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
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
|
|
212
|
+
var errors = parseErrors(err);
|
|
201
213
|
// throw already handled error or throw a generic invalid-format error
|
|
202
|
-
return (0, exports.
|
|
203
|
-
? __assign(__assign({}, data), { key: compileFullKey(key, data.key) })
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
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.
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
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
|
|
246
|
+
var errors = parseErrors(err);
|
|
230
247
|
// throw already handled error or throw a generic invalid-format error
|
|
231
|
-
return
|
|
232
|
-
? (0, exports.
|
|
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
|
-
?
|
|
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.
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
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
|
-
|
|
520
|
+
var values = Object.keys(json)
|
|
500
521
|
.filter(nonEmptyCell)
|
|
501
|
-
.
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
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; }
|