Skip to content

feature: traverse ValidationError.children #2328

Closed
@eladchen

Description

@eladchen

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    flag: needs discussionIssues which needs discussion before implementation.type: featureIssues related to new features.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions