* Builds a nested tree structure from dot-notation paths * Uses lodash setWith to avoid automatic array creation
(errors: ValidationError[])
| 10 | * Uses lodash setWith to avoid automatic array creation |
| 11 | */ |
| 12 | function buildNestedTree(errors: ValidationError[]): TreeNode { |
| 13 | const tree: TreeNode = {}; |
| 14 | errors.forEach(error => { |
| 15 | if (!error.path) { |
| 16 | // Root level error - use empty string as key |
| 17 | tree[''] = error.message; |
| 18 | return; |
| 19 | } |
| 20 | |
| 21 | // Try to enhance the path with meaningful values |
| 22 | const pathParts = error.path.split('.'); |
| 23 | let modifiedPath = error.path; |
| 24 | |
| 25 | // If we have an invalid value, try to make the path more readable |
| 26 | if (error.invalidValue !== null && error.invalidValue !== undefined && pathParts.length > 0) { |
| 27 | const newPathParts: string[] = []; |
| 28 | for (let i = 0; i < pathParts.length; i++) { |
| 29 | const part = pathParts[i]; |
| 30 | if (!part) continue; |
| 31 | const numericPart = parseInt(part, 10); |
| 32 | |
| 33 | // If this is a numeric index and it's the last part where we have the invalid value |
| 34 | if (!isNaN(numericPart) && i === pathParts.length - 1) { |
| 35 | // Format the value for display |
| 36 | let displayValue: string; |
| 37 | if (typeof error.invalidValue === 'string') { |
| 38 | displayValue = `"${error.invalidValue}"`; |
| 39 | } else if (error.invalidValue === null) { |
| 40 | displayValue = 'null'; |
| 41 | } else if (error.invalidValue === undefined) { |
| 42 | displayValue = 'undefined'; |
| 43 | } else { |
| 44 | displayValue = String(error.invalidValue); |
| 45 | } |
| 46 | newPathParts.push(displayValue); |
| 47 | } else { |
| 48 | // Keep other parts as-is |
| 49 | newPathParts.push(part); |
| 50 | } |
| 51 | } |
| 52 | modifiedPath = newPathParts.join('.'); |
| 53 | } |
| 54 | setWith(tree, modifiedPath, error.message, Object); |
| 55 | }); |
| 56 | return tree; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Groups and displays validation errors using treeify with deduplication |
no test coverage detected