guardz
Version:
A simple and lightweight TypeScript type guard library for runtime type validation.
38 lines (37 loc) • 1.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSimplifiedTree = void 0;
/**
* Create a simplified flat tree structure for JSON output
* @param tree - The validation tree to simplify
* @returns A simplified tree structure as a plain object
*/
const createSimplifiedTree = (tree) => {
const rootKey = tree.path.split('.').pop() || 'root';
const result = {};
if (tree.children && Object.keys(tree.children).length) {
// Object with properties
const value = {};
Object.entries(tree.children).forEach(([key, childTree]) => {
value[key] = {
valid: childTree.valid,
value: childTree.actualValue,
...(childTree.expectedType && { expectedType: childTree.expectedType })
};
});
result[rootKey] = {
valid: tree.valid,
value
};
}
else {
// Primitive value
result[rootKey] = {
valid: tree.valid,
value: tree.actualValue,
...(tree.expectedType && { expectedType: tree.expectedType })
};
}
return result;
};
exports.createSimplifiedTree = createSimplifiedTree;