Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
baa2440
commit ad8c4de
Showing
4 changed files
with
80 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Validator } from 'validator'; | ||
import { Node } from './models'; | ||
|
||
describe('Validator', () => { | ||
describe('validateTreeInput', () => { | ||
it('should throw error if ids are duplicated', () => { | ||
// arrange | ||
const input: Node[] = [ | ||
{ id: '1', name: 'n1', children: [] }, | ||
{ id: '1', name: 'n1', children: [] }, | ||
]; | ||
|
||
// act | ||
const result = () => Validator.validateTreeInput(input); | ||
|
||
// assert | ||
expect(result).toThrowError('Duplicated ID found for: 1'); | ||
}); | ||
|
||
it('should throw error if ids are duplicated in child nodes', () => { | ||
// arrange | ||
const input: Node[] = [ | ||
{ id: '1', name: 'n1', children: [{ id: '1', name: 'n1', children: [] }] }, | ||
]; | ||
|
||
// act | ||
const result = () => Validator.validateTreeInput(input); | ||
|
||
// assert | ||
expect(result).toThrowError('Duplicated ID found for: 1'); | ||
}); | ||
}); | ||
}); | ||
// id, label, parent | ||
// 1, n1, | ||
// 2, n2, 1 | ||
// 3, , 1 | ||
// 3, , 1 | ||
// 4, n4n,3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Node } from './models'; | ||
|
||
/** | ||
* A utility class for validating a tree structure represented by an array of nodes. | ||
*/ | ||
export class Validator { | ||
/** | ||
* Validates the input tree for duplicate node IDs and child relationships. | ||
* @param {Node[]} tree - The array of nodes representing the tree structure. | ||
* @throws {Error} If a duplicate node ID is found. | ||
*/ | ||
public static validateTreeInput(tree: Node[]): void { | ||
/** | ||
* Set to store encountered node IDs. | ||
* @type {Set<Node['id']>} | ||
*/ | ||
const nodeIds: Set<Node['id']> = new Set(); | ||
|
||
/** | ||
* Recursively validates node IDs and child relationships. | ||
* @param {Node} node - The node to validate. | ||
* @throws {Error} If a duplicate node ID is found. | ||
*/ | ||
function validateIds(node: Node): void { | ||
if (nodeIds.has(node.id)) { | ||
throw new Error(`Duplicated ID found for: ${node.id}`); | ||
} | ||
|
||
nodeIds.add(node.id); | ||
|
||
if (node.children) { | ||
node.children.forEach(validateIds); | ||
} | ||
} | ||
|
||
tree.forEach(validateIds); | ||
} | ||
} |