@intelligo.ai/object-to-schema
Advanced tools
Comparing version 1.4.0 to 1.4.2
@@ -8,20 +8,14 @@ "use strict"; | ||
var generalUtil_1 = require("./utils/generalUtil"); | ||
var transform_1 = require("./utils/transform"); | ||
function mapObjectToSchema(obj, schema) { | ||
if (schema === void 0) { schema = {}; } | ||
return obj | ||
? Object | ||
.keys(obj) | ||
.reduce(function (schema, key) { return resolveOperation(schema, obj, key); }, schema) | ||
: {}; | ||
if (!obj) { | ||
return {}; | ||
} | ||
return Object | ||
.keys(obj) | ||
.reduce(function (schema, key) { return resolveOperation(schema, obj, key); }, schema); | ||
} | ||
exports.mapObjectToSchema = mapObjectToSchema; | ||
function handleEmptyValue(schema, key) { | ||
var value = schema[key]; | ||
if (value == null || value == 'unknown') { | ||
return unknownType(schema, key); | ||
} | ||
return schema; | ||
} | ||
function resolveOperation(schema, obj, key) { | ||
var _a, _b; | ||
var nestedValue = obj[key]; | ||
@@ -31,26 +25,60 @@ var archType = generalUtil_1.resolveValueArchtype(nestedValue); | ||
case 'EMPTY': | ||
return handleEmptyValue(schema, key); | ||
return transform_1.assignSchema(schema, key, handleEmptyValue(schema, key)); | ||
case 'SIMPLE': | ||
var type = generalUtil_1.resolveType(nestedValue); | ||
return simpleType(type, key, schema, nestedValue); | ||
return transform_1.assignSchema(schema, key, simpleType(type, nestedValue)); | ||
case 'ARRAY': | ||
return ramda_1.default.mergeDeepLeft(schema, (_a = {}, _a[key] = [arrayOfObjectsToSchema(nestedValue)], _a)); | ||
var schemaValue = schema[key]; | ||
return transform_1.assignSchema(schema, key, handleArrayValues(nestedValue, schemaValue)); | ||
case 'OBJECT': | ||
return ramda_1.default.mergeDeepLeft(schema, (_b = {}, _b[key] = mapObjectToSchema(nestedValue), _b)); | ||
return transform_1.assignSchema(schema, key, handleObjectValues(nestedValue, schema, key)); | ||
} | ||
} | ||
function arrayOfObjectsToSchema(array, schema) { | ||
if (schema === void 0) { schema = {}; } | ||
return array.reduce(function (acc, obj) { return generalUtil_1.resolveValueArchtype(obj) == 'OBJECT' | ||
? mapObjectToSchema(obj, acc) | ||
: JSON.stringify({ type: generalUtil_1.resolveType(obj).toLowerCase(), example: obj }); }, schema); | ||
function handleEmptyValue(schema, key) { | ||
var value = schema[key]; | ||
return value == null || value.includes('unknown') | ||
? unknownType() | ||
: schema; | ||
} | ||
function simpleType(type, key, schema, value) { | ||
var _a; | ||
return ramda_1.default.mergeDeepLeft(schema, (_a = {}, _a[key] = JSON.stringify({ type: type.toLowerCase(), example: value }), _a)); | ||
function handleObjectValues(nestedValue, schema, key) { | ||
if (schema[key]) { | ||
var mergedSchema = mapObjectToSchema(nestedValue, schema[key]); | ||
return mergedSchema; | ||
} | ||
else { | ||
return mapObjectToSchema(nestedValue); | ||
} | ||
} | ||
function unknownType(schema, key) { | ||
var _a; | ||
return ramda_1.default.mergeDeepLeft(schema, (_a = {}, _a[key] = JSON.stringify({ type: 'unknown' }), _a)); | ||
function handleArrayValues(nestedValue, schemaValue) { | ||
if (generalUtil_1.resolveValueArchtype(nestedValue[0]) == 'OBJECT') { | ||
var arraySchema = nestedValue.reduce(function (accSchema, obj) { return mapObjectToSchema(obj, accSchema); }, schemaValue ? schemaValue[0] : {}); | ||
return [arraySchema]; | ||
} | ||
else { | ||
var type = generalUtil_1.resolveType(nestedValue); | ||
return [ | ||
JSON.stringify({ | ||
type: type.toLowerCase(), | ||
example: generateExample(nestedValue[0]) | ||
}) | ||
]; | ||
} | ||
} | ||
function simpleType(type, value) { | ||
return JSON.stringify({ type: type.toLowerCase(), example: generateExample(value) }); | ||
} | ||
function unknownType() { | ||
return JSON.stringify({ type: 'unknown' }); | ||
} | ||
function generateExample(value) { | ||
if (typeof value == 'string') { | ||
return ramda_1.default.tryCatch(function () { | ||
var json = JSON.parse(value); | ||
return json.example && json.type ? json.example : value; | ||
}, function () { return value; })(); | ||
} | ||
else { | ||
return value; | ||
} | ||
} | ||
//# sourceMappingURL=schemaCreator.js.map |
import { Transform, SomeObj, TreeLeaf } from './types'; | ||
export declare function generateTransform(transforms: Transform[]): Iterator<Transform>; | ||
export declare function assignSchema(schema: SomeObj, key: string, value: any): SomeObj & { | ||
[x: string]: any; | ||
}; | ||
export declare function treeLeafsToTransforms(treeLeafs: TreeLeaf[]): Transform[]; | ||
@@ -4,0 +7,0 @@ export declare function groupTransformsByTarget(transforms: Transform[]): SomeObj; |
@@ -56,2 +56,7 @@ "use strict"; | ||
exports.generateTransform = generateTransform; | ||
function assignSchema(schema, key, value) { | ||
var _a; | ||
return Object.assign(schema, (_a = {}, _a[key] = value, _a)); | ||
} | ||
exports.assignSchema = assignSchema; | ||
function treeLeafsToTransforms(treeLeafs) { | ||
@@ -58,0 +63,0 @@ var transforms = treeLeafs.flatMap(function (_a) { |
{ | ||
"name": "@intelligo.ai/object-to-schema", | ||
"version": "1.4.0", | ||
"version": "1.4.2", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
319091
6226