fabian-hiller / valibot

The modular and type safe schema library for validating structural data 🤖

Home Page:https://valibot.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

flatten return type/FlatErrors type is incorrect for object schemas that contain properties with async schemas

niccholaspage opened this issue · comments

The FlatErrors type doesn't seem to work correctly for object schemas that have properties with async schemas.

Code like this:

const valibotPersonSchema = v.objectAsync({
  firstName: v.stringAsync([v.minLength(1)]),
  lastName: v.string(),
});

const flatIssues = v.flatten<typeof valibotPersonSchema>(
  vPersonResult.issues
);

Unfortunately results in flatIssues.nested not having the right keys in the Record and instead a type of never.

Here is a reproduction:
https://stackblitz.com/edit/stackblitz-starters-kz1tg3?file=index.ts

Thanks for creating this issue. In my tests stringAsync is the problem. I will try to investigate it further this week or next.

I appreciate you taking a look! To add a bit more info, I was also able to trigger the issue by using other async schemas inside of the async object schema - for example, numberAsync and such. I went down a bit of a debugging rabbit hole after creating this issue and noticed that the NestedPath type seems to return never instead of the expected keys in this case, but didn’t figure out why yet. I will let you know if I find anything else out myself before you take a deeper look!

I know what the problem is. If we split the code in lines 78 to 83 into a condition for non-async and one for async, it works. I will refactor the code later.

v0.30.0 is available