-
Notifications
You must be signed in to change notification settings - Fork 43
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
Feat: Build form with Zod Schema #266
base: main
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for astro-reactive-docs canceled.
|
✅ Deploy Preview for astro-reactive canceled.
|
@ayoayco what do you think about this approach? How about we only use schema builder only in It looks something like this const formGroup = new FormGroup([
{
name : "firstName",
label : "First Name",
validators : zodResolver(z.string().optional().min(3))
}
]) |
@fazzaamiarso thanks for this! I think that suggestion will indeed address the need to keep some props that we need to build the form (like the label). This is great if users are writing from scratch, I think. I'm just trying to think of a solution for one use case; which is for people who already have their schemas written in zod. So they might already have objects, and just want to plug that in when building their form and still be able to provide properties like label. How do we implement it so that they don't have to refactor/rewrite a lot? 🤔 |
@ayoayco Tried new approach where we can just plug the Also, passing types to Will clean the code and write tests later. |
Thanks a lot @fazzaamiarso give me until this weekend to review the code 🙏 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, @fazzaamiarso thanks for this! It's nice to see some unit tests started, and I think we need more of that for existing classes.
I went from top to bottom putting comments to each file independently. Please read through them so we are in line with some goals the project needs (i.e. reduce package coupling)
However, in the end, I realized it would be good if we can ship the resolver function as part of the validator
package as well. We can then remove the need for the transformToValidatorRules
if the resolver function is already able to return an object by using the functions in Validators. If this is achievable with having a separate resolver
package, then we can move forward with having a new package.
But keep in mind we want to reduce/remove (non-common) package dependencies (more info in one of my comments below)... maybe a Map in the common
package can help? ;)
The rough idea is that the resolver function from our validator package will be able to take in the schema and spit out an array of strings using functions in our Validators class that our FormControl constructor can already understand and use.
Let me know if anything is not clear. As always this is already a great exploration, so thank you!
packages/resolvers/zod/index.ts
Outdated
import { ZodFirstPartyTypeKind, ZodObject } from "zod"; | ||
import type { ResolvedField, ResolvedValidator } from "@astro-reactive/common"; | ||
|
||
export function zodResolver(schema: ZodObject<any>) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a return type here, ResolvedField
I think. Also, some JSDoc for the function would be nice!
Then I will ask that you start a new page in our docs site as well for resolvers. I don't mind if it is very initial. Just copy-paste the JSDoc 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, will do it! 👍
@ayoayco what do you think about the new implementation? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this implementation is a good start and would love to merge this to play around with Zod :) Just a few requests/suggestions left 🙏 Also don't forget to "resolve" previous comments you addressed so I can scan them later. ❤️
@@ -37,7 +37,7 @@ const nameForm = new FormGroup( | |||
value: 'Doe', | |||
}, | |||
], | |||
'Name' | |||
{ name: 'Name' } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just realized, because this is a major API change, I would have to ask that you update old examples in existing docs/readme 🙏 @fazzaamiarso
Also maybe this is a good chance to add a changeset! :)
@ayoayco we're trying changeset now. Could you install changeset bot to this repo? |
Installed changeset bot! |
🦋 Changeset detectedLatest commit: a628f7a The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@ayoayco currently our repo's Changeset GH action only create a new PR with bumped package version without publising them. That means we must merge PR and publish it manually. Although, we can set up a GH action that automate the release too. https://github.com/changesets/action#with-publishing. |
@fazzaamiarso Ah no worries, we can set that up in another time. Thanks for entertaining my request to add a changesets entry |
Sorry it took a while, I updated the dependencies. I'll pick this up next ;) |
Fixes #99
Description of changes:
resolvers
packageTag a reviewer: @ayoayco
Tasks:
npm run build
npm run test
npm run lint