@hestia-earth/schema-convert
Advanced tools
Comparing version 23.4.1 to 23.5.0
302
json.js
@@ -85,3 +85,3 @@ "use strict"; | ||
}; | ||
var _a, _b; | ||
var _a; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -154,3 +154,3 @@ exports.toJson = exports.filterEmptyNode = exports.formatNode = exports.cleanStringValue = exports.throwCSVError = exports.acceptedNodeTypes = void 0; | ||
var isEmptyValue = function (value, schemas) { | ||
return (typeof value) in isEmptyValueType ? isEmptyValueType[typeof value](value, schemas) : isEmptyCell(value); | ||
return typeof value in isEmptyValueType ? isEmptyValueType[typeof value](value, schemas) : isEmptyCell(value); | ||
}; | ||
@@ -173,6 +173,8 @@ var compileFullKey = function (key1, key2) { | ||
exports.throwCSVError = throwCSVError; | ||
var schemaNotFoundError = function (schema) { return (0, exports.throwCSVError)({ | ||
message: 'schema-not-found', | ||
schema: schema | ||
}); }; | ||
var schemaNotFoundError = function (schema) { | ||
return (0, exports.throwCSVError)({ | ||
message: 'schema-not-found', | ||
schema: schema | ||
}); | ||
}; | ||
var computeSuggestions = function (_a, key) { | ||
@@ -184,10 +186,12 @@ var properties = _a.properties; | ||
}; | ||
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) | ||
}); }; | ||
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) | ||
}); | ||
}; | ||
var propertyInvalidFormat = function (schema, key, value, func) { | ||
@@ -200,3 +204,4 @@ try { | ||
// throw already handled error or throw a generic invalid-format error | ||
return (0, exports.throwCSVError)(data ? __assign(__assign({}, data), { key: compileFullKey(key, data.key) }) : { | ||
return (0, exports.throwCSVError)(data | ||
? __assign(__assign({}, data), { key: compileFullKey(key, data.key) }) : { | ||
schema: schema.title, | ||
@@ -229,5 +234,5 @@ message: 'property-invalid-format', | ||
// 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) })) : | ||
(function () { | ||
return data | ||
? (0, exports.throwCSVError)(__assign(__assign({}, data), { key: compileFullKey("".concat(index), data.key) })) | ||
: (function () { | ||
throw err; | ||
@@ -237,7 +242,3 @@ })(); | ||
}; }; | ||
var allowedGeoJSONTypes = [ | ||
'FeatureCollection', | ||
'Feature', | ||
'GeometryCollection' | ||
]; | ||
var allowedGeoJSONTypes = ['FeatureCollection', 'Feature', 'GeometryCollection']; | ||
var geoJSONGeomtryTypeValidation = { | ||
@@ -281,7 +282,9 @@ Point: function () { return throwError('use "latitude" and "longitude" instead of "Point"'); }, | ||
type: 'FeatureCollection', | ||
features: [{ | ||
features: [ | ||
{ | ||
type: 'Feature', | ||
properties: {}, | ||
geometry: (0, wkt_1.wktToGeoJSON)(value) | ||
}] | ||
} | ||
] | ||
}; | ||
@@ -299,7 +302,9 @@ } | ||
type: 'FeatureCollection', | ||
features: [{ | ||
features: [ | ||
{ | ||
type: 'Feature', | ||
properties: {}, | ||
geometry: data | ||
}] | ||
} | ||
] | ||
} | ||
@@ -309,4 +314,6 @@ : ['Feature'].includes(data.type) | ||
type: 'FeatureCollection', | ||
features: [__assign(__assign({}, data), { properties: {} // make sure they are set or it is invalid | ||
})] | ||
features: [ | ||
__assign(__assign({}, data), { properties: {} // make sure they are set or it is invalid | ||
}) | ||
] | ||
} | ||
@@ -339,90 +346,90 @@ : data; | ||
}; | ||
var cleanStringValue = function (value) { return (value.match(/^[\d]+\.[\d]+\.[\d]+$/) === null ? value.replace(/\.0$/, '') : value).trim(); }; | ||
var cleanStringValue = function (value) { | ||
return (value.match(/^[\d]+\.[\d]+\.[\d]+$/) === null ? value.replace(/\.0$/, '') : value).trim(); | ||
}; | ||
exports.cleanStringValue = cleanStringValue; | ||
var geojsonRef = 'http://json.schemastore.org/geojson'; | ||
var propertyTypeToValue = (_b = { | ||
null: function (value) { | ||
var args = []; | ||
for (var _a = 1; _a < arguments.length; _a++) { | ||
args[_a - 1] = arguments[_a]; | ||
} | ||
return isEmptyCell(value) ? null : propertyTypeToValue.auto.apply(propertyTypeToValue, __spreadArray([value], __read(args), false)); | ||
}, | ||
string: function (value) { return (0, exports.cleanStringValue)(value || ''); }, | ||
number: function (value) { | ||
return isEmptyCell(value) | ||
? undefined | ||
: isNaN(+value) | ||
? (function () { | ||
throw new Error('failed to parse number'); | ||
})() | ||
: +value; | ||
}, | ||
integer: function (value) { return isEmptyCell(value) ? undefined : +value; }, | ||
boolean: function (value) { | ||
return isEmptyCell(value) | ||
? undefined | ||
: (0, utils_1.isBoolean)(value) | ||
? value.toLowerCase() === 'true' | ||
: (0, utils_1.isNumber)(value) | ||
? +value === 1 // handle setting 1 or 1.0 as true | ||
: (function () { | ||
throw new Error('failed to parse boolean, expected true or false'); | ||
})(); | ||
}, | ||
object: function (value, schemas, _a, _i) { | ||
var $ref = _a.$ref, required = _a.required; | ||
return $ref === geojsonRef | ||
? propertyTypeToValue[geojsonRef](value) | ||
: $ref | ||
? propertyTypeToValue.$ref(value, schemas, { $ref: $ref }, _i) | ||
: propertyTypeToValue.required(value, schemas, { required: required }, _i); | ||
}, | ||
array: function (value, schemas, _a, _i) { | ||
var items = _a.items; | ||
return (Array.isArray(value) ? value : value.split(arrayDelimiter())) | ||
.map(handleArrayError(function (val) { return items ? ('$ref' in items ? | ||
propertyTypeToValue.object(val, schemas, items, _i) : | ||
Array.isArray(items.type) ? | ||
propertyTypeToValue.auto(val, schemas, items, _i) : | ||
propertyTypeToValue[items.type](val, schemas, items, _i)) : val; })) | ||
.filter(function (val) { return !isEmptyValue(val, schemas); }); | ||
}, | ||
// try to determine the type automatically | ||
auto: function (value, schemas, _d, _i) { | ||
// iris are mapped as {@id: val} | ||
return (0, utils_1.isIri)(value) | ||
? propertyTypeToValue.required(value, schemas, { required: ['@id'] }, _i) | ||
: (0, utils_1.isBoolean)(value) | ||
? propertyTypeToValue.boolean(value) | ||
: (0, utils_1.isNumber)(value) | ||
? propertyTypeToValue.number(value) | ||
: value === 'null' | ||
? null | ||
: propertyTypeToValue.string(value); | ||
}, | ||
required: function (value, _schemas, _a) { | ||
var required = _a.required; | ||
var data = propertyRequiredValue(value, required); | ||
return (0, utils_1.isEmpty)(data) ? {} : data; | ||
}, | ||
$ref: function (value, schemas, _a, _i) { | ||
var $ref = _a.$ref; | ||
var schemaType = (0, schema_1.refToSchemaType)($ref); | ||
var schema = schemaType ? schemas[schemaType] : undefined; | ||
var data = schema | ||
? mapContent(schemas, schema, _i)(schemaRefValue(value, schemaType)) | ||
: $ref in propertyTypeToValue | ||
? propertyTypeToValue[$ref](value) | ||
: safeParseJSON(value); | ||
var includeDefaults = (0, schema_1.isBlankNode)(data) || schemaType === schema_1.SchemaType.Actor; // only blank nodes or actors allowed | ||
return (0, utils_1.isEmpty)(data) | ||
? {} | ||
: !!schema | ||
? extendDataFromSchema(data, schema, schemaType, _i, includeDefaults) | ||
: data; | ||
var propertyTypeToValue = { | ||
null: function (value) { | ||
var args = []; | ||
for (var _a = 1; _a < arguments.length; _a++) { | ||
args[_a - 1] = arguments[_a]; | ||
} | ||
return (isEmptyCell(value) ? null : propertyTypeToValue.auto.apply(propertyTypeToValue, __spreadArray([value], __read(args), false))); | ||
}, | ||
_b[geojsonRef] = function (value) { return isEmptyCell(value) ? undefined : parseGeoJSONValue(value); }, | ||
_b); | ||
string: function (value) { return (0, exports.cleanStringValue)(value || ''); }, | ||
number: function (value) { | ||
return isEmptyCell(value) | ||
? undefined | ||
: isNaN(+value) | ||
? (function () { | ||
throw new Error('failed to parse number'); | ||
})() | ||
: +value; | ||
}, | ||
integer: function (value) { return (isEmptyCell(value) ? undefined : +value); }, | ||
boolean: function (value) { | ||
return isEmptyCell(value) | ||
? undefined | ||
: (0, utils_1.isBoolean)(value) | ||
? value.toLowerCase() === 'true' | ||
: (0, utils_1.isNumber)(value) | ||
? +value === 1 // handle setting 1 or 1.0 as true | ||
: (function () { | ||
throw new Error('failed to parse boolean, expected true or false'); | ||
})(); | ||
}, | ||
object: function (value, schemas, _a, _i) { | ||
var $ref = _a.$ref, required = _a.required, geojson = _a.geojson; | ||
return geojson | ||
? propertyTypeToValue.geojson(value) | ||
: $ref | ||
? propertyTypeToValue.$ref(value, schemas, { $ref: $ref }, _i) | ||
: propertyTypeToValue.required(value, schemas, { required: required }, _i); | ||
}, | ||
array: function (value, schemas, _a, _i) { | ||
var items = _a.items; | ||
return (Array.isArray(value) ? value : value.split(arrayDelimiter())) | ||
.map(handleArrayError(function (val) { | ||
return items | ||
? '$ref' in items | ||
? propertyTypeToValue.object(val, schemas, items, _i) | ||
: Array.isArray(items.type) | ||
? propertyTypeToValue.auto(val, schemas, items, _i) | ||
: propertyTypeToValue[items.type](val, schemas, items, _i) | ||
: val; | ||
})) | ||
.filter(function (val) { return !isEmptyValue(val, schemas); }); | ||
}, | ||
// try to determine the type automatically | ||
auto: function (value, schemas, _d, _i) { | ||
// iris are mapped as {@id: val} | ||
return (0, utils_1.isIri)(value) | ||
? propertyTypeToValue.required(value, schemas, { required: ['@id'] }, _i) | ||
: (0, utils_1.isBoolean)(value) | ||
? propertyTypeToValue.boolean(value) | ||
: (0, utils_1.isNumber)(value) | ||
? propertyTypeToValue.number(value) | ||
: value === 'null' | ||
? null | ||
: propertyTypeToValue.string(value); | ||
}, | ||
required: function (value, _schemas, _a) { | ||
var required = _a.required; | ||
var data = propertyRequiredValue(value, required); | ||
return (0, utils_1.isEmpty)(data) ? {} : data; | ||
}, | ||
$ref: function (value, schemas, _a, _i) { | ||
var $ref = _a.$ref; | ||
var schemaType = (0, schema_1.refToSchemaType)($ref); | ||
var schema = schemaType ? schemas[schemaType] : undefined; | ||
var data = schema | ||
? mapContent(schemas, schema, _i)(schemaRefValue(value, schemaType)) | ||
: $ref in propertyTypeToValue | ||
? propertyTypeToValue[$ref](value) | ||
: safeParseJSON(value); | ||
var includeDefaults = (0, schema_1.isBlankNode)(data) || schemaType === schema_1.SchemaType.Actor; // only blank nodes or actors allowed | ||
return (0, utils_1.isEmpty)(data) ? {} : !!schema ? extendDataFromSchema(data, schema, schemaType, _i, includeDefaults) : data; | ||
}, | ||
geojson: function (value) { return (isEmptyCell(value) ? undefined : parseGeoJSONValue(value)); } | ||
}; | ||
var getPropertyDefinition = function (schema, key, ignoreErrors, value) { | ||
@@ -433,16 +440,23 @@ if (ignoreErrors === void 0) { ignoreErrors = false; } | ||
var getValueType = function (schema, json) { | ||
return VALUE_TYPE_KEY in schema.properties ? ( | ||
// valueType can be present but skipped | ||
(json[VALUE_TYPE_KEY] !== IGNORE_FIELD_KEY ? json[VALUE_TYPE_KEY] : null) || | ||
schema.properties[VALUE_TYPE_KEY].default) : null; | ||
return VALUE_TYPE_KEY in schema.properties | ||
? // valueType can be present but skipped | ||
(json[VALUE_TYPE_KEY] !== IGNORE_FIELD_KEY ? json[VALUE_TYPE_KEY] : null) || | ||
schema.properties[VALUE_TYPE_KEY].default | ||
: null; | ||
}; | ||
var getPropertyType = function (def, key, valueType) { | ||
// default type is object (like $ref) | ||
return (key === 'value' && valueType ? valueType : (Array.isArray(def.type) ? (def.type.includes('null') ? 'null' : 'auto') : def.type)) || 'object'; | ||
return (key === 'value' && valueType | ||
? valueType | ||
: Array.isArray(def.type) | ||
? def.type.includes('null') | ||
? 'null' | ||
: 'auto' | ||
: def.type) || 'object'; | ||
}; | ||
var setDefaultProperty = function (prop, def, node, ignoreInternal) { | ||
if (ignoreInternal === void 0) { ignoreInternal = false; } | ||
return prop !== VALUE_TYPE_KEY && ( | ||
// only get default if internal or not present | ||
(!ignoreInternal && def.internal) || !node || !(prop in node)); | ||
return prop !== VALUE_TYPE_KEY && | ||
// only get default if internal or not present | ||
((!ignoreInternal && def.internal) || !node || !(prop in node)); | ||
}; | ||
@@ -468,9 +482,3 @@ var getDefaultProperties = function (schema, node, ignoreInternal) { | ||
if (existingNode === void 0) { existingNode = false; } | ||
return type | ||
? (0, schema_1.isTypeNode)(type) | ||
? existingNode | ||
? { '@type': type } | ||
: { id: id, type: type } | ||
: { type: type } | ||
: {}; | ||
return type ? ((0, schema_1.isTypeNode)(type) ? (existingNode ? { '@type': type } : { id: id, type: type }) : { type: type }) : {}; | ||
}; | ||
@@ -481,18 +489,21 @@ var extendDataFromSchema = function (_a, schema, type, ignoreInternal, addDefaults) { | ||
if (addDefaults === void 0) { addDefaults = false; } | ||
return (0, utils_1.reduceUndefinedValues)(__assign(__assign(__assign({}, filterProperties(data, ignoreInternal ? [] : internalProperties(schema))), (addDefaults && !('@id' in data) && schema ? | ||
getDefaultProperties(schema, data, ignoreInternal).reduce(function (prev, _a) { | ||
return (0, utils_1.reduceUndefinedValues)(__assign(__assign(__assign({}, filterProperties(data, ignoreInternal ? [] : internalProperties(schema))), (addDefaults && !('@id' in data) && schema | ||
? getDefaultProperties(schema, data, ignoreInternal).reduce(function (prev, _a) { | ||
var property = _a.property, defaultValue = _a.defaultValue; | ||
prev[property] = defaultValue; | ||
return prev; | ||
}, {}) : | ||
{})), nodeType(id, type, '@id' in data)), true); | ||
}, {}) | ||
: {})), nodeType(id, type, '@id' in data)), true); | ||
}; | ||
var validateArrayType = function (schema, def, key, value) { | ||
return typeof value !== 'object' || def.type !== 'array' || Array.isArray(value) || (0, exports.throwCSVError)({ | ||
schema: schema.title, | ||
message: 'object-array-invalid', | ||
schemaKey: key, | ||
key: key, | ||
value: value | ||
}); | ||
return typeof value !== 'object' || | ||
def.type !== 'array' || | ||
Array.isArray(value) || | ||
(0, exports.throwCSVError)({ | ||
schema: schema.title, | ||
message: 'object-array-invalid', | ||
schemaKey: key, | ||
key: key, | ||
value: value | ||
}); | ||
}; | ||
@@ -510,7 +521,10 @@ var mapContent = function (schemas, schema, ignoreInternal) { return function (json) { | ||
var type = getPropertyType(propertyDefinition, key, valueType); | ||
// console.log(key, propertyDefinition, valueType, type, typeof value); | ||
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); | ||
}); | ||
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))); | ||
@@ -517,0 +531,0 @@ }, {}); |
{ | ||
"name": "@hestia-earth/schema-convert", | ||
"version": "23.4.1", | ||
"version": "23.5.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
1414
67408