question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Export InvalidUnionError type

See original GitHub issue

I am currently making a utility function for a use case to specially handle union errors. The error object is a bit nested in my situation so feeling the need for that. Can InvalidUnionError be exported as a type from the library? Otherwise finding it a bit difficult to deal with it in TS.

For instance, I have:

function findUnionError(zodError: zod.ZodError): ZodSuberror | undefined {
  return zodError.errors.find(err => {
    return err.code === zod.ZodErrorCode.invalid_union;
  })
}

It will be great if I can do:

function findUnionError(zodError: zod.ZodError): InvalidUnionError | undefined {
  return zodError.errors.find((err) => {
    return err.code === zod.ZodErrorCode.invalid_union;
  }) as InvalidUnionError | undefined;
}

Because later I need to access the .unionErrors property and can’t do it:

image

I am again needing to do:

function something(err: zod.ZodError): void {
  const errorItem = findUnionError(err);
  if (errorItem !== undefined && errorItem.code === zod.ZodErrorCode.invalid_union) {
    // do something with errorItem.unionErrors
    errorItem.unionErrors
  }
}

which feels like a little extra steps.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
colinhackscommented, Aug 30, 2020

As of zod@1.11 InvalidUnionError and all other suberror data types are now exported

1reaction
maneetgoyalcommented, Aug 23, 2020

@vriad

Are you just trying to print a custom error? If so the recommended way to do this is with a custom error map.

No, I am trying to access a deeply nested error in a ZodSuberror with error code ZodErrorCode.invalid_union. I tried using ZodErrorMap but the thing it seems to be good at is modifying the error messages. In my use case, I am not altering the error message. The issue is that the error message I am interested in extracting is quite deep:

  • I get a InvalidUnionError.
  • Then I go to its .unionErrors property which is an array of ZodError.
  • In that, I am only interested in the ZodError which has the smallest .errors array size.
  • In that smallest ZodError, the ZodSuberror I am interested in is again a InvalidUnionError.
  • Finally, I am interested in that error message.
const smallestError = findSmallestZodError(subError.unionErrors);
const unionError = findZodUnionError(smallestError);
if ( unionError !== undefined && unionError.path.includes("something")) {
   lodash.set(errors, unionError.path, unionError.unionErrors[0].errors[0].message);
}

For achieving it, one of the utility function I made is this:

/**
 * Checks whether a given Zod error has a sunion type sub error
 * @param err
 */
export function findZodUnionError(
  err?: zod.ZodError
): InvalidUnionError | undefined {
  let unionError: InvalidUnionError | undefined;
  if (err !== undefined) {
    unionError = err.errors.find((subErr) => {
      return subErr.code === zod.ZodErrorCode.invalid_union;
    }) as InvalidUnionError | undefined;
  }
  return unionError;
}

So if InvalidUnionError is exported natively, it will be quite useful in my case. Any other suggestion is also greatly appreciated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

export type * from 'somewhere' #48508 - GitHub
The way to think about type-only imports and exports is that they have identical resolution semantics to their non-type-only counterparts, but ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found