Skip to content
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

Add types to extract error results from schema #856

Merged
merged 2 commits into from
Jan 19, 2022

Conversation

fnky
Copy link
Contributor

@fnky fnky commented Jan 15, 2022

Fixes #855

[P in keyof TypeOf<T>]?: U[];
};
};
export type TypeOfFormErrors<
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a single z.inferFormErrors type that just has the signature of z.inferFlattenedError, but I wanted to avoid confusion between results of flatten() vs formErrors getter.

What would you prefer? A single type, or is the convince alias type useful?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally appreciate the alias here since it actually gives a pretty nice hint at what is actually happening.

@kentcdodds
Copy link
Contributor

This would be quite helpful! I would like to use this in the example project for integrating Remix with zod.

Copy link
Collaborator

@scotttrinh scotttrinh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any type-level tests that we can add, maybe some AssertEqual to the errors test suite?

src/ZodError.ts Show resolved Hide resolved
[P in keyof TypeOf<T>]?: U[];
};
};
export type TypeOfFormErrors<
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally appreciate the alias here since it actually gives a pretty nice hint at what is actually happening.

@fnky
Copy link
Contributor Author

fnky commented Jan 18, 2022

Are there any type-level tests that we can add, maybe some AssertEquals to the errors test suite?

I was thinking the same. There's a few libraries to do so; dtslint and tsd are the two most prominent. The latter has some differences with dtslint.

@scotttrinh
Copy link
Collaborator

I was thinking the same.

I just meant literally using the existing AssertEqual type in a test case in the errors test suite. We have a few examples around: https://github.com/colinhacks/zod/search?q=AssertEqual

@fnky
Copy link
Contributor Author

fnky commented Jan 18, 2022

I was thinking the same.

I just meant literally using the existing AssertEqual type in a test case in the errors test suite. We have a few examples around: https://github.com/colinhacks/zod/search?q=AssertEqual

Oh, sorry I totally misunderstood. I'll add some tests for this :)

@fnky
Copy link
Contributor Author

fnky commented Jan 19, 2022

@scotttrinh I added tests for type inference and type assertion with flatten() as well as additional checks.

I don't know if all-errors.test.ts is the right place as safeparse.test.ts also seemed like a good location for this, but let me know :)

Copy link
Collaborator

@scotttrinh scotttrinh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great tests, great implementation, thanks for your contribution!

@scotttrinh scotttrinh merged commit cc8ad19 into colinhacks:master Jan 19, 2022
@kentcdodds
Copy link
Contributor

Wonderful! Thank you!

@fnky fnky deleted the infer-error-type branch January 19, 2022 23:36
@kentcdodds
Copy link
Contributor

When can we expect a release of this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Helper type to infer errors from flattened safeParse error result.
3 participants