@hestia-earth/schema-convert 23.4.0 → 23.5.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 (2) hide show
  1. package/json.js +159 -145
  2. package/package.json +1 -1
package/json.js CHANGED
@@ -82,7 +82,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
82
82
  }
83
83
  return to.concat(ar || Array.prototype.slice.call(from));
84
84
  };
85
- var _a, _b;
85
+ var _a;
86
86
  Object.defineProperty(exports, "__esModule", { value: true });
87
87
  exports.toJson = exports.filterEmptyNode = exports.formatNode = exports.cleanStringValue = exports.throwCSVError = exports.acceptedNodeTypes = void 0;
88
88
  var csvtojson = require("csvtojson");
@@ -92,7 +92,7 @@ var schema_1 = require("@hestia-earth/schema");
92
92
  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
- exports.acceptedNodeTypes = Object.values(schema_1.NodeType).filter(function (val) { return val !== schema_1.NodeType.Term; });
95
+ exports.acceptedNodeTypes = Object.values(schema_1.NodeType);
96
96
  var IGNORE_FIELD_KEY = '-';
97
97
  var VALUE_TYPE_KEY = 'valueType';
98
98
  var DEFAULT_ARRAY_DELIMITER = ';';
@@ -150,7 +150,7 @@ var isEmptyValueType = (_a = {
150
150
  },
151
151
  _a);
152
152
  var isEmptyValue = function (value, schemas) {
153
- return (typeof value) in isEmptyValueType ? isEmptyValueType[typeof value](value, schemas) : isEmptyCell(value);
153
+ return typeof value in isEmptyValueType ? isEmptyValueType[typeof value](value, schemas) : isEmptyCell(value);
154
154
  };
