@hestia-earth/schema-convert 7.10.0 → 8.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 (4) hide show
  1. package/csv.js +5 -1
  2. package/json.d.ts +1 -0
  3. package/json.js +13 -5
  4. package/package.json +2 -1
package/csv.js CHANGED
@@ -50,7 +50,11 @@ var isIgnored = function (val) { return Object.values(ignoreValues).some(functio
50
50
  exports.headersFromCsv = function (csv) { return csv.split('\n')[0].split(exports.csvColSep); };
51
51
  var convertValue = {
52
52
  default: function (value) { return "" + value; },
53
- object: function (value) { return value instanceof Date ? value.toJSON() : JSON.stringify(value); }
53
+ object: function (value) { return value === null
54
+ ? noValue
55
+ : value instanceof Date
56
+ ? value.toJSON()
57
+ : JSON.stringify(value); }
54
58
  };
55
59
  var escapeField = function (value) {
56
60
  var convertType = (typeof value) in convertValue ? typeof value : 'default';
package/json.d.ts CHANGED
@@ -21,6 +21,7 @@ export interface ICSVError {
21
21
  value?: any;
22
22
  error?: string;
23
23
  headers?: ICSVHeader[];
24
+ suggestions?: string[];
24
25
  }
25
26
  export declare const throwCSVError: <T extends ICSVError>(error: T) => never;
26
27
  export declare const cleanStringValue: (value: string) => string;
package/json.js CHANGED
@@ -81,6 +81,7 @@ var _a;
81
81
  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
+ var levenshtein = require("fast-levenshtein");
84
85
  var schema_1 = require("@hestia-earth/schema");
85
86
  var types_1 = require("@hestia-earth/json-schema/types");
86
87
  var utils_1 = require("./utils");
@@ -155,12 +156,18 @@ var schemaNotFoundError = function (schema) { return exports.throwCSVError({
155
156
  message: 'schema-not-found',
156
157
  schema: schema
157
158
  }); };
159
+ var computeSuggestions = function (_a, key) {
160
+ var properties = _a.properties;
161
+ var allKeys = Object.keys(properties);
162
+ return allKeys.filter(function (v) { return levenshtein.get(v, key) <= 3; });
163
+ };
158
164
  var propertyNotFoundError = function (schema, key, value) { return exports.throwCSVError({
159
165
  schema: schema.title,
160
166
  message: 'property-not-found',
161
167
  schemaKey: key,
162
168
  key: key,
163
- value: value
169
+ value: value,
170
+ suggestions: computeSuggestions(schema, key)
164
171
  }); };
165
172
  var propertyInvalidFormat = function (schema, key, value, func) {
166
173
  try {
@@ -261,7 +268,8 @@ var propertyTypeToValue = {
261
268
  var schemaType = schema_1.refToSchemaType($ref);
262
269
  var schema = schemaType ? schemas[schemaType] : undefined;
263
270
  var data = schema ? mapContent(schemas, schema, _i)(schemaRefValue(value, schemaType)) : safeParseJSON(value);
264
- return utils_1.isEmpty(data) ? {} : (schema ? extendDataFromSchema(data, schema, schemaType, _i) : data);
271
+ var includeDefaults = [schema_1.SchemaType.Actor].includes(schemaType); // only nested node allowed
272
+ return utils_1.isEmpty(data) ? {} : (schema ? extendDataFromSchema(data, schema, schemaType, _i, includeDefaults) : data);
265
273
  }
266
274
  };
267
275
  var getPropertyDefinition = function (schema, key, ignoreErrors, value) {
@@ -305,11 +313,11 @@ var nodeType = function (id, type, existingNode) {
305
313
  if (existingNode === void 0) { existingNode = false; }
306
314
  return type ? (schema_1.isTypeNode(type) ? (existingNode ? { '@type': type } : { id: id, type: type }) : { type: type }) : {};
307
315
  };
308
- var extendDataFromSchema = function (_a, schema, type, ignoreInternal, top) {
316
+ var extendDataFromSchema = function (_a, schema, type, ignoreInternal, addDefaults) {
309
317
  if (ignoreInternal === void 0) { ignoreInternal = false; }
310
- if (top === void 0) { top = false; }
318
+ if (addDefaults === void 0) { addDefaults = false; }
311
319
  var id = _a.id, _t = _a.type, data = __rest(_a, ["id", "type"]);
312
- return utils_1.reduceUndefinedValues(__assign(__assign(__assign({}, filterProperties(data, ignoreInternal ? [] : internalProperties(schema))), (top && schema ?
320
+ return utils_1.reduceUndefinedValues(__assign(__assign(__assign({}, filterProperties(data, ignoreInternal ? [] : internalProperties(schema))), (addDefaults && !('@id' in data) && schema ?
313
321
  getDefaultProperties(schema, data, ignoreInternal).reduce(function (prev, _a) {
314
322
  var _b;
315
323
  var property = _a.property, defaultValue = _a.defaultValue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hestia-earth/schema-convert",
3
- "version": "7.10.0",
3
+ "version": "8.1.0",
4
4
  "description": "Hestia Schema Converters",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
@@ -29,6 +29,7 @@
29
29
  "@hestia-earth/json-schema": "*",
30
30
  "@hestia-earth/schema": "*",
31
31
  "csvtojson": "^2.0.0",
32
+ "fast-levenshtein": "^3.0.0",
32
33
  "json-2-csv": "^3.14.0",
33
34
  "lodash.get": "^4.0.0",
34
35
  "lodash.unset": "^4.0.0"