@hestia-earth/schema-convert 7.11.0 → 8.2.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 +25 -12
  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,21 @@ 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 internal = internalProperties({ properties: properties });
162
+ var allKeys = Object.keys(properties).filter(function (k) { return !__spread([
163
+ '@type', 'type'
164
+ ], internal).includes(k); });
165
+ return allKeys.filter(function (v) { return levenshtein.get(v, key) <= 3; });
166
+ };
158
167
  var propertyNotFoundError = function (schema, key, value) { return exports.throwCSVError({
159
168
  schema: schema.title,
160
169
  message: 'property-not-found',
161
170
  schemaKey: key,
162
171
  key: key,
163
- value: value
172
+ value: value,
173
+ suggestions: computeSuggestions(schema, key)
164
174
  }); };
165
175
  var propertyInvalidFormat = function (schema, key, value, func) {
166
176
  try {
@@ -221,13 +231,15 @@ var propertyTypeToValue = {
221
231
  },
222
232
  integer: function (value) { return isEmptyCell(value) ? undefined : +value; },
223
233
  boolean: function (value) {
224
- return isEmptyCell(value) ?
225
- undefined :
226
- utils_1.isBoolean(value) ?
227
- value.toLowerCase() === 'true' :
228
- (function () {
229
- throw new Error('failed to parse boolean, expected true or false');
230
- })();
234
+ return isEmptyCell(value)
235
+ ? undefined
236
+ : utils_1.isBoolean(value)
237
+ ? value.toLowerCase() === 'true'
238
+ : utils_1.isNumber(value)
239
+ ? +value === 1 // handle setting 1 or 1.0 as true
240
+ : (function () {
241
+ throw new Error('failed to parse boolean, expected true or false');
242
+ })();
231
243
  },
232
244
  object: function (value, schemas, _a, _i) {
233
245
  var $ref = _a.$ref, required = _a.required;
@@ -261,7 +273,8 @@ var propertyTypeToValue = {
261
273
  var schemaType = schema_1.refToSchemaType($ref);
262
274
  var schema = schemaType ? schemas[schemaType] : undefined;
263
275
  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);
276
+ 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);
265
278
  }
266
279
  };
267
280
  var getPropertyDefinition = function (schema, key, ignoreErrors, value) {
@@ -305,11 +318,11 @@ var nodeType = function (id, type, existingNode) {
305
318
  if (existingNode === void 0) { existingNode = false; }
306
319
  return type ? (schema_1.isTypeNode(type) ? (existingNode ? { '@type': type } : { id: id, type: type }) : { type: type }) : {};
307
320
  };
308
- var extendDataFromSchema = function (_a, schema, type, ignoreInternal, top) {
321
+ var extendDataFromSchema = function (_a, schema, type, ignoreInternal, addDefaults) {
309
322
  if (ignoreInternal === void 0) { ignoreInternal = false; }
310
- if (top === void 0) { top = false; }
323
+ if (addDefaults === void 0) { addDefaults = false; }
311
324
  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 ?
325
+ return utils_1.reduceUndefinedValues(__assign(__assign(__assign({}, filterProperties(data, ignoreInternal ? [] : internalProperties(schema))), (addDefaults && !('@id' in data) && schema ?
313
326
  getDefaultProperties(schema, data, ignoreInternal).reduce(function (prev, _a) {
314
327
  var _b;
315
328
  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.11.0",
3
+ "version": "8.2.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"