155
155
  var compileFullKey = function (key1, key2) {
156
156
  // handle arrays
@@ -168,24 +168,28 @@ var throwError = function (error) {
168
168
  };
169
169
  var throwCSVError = function (error) { return throwError(JSON.stringify(error)); };
170
170
  exports.throwCSVError = throwCSVError;
171
- var schemaNotFoundError = function (schema) { return (0, exports.throwCSVError)({
172
- message: 'schema-not-found',
173
- schema: schema
174
- }); };
171
+ var schemaNotFoundError = function (schema) {
172
+ return (0, exports.throwCSVError)({
173
+ message: 'schema-not-found',
174
+ schema: schema
175
+ });
176
+ };
175
177
  var computeSuggestions = function (_a, key) {
176
178
  var properties = _a.properties;
177
179
  var internal = internalProperties({ properties: properties });
178
180
  var allKeys = Object.keys(properties).filter(function (k) { return !__spreadArray(__spreadArray([], __read(types_1.excludedDefaultProperties), false), __read(internal), false).includes(k); });
179
181
  return allKeys.filter(function (v) { return levenshtein.get(v, key) <= 3; });
180
182
  };
181
- var propertyNotFoundError = function (schema, key, value) { return (0, exports.throwCSVError)({
182
- schema: schema.title,
183
- message: 'property-not-found',
184
- schemaKey: key,
185
- key: key,
186
- value: value,
187
- suggestions: computeSuggestions(schema, key)
188
- }); };
183
+ var propertyNotFoundError = function (schema, key, value) {
184
+ return (0, exports.throwCSVError)({
185
+ schema: schema.title,
186
+ message: 'property-not-found',
187
+ schemaKey: key,
188
+ key: key,
189
+ value: value,
190
+ suggestions: computeSuggestions(schema, key)
191
+ });
192
+ };
189
193
  var propertyInvalidFormat = function (schema, key, value, func) {
190
194
  try {
191
195
  return func();
@@ -193,7 +197,8 @@ var propertyInvalidFormat = function (schema, key, value, func) {
193
197
  catch (err) {
194
198
  var data = parseError(err);
195
199
  // throw already handled error or throw a generic invalid-format error
196
- return (0, exports.throwCSVError)(data ? __assign(__assign({}, data), { key: compileFullKey(key, data.key) }) : {
200
+ return (0, exports.throwCSVError)(data
201
+ ? __assign(__assign({}, data), { key: compileFullKey(key, data.key) }) : {
197
202
  schema: schema.title,
198
203
  message: 'property-invalid-format',
199
204
  schemaKey: key,
@@ -221,18 +226,14 @@ var handleArrayError = function (func) { return function (value, index) {
221
226
  catch (err) {
222
227
  var data = parseError(err);
223
228
  // throw already handled error or throw a generic invalid-format error
224
- return data ?
225
- (0, exports.throwCSVError)(__assign(__assign({}, data), { key: compileFullKey("".concat(index), data.key) })) :
226
- (function () {
229
+ return data
230
+ ? (0, exports.throwCSVError)(__assign(__assign({}, data), { key: compileFullKey("".concat(index), data.key) }))
231
+ : (function () {
227
232
  throw err;
228
233
  })();
229
234
  }
230
235
  }; };
231
- var allowedGeoJSONTypes = [
232
- 'FeatureCollection',
233
- 'Feature',
234
- 'GeometryCollection'
235
- ];
236
+ var allowedGeoJSONTypes = ['FeatureCollection', 'Feature', 'GeometryCollection'];
236
237
  var geoJSONGeomtryTypeValidation = {
237
238
  Point: function () { return throwError('use "latitude" and "longitude" instead of "Point"'); },
238
239
  MultiPoint: function () { return throwError('use a "Polygon" instead of "MultiPoint"'); },
@@ -271,11 +272,13 @@ var parseWtkValue = function (value) {
271
272
  try {
272
273
  return {
273
274
  type: 'FeatureCollection',
274
- features: [{
275
+ features: [
276
+ {
275
277
  type: 'Feature',
276
278
  properties: {},
277
279
  geometry: (0, wkt_1.wktToGeoJSON)(value)
278
- }]
280
+ }
281
+ ]
279
282
  };
280
283
  }
281
284
  catch (error) {
@@ -288,17 +291,21 @@ var parseJsonValue = function (value) {
288
291
  return ['Polygon', 'MultiPolygon'].includes(data.type)
289
292
  ? {
290
293
  type: 'FeatureCollection',
291
- features: [{
294
+ features: [
295
+ {
292
296
  type: 'Feature',
293
297
  properties: {},
294
298
  geometry: data
295
- }]
299
+ }
300
+ ]
296
301
  }
297
302
  : ['Feature'].includes(data.type)
298
303
  ? {
299
304
  type: 'FeatureCollection',
300
- features: [__assign(__assign({}, data), { properties: {} // make sure they are set or it is invalid
301
- })]
305
+ features: [
306
+ __assign(__assign({}, data), { properties: {} // make sure they are set or it is invalid
307
+ })
308
+ ]
302
309
  }
303
310
  : data;
304
311
  }
@@ -326,113 +333,120 @@ var schemaRefValue = function (value, type) {
326
333
  ? { term: { name: value } }
327
334
  : { id: value };
328
335
  };
329
- var cleanStringValue = function (value) { return (value.match(/^[\d]+\.[\d]+\.[\d]+$/) === null ? value.replace(/\.0$/, '') : value).trim(); };
336
+ var cleanStringValue = function (value) {
337
+ return (value.match(/^[\d]+\.[\d]+\.[\d]+$/) === null ? value.replace(/\.0$/, '') : value).trim();
338
+ };
330
339
  exports.cleanStringValue = cleanStringValue;
331
- var geojsonRef = 'http://json.schemastore.org/geojson';
332
- var propertyTypeToValue = (_b = {
333
- null: function (value) {
334
- var args = [];
335
- for (var _a = 1; _a < arguments.length; _a++) {
336
- args[_a - 1] = arguments[_a];
337
- }
338
- return isEmptyCell(value) ? null : propertyTypeToValue.auto.apply(propertyTypeToValue, __spreadArray([value], __read(args), false));
339
- },
340
- string: function (value) { return (0, exports.cleanStringValue)(value || ''); },
341
- number: function (value) {
342
- return isEmptyCell(value)
343
- ? undefined
344
- : isNaN(+value)
345
- ? (function () {
346
- throw new Error('failed to parse number');
347
- })()
348
- : +value;
349
- },
350
- integer: function (value) { return isEmptyCell(value) ? undefined : +value; },
351
- boolean: function (value) {
352
- return isEmptyCell(value)
353
- ? undefined
354
- : (0, utils_1.isBoolean)(value)
355
- ? value.toLowerCase() === 'true'
356
- : (0, utils_1.isNumber)(value)
357
- ? +value === 1 // handle setting 1 or 1.0 as true
358
- : (function () {
359
- throw new Error('failed to parse boolean, expected true or false');
360
- })();
361
- },
362
- object: function (value, schemas, _a, _i) {
363
- var $ref = _a.$ref, required = _a.required;
364
- return $ref === geojsonRef
365
- ? propertyTypeToValue[geojsonRef](value)
366
- : $ref
367
- ? propertyTypeToValue.$ref(value, schemas, { $ref: $ref }, _i)
368
- : propertyTypeToValue.required(value, schemas, { required: required }, _i);
369
- },
370
- array: function (value, schemas, _a, _i) {
371
- var items = _a.items;
372
- return (Array.isArray(value) ? value : value.split(arrayDelimiter()))
373
- .map(handleArrayError(function (val) { return items ? ('$ref' in items ?
374
- propertyTypeToValue.object(val, schemas, items, _i) :
375
- Array.isArray(items.type) ?
376
- propertyTypeToValue.auto(val, schemas, items, _i) :
377
- propertyTypeToValue[items.type](val, schemas, items, _i)) : val; }))
378
- .filter(function (val) { return !isEmptyValue(val, schemas); });
379
- },
380
- // try to determine the type automatically
381
- auto: function (value, schemas, _d, _i) {
382
- // iris are mapped as {@id: val}
383
- return (0, utils_1.isIri)(value)
384
- ? propertyTypeToValue.required(value, schemas, { required: ['@id'] }, _i)
385
- : (0, utils_1.isBoolean)(value)
386
- ? propertyTypeToValue.boolean(value)
387
- : (0, utils_1.isNumber)(value)
388
- ? propertyTypeToValue.number(value)
389
- : value === 'null'
390
- ? null
391
- : propertyTypeToValue.string(value);
392
- },
393
- required: function (value, _schemas, _a) {
394
- var required = _a.required;
395
- var data = propertyRequiredValue(value, required);
396
- return (0, utils_1.isEmpty)(data) ? {} : data;
397
- },
398
- $ref: function (value, schemas, _a, _i) {
399
- var $ref = _a.$ref;
400
- var schemaType = (0, schema_1.refToSchemaType)($ref);
401
- var schema = schemaType ? schemas[schemaType] : undefined;
402
- var data = schema
403
- ? mapContent(schemas, schema, _i)(schemaRefValue(value, schemaType))
404
- : $ref in propertyTypeToValue
405
- ? propertyTypeToValue[$ref](value)
406
- : safeParseJSON(value);
407
- var includeDefaults = (0, schema_1.isBlankNode)(data) || schemaType === schema_1.SchemaType.Actor; // only blank nodes or actors allowed
408
- return (0, utils_1.isEmpty)(data)
409
- ? {}
410
- : !!schema
411
- ? extendDataFromSchema(data, schema, schemaType, _i, includeDefaults)
412
- : data;
340
+ var propertyTypeToValue = {
341
+ null: function (value) {
342
+ var args = [];
343
+ for (var _a = 1; _a < arguments.length; _a++) {
344
+ args[_a - 1] = arguments[_a];
413
345
  }
346
+ return (isEmptyCell(value) ? null : propertyTypeToValue.auto.apply(propertyTypeToValue, __spreadArray([value], __read(args), false)));
347
+ },
348
+ string: function (value) { return (0, exports.cleanStringValue)(value || ''); },
349
+ number: function (value) {
350
+ return isEmptyCell(value)
351
+ ? undefined
352
+ : isNaN(+value)
353
+ ? (function () {
354
+ throw new Error('failed to parse number');
355
+ })()
356
+ : +value;
357
+ },
358
+ integer: function (value) { return (isEmptyCell(value) ? undefined : +value); },
359
+ boolean: function (value) {
360
+ return isEmptyCell(value)
361
+ ? undefined
362
+ : (0, utils_1.isBoolean)(value)
363
+ ? value.toLowerCase() === 'true'
364
+ : (0, utils_1.isNumber)(value)
365
+ ? +value === 1 // handle setting 1 or 1.0 as true
366
+ : (function () {
367
+ throw new Error('failed to parse boolean, expected true or false');
368
+ })();
369
+ },
370
+ object: function (value, schemas, _a, _i) {
371
+ var $ref = _a.$ref, required = _a.required, geojson = _a.geojson;
372
+ return geojson
373
+ ? propertyTypeToValue.geojson(value)
374
+ : $ref
375
+ ? propertyTypeToValue.$ref(value, schemas, { $ref: $ref }, _i)
376
+ : propertyTypeToValue.required(value, schemas, { required: required }, _i);
377
+ },
378
+ array: function (value, schemas, _a, _i) {
379
+ var items = _a.items;
380
+ return (Array.isArray(value) ? value : value.split(arrayDelimiter()))
381
+ .map(handleArrayError(function (val) {
382
+ return items
383
+ ? '$ref' in items
384
+ ? propertyTypeToValue.object(val, schemas, items, _i)
385
+ : Array.isArray(items.type)
386
+ ? propertyTypeToValue.auto(val, schemas, items, _i)
387
+ : propertyTypeToValue[items.type](val, schemas, items, _i)
388
+ : val;
389
+ }))
390
+ .filter(function (val) { return !isEmptyValue(val, schemas); });
414
391
  },
415
- _b[geojsonRef] = function (value) { return isEmptyCell(value) ? undefined : parseGeoJSONValue(value); },
416
- _b);
392
+ // try to determine the type automatically
393
+ auto: function (value, schemas, _d, _i) {
394
+ // iris are mapped as {@id: val}
395
+ return (0, utils_1.isIri)(value)
396
+ ? propertyTypeToValue.required(value, schemas, { required: ['@id'] }, _i)
397
+ : (0, utils_1.isBoolean)(value)
398
+ ? propertyTypeToValue.boolean(value)
399
+ : (0, utils_1.isNumber)(value)
400
+ ? propertyTypeToValue.number(value)
401
+ : value === 'null'
402
+ ? null
403
+ : propertyTypeToValue.string(value);
404
+ },
405
+ required: function (value, _schemas, _a) {
406
+ var required = _a.required;
407
+ var data = propertyRequiredValue(value, required);
408
+ return (0, utils_1.isEmpty)(data) ? {} : data;
409
+ },
410
+ $ref: function (value, schemas, _a, _i) {
411
+ var $ref = _a.$ref;
412
+ var schemaType = (0, schema_1.refToSchemaType)($ref);
413
+ var schema = schemaType ? schemas[schemaType] : undefined;
414
+ var data = schema
415
+ ? mapContent(schemas, schema, _i)(schemaRefValue(value, schemaType))
416
+ : $ref in propertyTypeToValue
417
+ ? propertyTypeToValue[$ref](value)
418
+ : safeParseJSON(value);
419
+ var includeDefaults = (0, schema_1.isBlankNode)(data) || schemaType === schema_1.SchemaType.Actor; // only blank nodes or actors allowed
420
+ return (0, utils_1.isEmpty)(data) ? {} : !!schema ? extendDataFromSchema(data, schema, schemaType, _i, includeDefaults) : data;
421
+ },
422
+ geojson: function (value) { return (isEmptyCell(value) ? undefined : parseGeoJSONValue(value)); }
423
+ };
417
424
  var getPropertyDefinition = function (schema, key, ignoreErrors, value) {
418
425
  if (ignoreErrors === void 0) { ignoreErrors = false; }
419
426
  return key in schema.properties ? schema.properties[key] : !ignoreErrors && propertyNotFoundError(schema, key, value);
420
427
  };
421
428
  var getValueType = function (schema, json) {
422
- return VALUE_TYPE_KEY in schema.properties ? (
423
- // valueType can be present but skipped
424
- (json[VALUE_TYPE_KEY] !== IGNORE_FIELD_KEY ? json[VALUE_TYPE_KEY] : null) ||
425
- schema.properties[VALUE_TYPE_KEY].default) : null;
429
+ return VALUE_TYPE_KEY in schema.properties
430
+ ? // valueType can be present but skipped
431
+ (json[VALUE_TYPE_KEY] !== IGNORE_FIELD_KEY ? json[VALUE_TYPE_KEY] : null) ||
432
+ schema.properties[VALUE_TYPE_KEY].default
433
+ : null;
426
434
  };
427
435
  var getPropertyType = function (def, key, valueType) {
428
436
  // default type is object (like $ref)
429
- return (key === 'value' && valueType ? valueType : (Array.isArray(def.type) ? (def.type.includes('null') ? 'null' : 'auto') : def.type)) || 'object';
437
+ return (key === 'value' && valueType
438
+ ? valueType
439
+ : Array.isArray(def.type)
440
+ ? def.type.includes('null')
441
+ ? 'null'
442
+ : 'auto'
443
+ : def.type) || 'object';
430
444
  };
431
445
  var setDefaultProperty = function (prop, def, node, ignoreInternal) {
432
446
  if (ignoreInternal === void 0) { ignoreInternal = false; }
433
- return prop !== VALUE_TYPE_KEY && (
434
- // only get default if internal or not present
435
- (!ignoreInternal && def.internal) || !node || !(prop in node));
447
+ return prop !== VALUE_TYPE_KEY &&
448
+ // only get default if internal or not present
449
+ ((!ignoreInternal && def.internal) || !node || !(prop in node));
436
450
  };
437
451
  var getDefaultProperties = function (schema, node, ignoreInternal) {
438
452
  if (ignoreInternal === void 0) { ignoreInternal = false; }
@@ -453,34 +467,31 @@ var filterProperties = function (data, excludedProps) {
453
467
  };
454
468
  var nodeType = function (id, type, existingNode) {
455
469
  if (existingNode === void 0) { existingNode = false; }
456
- return type
457
- ? (0, schema_1.isTypeNode)(type)
458
- ? existingNode
459
- ? { '@type': type }
460
- : { id: id, type: type }
461
- : { type: type }
462
- : {};
470
+ return type ? ((0, schema_1.isTypeNode)(type) ? (existingNode ? { '@type': type } : { id: id, type: type }) : { type: type }) : {};
463
471
  };
464
472
  var extendDataFromSchema = function (_a, schema, type, ignoreInternal, addDefaults) {
465
473
  var id = _a.id, _t = _a.type, data = __rest(_a, ["id", "type"]);
466
474
  if (ignoreInternal === void 0) { ignoreInternal = false; }
467
475
  if (addDefaults === void 0) { addDefaults = false; }
468
- return (0, utils_1.reduceUndefinedValues)(__assign(__assign(__assign({}, filterProperties(data, ignoreInternal ? [] : internalProperties(schema))), (addDefaults && !('@id' in data) && schema ?
469
- getDefaultProperties(schema, data, ignoreInternal).reduce(function (prev, _a) {
476
+ return (0, utils_1.reduceUndefinedValues)(__assign(__assign(__assign({}, filterProperties(data, ignoreInternal ? [] : internalProperties(schema))), (addDefaults && !('@id' in data) && schema
477
+ ? getDefaultProperties(schema, data, ignoreInternal).reduce(function (prev, _a) {
470
478
  var property = _a.property, defaultValue = _a.defaultValue;
471
479
  prev[property] = defaultValue;
472
480
  return prev;
473
- }, {}) :
474
- {})), nodeType(id, type, '@id' in data)), true);
481
+ }, {})
482
+ : {})), nodeType(id, type, '@id' in data)), true);
475
483
  };
476
484
  var validateArrayType = function (schema, def, key, value) {
477
- return typeof value !== 'object' || def.type !== 'array' || Array.isArray(value) || (0, exports.throwCSVError)({
478
- schema: schema.title,
479
- message: 'object-array-invalid',
480
- schemaKey: key,
481
- key: key,
482
- value: value
483
- });
485
+ return typeof value !== 'object' ||
486
+ def.type !== 'array' ||
487
+ Array.isArray(value) ||
488
+ (0, exports.throwCSVError)({
489
+ schema: schema.title,
490
+ message: 'object-array-invalid',
491
+ schemaKey: key,
492
+ key: key,
493
+ value: value
494
+ });
484
495
  };
485
496
  var mapContent = function (schemas, schema, ignoreInternal) { return function (json) {
486
497
  return Object.keys(json)
@@ -493,11 +504,14 @@ var mapContent = function (schemas, schema, ignoreInternal) { return function (j
493
504
  var propertyDefinition = getPropertyDefinition(schema, key, false, json);
494
505
  var valueType = getValueType(schema, json);
495
506
  var type = getPropertyType(propertyDefinition, key, valueType);
496
- // console.log(key, propertyDefinition, valueType, type, typeof value);
497
507
  validateArrayType(schema, propertyDefinition, key, value);
498
- var newValue = key === VALUE_TYPE_KEY ? '' : propertyInvalidFormat(schema, key, value, function () {
499
- return Array.isArray(type) ? value : propertyTypeToValue[type](value, schemas, propertyDefinition, ignoreInternal);
500
- });
508
+ var newValue = key === VALUE_TYPE_KEY
509
+ ? ''
510
+ : propertyInvalidFormat(schema, key, value, function () {
511
+ return Array.isArray(type)
512
+ ? value
513
+ : propertyTypeToValue[type](value, schemas, propertyDefinition, ignoreInternal);
514
+ });
501
515
  return __assign(__assign(__assign({}, prev), (schema.$id ? { type: schema.title } : {})), (type !== 'null' && isEmptyValue(newValue, schemas) ? {} : (_a = {}, _a[key] = newValue, _a)));
502
516
  }, {});
503
517
  }; };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hestia-earth/schema-convert",
3
- "version": "23.4.0",
3
+ "version": "23.5.0",
4
4
  "description": "Hestia Schema Converters",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",