@hestia-earth/schema-convert 9.2.0 → 9.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 +96 -18
  2. package/package.json +2 -1
package/json.js CHANGED
@@ -82,6 +82,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
82
82
  exports.toJson = exports.filterEmptyNode = exports.formatNode = exports.cleanStringValue = exports.throwCSVError = void 0;
83
83
  var csvtojson = require("csvtojson");
84
84
  var levenshtein = require("fast-levenshtein");
85
+ var wkt_1 = require("@terraformer/wkt");
85
86
  var schema_1 = require("@hestia-earth/schema");
86
87
  var types_1 = require("@hestia-earth/json-schema/types");
87
88
  var utils_1 = require("./utils");
@@ -121,13 +122,15 @@ var isEmptyValueType = (_a = {
121
122
  },
122
123
  _a.array = function (value) { return value.filter(function (v) { return !utils_1.isEmpty(v); }).length === 0; },
123
124
  _a.object = function (value, schemas) {
124
- return Array.isArray(value) ?
125
- isEmptyValueType.array(value) :
126
- (value['@type'] || value.type) === schema_1.SchemaType.Term ?
127
- isEmptyValueType[schema_1.SchemaType.Term](value) :
128
- schema_1.isBlankNode(value) ?
129
- isEmptyValueType.BlankNode(value, schemas) :
130
- Object.keys(value).length === 0;
125
+ return value === null
126
+ ? false
127
+ : Array.isArray(value)
128
+ ? isEmptyValueType.array(value)
129
+ : (value['@type'] || value.type) === schema_1.SchemaType.Term
130
+ ? isEmptyValueType[schema_1.SchemaType.Term](value)
131
+ : schema_1.isBlankNode(value)
132
+ ? isEmptyValueType.BlankNode(value, schemas)
133
+ : Object.keys(value).length === 0;
131
134
  },
132
135
  _a);
133
136
  var isEmptyValue = function (value, schemas) {
@@ -149,9 +152,10 @@ var propertyRequiredValue = function (value, required) {
149
152
  return nonEmptyCell(val) ? (_a = {}, _a[required[0]] = val, _a) : {};
150
153
  };
151
154
  var parseError = function (err) { return safeParseJSON(err.message); };
152
- exports.throwCSVError = function (error) {
153
- throw new Error(JSON.stringify(error));
155
+ var throwError = function (error) {
156
+ throw new Error(error);
154
157
  };
158
+ exports.throwCSVError = function (error) { return throwError(JSON.stringify(error)); };
155
159
  var schemaNotFoundError = function (schema) { return exports.throwCSVError({
156
160
  message: 'schema-not-found',
157
161
  schema: schema
@@ -203,6 +207,41 @@ var handleArrayError = function (func) { return function (value, index) {
203
207
  })();
204
208
  }
205
209
  }; };
210
+ var allowedGeoJSONTypes = [
211
+ 'FeatureCollection',
212
+ 'Feature',
213
+ 'GeometryCollection'
214
+ ];
215
+ var geoJSONGeomtryTypeValidation = {
216
+ Point: function () { return throwError('use "latitude" and "longitude" instead of "Point"'); },
217
+ MultiPoint: function () { return throwError('use a "Polygon" instead of "MultiPoint"'); },
218
+ LineString: function () { return throwError('use a "Polygon" instead of "LineString"'); },
219
+ MultiLineString: function () { return throwError('use a "MultiPolygon" instead of "MultiLineString"'); }
220
+ };
221
+ var validateGeoJSONGeometryType = function (_a) {
222
+ var type = _a.type;
223
+ return type in geoJSONGeomtryTypeValidation ? geoJSONGeomtryTypeValidation[type]() : true;
224
+ };
225
+ /**
226
+ * Validate the GeoJSON format provided.
227
+ * GeoJSON is only relevant if it includes a `Polygon` or `MultiPolygon`, which would represent an area.
228
+ * If a single `Point` is provided, `latitude` and `longitude` should be used instead.
229
+ *
230
+ * @param value The GeoJSON as object.
231
+ */
232
+ var validateGeoJSONFormat = function (value) {
233
+ return (!allowedGeoJSONTypes.includes(value.type)
234
+ ? throwError("Invalid GeoJSON \"type\". Allowed values are: " + allowedGeoJSONTypes.join(', '))
235
+ : 'geometries' in value
236
+ ? (value.geometries || []).every(validateGeoJSONGeometryType)
237
+ : 'geometry' in value
238
+ ? validateGeoJSONGeometryType(value.geometry)
239
+ : 'features' in value
240
+ ? (value.features || []).every(validateGeoJSONFormat)
241
+ : true)
242
+ ? value
243
+ : null;
244
+ };
206
245
  /**
207
246
  * If the user provided a non-object where an object was expected, assume it was meant to be the `name` property.
208
247
  * For non-Terms, we assume it is the `id`.
@@ -223,11 +262,13 @@ var propertyTypeToValue = {
223
262
  },
224
263
  string: function (value) { return exports.cleanStringValue(value || ''); },
225
264
  number: function (value) {
226
- return isEmptyCell(value) ?
227
- undefined :
228
- isNaN(+value) ? (function () {
229
- throw new Error('failed to parse number');
230
- })() : +value;
265
+ return isEmptyCell(value)
266
+ ? undefined
267
+ : isNaN(+value)
268
+ ? (function () {
269
+ throw new Error('failed to parse number');
270
+ })()
271
+ : +value;
231
272
  },
232
273
  integer: function (value) { return isEmptyCell(value) ? undefined : +value; },
233
274
  boolean: function (value) {
@@ -260,8 +301,15 @@ var propertyTypeToValue = {
260
301
  // try to determine the type automatically
261
302
  auto: function (value, schemas, _d, _i) {
262
303
  // iris are mapped as {@id: val}
263
- return utils_1.isIri(value) ? propertyTypeToValue.required(value, schemas, { required: ['@id'] }, _i) : (utils_1.isBoolean(value) ? propertyTypeToValue.boolean(value) : (utils_1.isNumber(value) ? propertyTypeToValue.number(value) :
264
- propertyTypeToValue.string(value)));
304
+ return utils_1.isIri(value)
305
+ ? propertyTypeToValue.required(value, schemas, { required: ['@id'] }, _i)
306
+ : utils_1.isBoolean(value)
307
+ ? propertyTypeToValue.boolean(value)
308
+ : utils_1.isNumber(value)
309
+ ? propertyTypeToValue.number(value)
310
+ : value === 'null'
311
+ ? null
312
+ : propertyTypeToValue.string(value);
265
313
  },
266
314
  required: function (value, _schemas, _a) {
267
315
  var required = _a.required;
@@ -272,9 +320,39 @@ var propertyTypeToValue = {
272
320
  var $ref = _a.$ref;
273
321
  var schemaType = schema_1.refToSchemaType($ref);
274
322
  var schema = schemaType ? schemas[schemaType] : undefined;
275
- var data = schema ? mapContent(schemas, schema, _i)(schemaRefValue(value, schemaType)) : safeParseJSON(value);
323
+ var data = schema
324
+ ? mapContent(schemas, schema, _i)(schemaRefValue(value, schemaType))
325
+ : $ref in propertyTypeToValue
326
+ ? propertyTypeToValue[$ref](value)
327
+ : safeParseJSON(value);
276
328
  var includeDefaults = [schema_1.SchemaType.Actor].includes(schemaType); // only nested node allowed
277
- return utils_1.isEmpty(data) ? {} : (schema ? extendDataFromSchema(data, schema, schemaType, _i, includeDefaults) : data);
329
+ return utils_1.isEmpty(data)
330
+ ? {}
331
+ : schema
332
+ ? extendDataFromSchema(data, schema, schemaType, _i, includeDefaults)
333
+ : data;
334
+ },
335
+ 'http://json.schemastore.org/geojson': function (value) {
336
+ var result;
337
+ try {
338
+ result = {
339
+ type: 'Feature',
340
+ geometry: wkt_1.wktToGeoJSON(value)
341
+ };
342
+ }
343
+ catch (e1) {
344
+ try {
345
+ var data = JSON.parse(value);
346
+ return result = ['Polygon', 'MultiPolygon'].includes(data.type)
347
+ ? {
348
+ type: 'Feature',
349
+ geometry: data
350
+ }
351
+ : data;
352
+ }
353
+ catch (e2) { }
354
+ }
355
+ return result ? validateGeoJSONFormat(result) : value;
278
356
  }
279
357
  };
280
358
  var getPropertyDefinition = function (schema, key, ignoreErrors, value) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hestia-earth/schema-convert",
3
- "version": "9.2.0",
3
+ "version": "9.5.0",
4
4
  "description": "Hestia Schema Converters",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
@@ -28,6 +28,7 @@
28
28
  "dependencies": {
29
29
  "@hestia-earth/json-schema": "*",
30
30
  "@hestia-earth/schema": "*",
31
+ "@terraformer/wkt": "^2.1.1",
31
32
  "csvtojson": "^2.0.0",
32
33
  "fast-levenshtein": "^3.0.0",
33
34
  "json-2-csv": "^3.14.0",