Skip to content

feature: traverse ValidationError.children #2328

Closed

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

Activity

  1. changed the title [-]feature: traverse errors[/-] [+]feature: traverse ValidationError.children[/+] on Dec 5, 2023
  2. braaar commented on Jan 11, 2024

    @braaar
    Member

    I'm moving this to the discussion tab, as we are trying to keep the issue count low. Please see this issue for an explanation.

  3. locked and limited conversation to collaborators on Jan 11, 2024
  4. converted this issue into a discussion #2363 on Jan 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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