@hestia-earth/schema-convert 9.4.0 → 9.7.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 +71 -4
  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");
@@ -151,9 +152,10 @@ var propertyRequiredValue = function (value, required) {
151
152
  return nonEmptyCell(val) ? (_a = {}, _a[required[0]] = val, _a) : {};
152
153
  };
153
154
  var parseError = function (err) { return safeParseJSON(err.message); };
154
- exports.throwCSVError = function (error) {
155
- throw new Error(JSON.stringify(error));
155
+ var throwError = function (error) {
156
+ throw new Error(error);
156
157
  };
158
+ exports.throwCSVError = function (error) { return throwError(JSON.stringify(error)); };
157
159
  var schemaNotFoundError = function (schema) { return exports.throwCSVError({
158
160
  message: 'schema-not-found',
159
161
  schema: schema
@@ -205,6 +207,41 @@ var handleArrayError = function (func) { return function (value, index) {
205
207
  })();
206
208
  }
207
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
+ };
208
245
  /**
209
246
  * If the user provided a non-object where an object was expected, assume it was meant to be the `name` property.
210
247
  * For non-Terms, we assume it is the `id`.
@@ -283,9 +320,39 @@ var propertyTypeToValue = {
283
320
  var $ref = _a.$ref;
284
321
  var schemaType = schema_1.refToSchemaType($ref);
285
322
  var schema = schemaType ? schemas[schemaType] : undefined;
286
- 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);
287
328
  var includeDefaults = [schema_1.SchemaType.Actor].includes(schemaType); // only nested node allowed
288
- 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;
289
356
  }
290
357
  };
291
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.4.0",
3
+ "version": "9.7.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",