@hestia-earth/schema-convert
Advanced tools
Comparing version 25.0.1 to 25.1.0
@@ -27,3 +27,13 @@ import { NodeType, SchemaType } from '@hestia-earth/schema'; | ||
} | ||
export declare const throwCSVError: <T extends ICSVError>(error: T) => never; | ||
export interface ICSVErrors { | ||
errors: ICSVError[]; | ||
} | ||
export declare enum ErrorKeys { | ||
SchemaNotFound = "schema-not-found", | ||
PropertyNotFound = "property-not-found", | ||
PropertyInvalidFormat = "property-invalid-format", | ||
DuplicatedIdFields = "duplicated-id-fields", | ||
ObjectArrayInvalid = "object-array-invalid" | ||
} | ||
export declare const throwCSVErrors: <T extends ICSVError>(errors: T[]) => never; | ||
export declare const cleanStringValue: (value: string) => string; | ||
@@ -30,0 +40,0 @@ export declare const formatNode: (schemas: definitions, type: SchemaType, data: ICSVContent, ignoreInternal: boolean, top?: boolean) => any; |
165
json.js
@@ -87,3 +87,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.toJson = exports.filterEmptyNode = exports.formatNode = exports.cleanStringValue = exports.throwCSVError = exports.acceptedNodeTypes = void 0; | ||
exports.toJson = exports.filterEmptyNode = exports.formatNode = exports.cleanStringValue = exports.throwCSVErrors = exports.ErrorKeys = exports.acceptedNodeTypes = void 0; | ||
var csvtojson = require("csvtojson"); | ||
@@ -97,2 +97,10 @@ var levenshtein = require("fast-levenshtein"); | ||
exports.acceptedNodeTypes = Object.values(schema_1.NodeType); | ||
var ErrorKeys; | ||
(function (ErrorKeys) { | ||
ErrorKeys["SchemaNotFound"] = "schema-not-found"; | ||
ErrorKeys["PropertyNotFound"] = "property-not-found"; | ||
ErrorKeys["PropertyInvalidFormat"] = "property-invalid-format"; | ||
ErrorKeys["DuplicatedIdFields"] = "duplicated-id-fields"; | ||
ErrorKeys["ObjectArrayInvalid"] = "object-array-invalid"; | ||
})(ErrorKeys = exports.ErrorKeys || (exports.ErrorKeys = {})); | ||
var IGNORE_FIELD_KEY = '-'; | ||
@@ -167,13 +175,15 @@ var VALUE_TYPE_KEY = 'valueType'; | ||
}; | ||
var parseError = function (err) { return safeParseJSON(err.message); }; | ||
var parseErrors = function (err) { var _a; return ((_a = safeParseJSON(err.message)) === null || _a === void 0 ? void 0 : _a.errors) || []; }; | ||
var throwError = function (error) { | ||
throw new Error(error); | ||
}; | ||
var throwCSVError = function (error) { return throwError(JSON.stringify(error)); }; | ||
exports.throwCSVError = throwCSVError; | ||
var throwCSVErrors = function (errors) { return throwError(JSON.stringify({ errors: errors })); }; | ||
exports.throwCSVErrors = throwCSVErrors; | ||
var schemaNotFoundError = function (schema) { | ||
return (0, exports.throwCSVError)({ | ||
message: 'schema-not-found', | ||
schema: schema | ||
}); | ||
return (0, exports.throwCSVErrors)([ | ||
{ | ||
message: ErrorKeys.SchemaNotFound, | ||
schema: schema | ||
} | ||
]); | ||
}; | ||
@@ -189,10 +199,12 @@ var computeSuggestions = function (_a, key) { | ||
var propertyNotFoundError = function (schema, key, value) { | ||
return (0, exports.throwCSVError)({ | ||
schema: schema.title, | ||
message: 'property-not-found', | ||
schemaKey: key, | ||
key: key, | ||
value: value, | ||
suggestions: computeSuggestions(schema, key) | ||
}); | ||
return (0, exports.throwCSVErrors)([ | ||
{ | ||
schema: schema.title, | ||
message: ErrorKeys.PropertyNotFound, | ||
schemaKey: key, | ||
key: key, | ||
value: value, | ||
suggestions: computeSuggestions(schema, key) | ||
} | ||
]); | ||
}; | ||
@@ -204,13 +216,16 @@ var propertyInvalidFormat = function (schema, key, value, func) { | ||
catch (err) { | ||
var data = parseError(err); | ||
var errors = parseErrors(err); | ||
// throw already handled error or throw a generic invalid-format error | ||
return (0, exports.throwCSVError)(data | ||
? __assign(__assign({}, data), { key: compileFullKey(key, data.key) }) : { | ||
schema: schema.title, | ||
message: 'property-invalid-format', | ||
schemaKey: key, | ||
key: key, | ||
value: value, | ||
error: err === null || err === void 0 ? void 0 : err.message | ||
}); | ||
return (0, exports.throwCSVErrors)(errors.length | ||
? errors.map(function (data) { return (__assign(__assign({}, data), { key: compileFullKey(key, data.key) })); }) | ||
: [ | ||
{ | ||
schema: schema.title, | ||
message: ErrorKeys.PropertyInvalidFormat, | ||
schemaKey: key, | ||
key: key, | ||
value: value, | ||
error: err === null || err === void 0 ? void 0 : err.message | ||
} | ||
]); | ||
} | ||
@@ -220,9 +235,11 @@ }; | ||
return typeof value === 'object' && [value.id, value['@id']].every(function (v) { return !!v && nonEmptyCell(v); }) | ||
? (0, exports.throwCSVError)({ | ||
schema: schema.title, | ||
message: 'duplicated-id-fields', | ||
schemaKey: key, | ||
key: key, | ||
value: value | ||
}) | ||
? (0, exports.throwCSVErrors)([ | ||
{ | ||
schema: schema.title, | ||
message: ErrorKeys.DuplicatedIdFields, | ||
schemaKey: key, | ||
key: key, | ||
value: value | ||
} | ||
]) | ||
: null; | ||
@@ -235,6 +252,6 @@ }; | ||
catch (err) { | ||
var data = parseError(err); | ||
var errors = parseErrors(err); | ||
// throw already handled error or throw a generic invalid-format error | ||
return data | ||
? (0, exports.throwCSVError)(__assign(__assign({}, data), { key: compileFullKey("".concat(index), data.key) })) | ||
return errors.length | ||
? (0, exports.throwCSVErrors)(errors.map(function (data) { return (__assign(__assign({}, data), { key: compileFullKey("".concat(index), data.key) })); })) | ||
: (function () { | ||
@@ -395,3 +412,5 @@ throw err; | ||
: Array.isArray(items.type) | ||
? propertyTypeToValue.auto(val, schemas, items, _i) | ||
? val === IGNORE_FIELD_KEY | ||
? null | ||
: propertyTypeToValue.auto(val, schemas, items, _i) | ||
: propertyTypeToValue[items.type](val, schemas, items, _i) | ||
@@ -498,31 +517,51 @@ : val; | ||
Array.isArray(value) || | ||
(0, exports.throwCSVError)({ | ||
schema: schema.title, | ||
message: 'object-array-invalid', | ||
schemaKey: key, | ||
key: key, | ||
value: value | ||
}); | ||
(0, exports.throwCSVErrors)([ | ||
{ | ||
schema: schema.title, | ||
message: ErrorKeys.ObjectArrayInvalid, | ||
schemaKey: key, | ||
key: key, | ||
value: value | ||
} | ||
]); | ||
}; | ||
var mapContent = function (schemas, schema, ignoreInternal) { return function (json) { | ||
return Object.keys(json) | ||
var values = Object.keys(json) | ||
.filter(nonEmptyCell) | ||
.reduce(function (prev, key) { | ||
var _a; | ||
var value = json[key]; | ||
// make sure we are not using both `id` and `@id` fields | ||
validateDuplicatedIdFields(schema, key, value); | ||
var propertyDefinition = getPropertyDefinition(schema, key, false, json); | ||
var valueType = getValueType(schema, json); | ||
var type = getPropertyType(propertyDefinition, key, valueType); | ||
validateArrayType(schema, propertyDefinition, key, value); | ||
var newValue = key === VALUE_TYPE_KEY | ||
? '' | ||
: propertyInvalidFormat(schema, key, value, function () { | ||
return Array.isArray(type) | ||
? value | ||
: propertyTypeToValue[type](value, schemas, propertyDefinition, ignoreInternal); | ||
}); | ||
return __assign(__assign(__assign({}, prev), (schema.$id ? { type: schema.title } : {})), (type !== 'null' && isEmptyValue(newValue, schemas) ? {} : (_a = {}, _a[key] = newValue, _a))); | ||
}, {}); | ||
.map(function (key) { | ||
try { | ||
var value_1 = json[key]; | ||
// make sure we are not using both `id` and `@id` fields | ||
validateDuplicatedIdFields(schema, key, value_1); | ||
var propertyDefinition_1 = getPropertyDefinition(schema, key, false, json); | ||
var valueType = getValueType(schema, json); | ||
var type_1 = getPropertyType(propertyDefinition_1, key, valueType); | ||
validateArrayType(schema, propertyDefinition_1, key, value_1); | ||
var newValue = key === VALUE_TYPE_KEY | ||
? '' | ||
: propertyInvalidFormat(schema, key, value_1, function () { | ||
return Array.isArray(type_1) | ||
? value_1 | ||
: propertyTypeToValue[type_1](value_1, schemas, propertyDefinition_1, ignoreInternal); | ||
}); | ||
return { errors: [], value: type_1 !== 'null' && isEmptyValue(newValue, schemas) ? undefined : [key, newValue] }; | ||
} | ||
catch (err) { | ||
var errors_1 = JSON.parse(err.message).errors; | ||
return { errors: errors_1 }; | ||
} | ||
}); | ||
var errors = values.filter(function (_a) { | ||
var errors = _a.errors; | ||
return (errors === null || errors === void 0 ? void 0 : errors.length) > 0; | ||
}).flatMap(function (_a) { | ||
var errors = _a.errors; | ||
return errors; | ||
}); | ||
return (errors === null || errors === void 0 ? void 0 : errors.length) | ||
? throwError(JSON.stringify({ errors: errors })) | ||
: Object.fromEntries(__spreadArray([schema.$id ? ['type', schema.title] : null], __read(values.map(function (_a) { | ||
var value = _a.value; | ||
return value; | ||
})), false).filter(Boolean)); | ||
}; }; | ||
@@ -529,0 +568,0 @@ var formatNode = function (schemas, type, data, ignoreInternal, top) { |
{ | ||
"name": "@hestia-earth/schema-convert", | ||
"version": "25.0.1", | ||
"version": "25.1.0", | ||
"description": "Hestia Schema Converters", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
71359
1531