Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: traverse ValidationError.children #2328

Closed
eladchen opened this issue Dec 5, 2023 · 1 comment
Closed

feature: traverse ValidationError.children #2328

eladchen opened this issue Dec 5, 2023 · 1 comment
Labels
flag: needs discussion Issues which needs discussion before implementation. type: feature Issues related to new features.

Comments

@eladchen
Copy link

eladchen commented Dec 5, 2023

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

@eladchen eladchen added flag: needs discussion Issues which needs discussion before implementation. type: feature Issues related to new features. labels Dec 5, 2023
@eladchen eladchen changed the title feature: traverse errors feature: traverse ValidationError.children Dec 5, 2023
@braaar
Copy link
Member

braaar commented Jan 11, 2024

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.

@typestack typestack locked and limited conversation to collaborators Jan 11, 2024
@braaar braaar converted this issue into discussion #2363 Jan 11, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
flag: needs discussion Issues which needs discussion before implementation. type: feature Issues related to new features.
Development

No branches or pull requests

2 participants