Closed
Description
Description
When a ValidationError is nested (its has children) producing an error message based on the children state requires some
traveseal function.
Proposed solution
Add a utility / a method on each ValidationError which performs a BFS traverse including the path
const traverseValidationError = (root: ValidationError, cb: (path: string, error: ValidationError) => void) => {
const queue = [{ error: root, path: root.property }];
while (queue.length > 0) {
const item = queue.shift();
if (item !== undefined) {
const { error, path } = item;
if (error.children?.length) {
for (const childError of error.children) {
const childPath = `${path}.${childError.property}`;
queue.push({ error: childError, path: childPath });
}
}
cb(path, error);
}
}
};
Note that the above function doesn't evaluate whether a proprety is number, and will produce
some_array.1.some_property instead of some_array.[1].some_property