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

Feat: Build form with Zod Schema #266

Draft
wants to merge 36 commits into
base: main
Choose a base branch
from

Conversation

fazzaamiarso
Copy link
Member

@fazzaamiarso fazzaamiarso commented Mar 7, 2023

Fixes #99

Description of changes:

  • new resolvers package
  • create zod schema resolver
  • create a resolver demo page

Tag a reviewer: @ayoayco

Tasks:

  • I have ran the build command to make sure apps are working: npm run build
  • I have ran the tests to make sure nothing is broken: npm run test
  • I have ran the linter to make sure code is clean: npm run lint
  • I have reviewed my own code to remove changes that are not needed

@netlify
Copy link

netlify bot commented Mar 7, 2023

Deploy Preview for astro-reactive-docs canceled.

Name Link
🔨 Latest commit a628f7a
🔍 Latest deploy log https://app.netlify.com/sites/astro-reactive-docs/deploys/641859e49acb2e0008ce5ed9

@netlify
Copy link

netlify bot commented Mar 7, 2023

Deploy Preview for astro-reactive canceled.

Name Link
🔨 Latest commit a628f7a
🔍 Latest deploy log https://app.netlify.com/sites/astro-reactive/deploys/641859e4f3539400089a34f1

@fazzaamiarso
Copy link
Member Author

fazzaamiarso commented Mar 7, 2023

@ayoayco what do you think about this approach?

How about we only use schema builder only in validators field? So, we just use schema builder as validator. This way, our form config could be more flexible.

It looks something like this

const formGroup = new FormGroup([
  {
  name : "firstName",
  label : "First Name",
  validators : zodResolver(z.string().optional().min(3))
 }
])

@ayoayco
Copy link
Member

ayoayco commented Mar 8, 2023

@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? 🤔

@fazzaamiarso
Copy link
Member Author

@ayoayco Tried new approach where we can just plug the resolver into FormGroup config.

Also, passing types to FormGroup constructor enable typesafety on the name props when building the FormGroup. I've made an example page on it.

Will clean the code and write tests later.

@ayoayco
Copy link
Member

ayoayco commented Mar 8, 2023

Thanks a lot @fazzaamiarso give me until this weekend to review the code 🙏

Copy link
Member

@ayoayco ayoayco left a 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!

apps/demo/src/pages/experimental/resolver.astro Outdated Show resolved Hide resolved
packages/common/types/validator.types.ts Outdated Show resolved Hide resolved
packages/form/core/form-control.ts Outdated Show resolved Hide resolved
packages/form/core/form-group.ts Outdated Show resolved Hide resolved
packages/form/core/form-group.ts Outdated Show resolved Hide resolved
packages/resolvers/vitest.config.ts Outdated Show resolved Hide resolved
packages/common/types/resolver.types.ts Outdated Show resolved Hide resolved
import { ZodFirstPartyTypeKind, ZodObject } from "zod";
import type { ResolvedField, ResolvedValidator } from "@astro-reactive/common";

export function zodResolver(schema: ZodObject<any>) {
Copy link
Member

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 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

Alright, will do it! 👍

@fazzaamiarso
Copy link
Member Author

@ayoayco what do you think about the new implementation?

@ayoayco ayoayco marked this pull request as ready for review March 15, 2023 17:24
Copy link
Member

@ayoayco ayoayco left a 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' }
Copy link
Member

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! :)

packages/form/core/form-group.ts Outdated Show resolved Hide resolved
@fazzaamiarso
Copy link
Member Author

@ayoayco we're trying changeset now. Could you install changeset bot to this repo?

@ayoayco
Copy link
Member

ayoayco commented Mar 19, 2023

Installed changeset bot!

@changeset-bot
Copy link

changeset-bot bot commented Mar 20, 2023

🦋 Changeset detected

Latest commit: a628f7a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@astro-reactive/resolvers Minor
@astro-reactive/validator Minor
@astro-reactive/form Minor
@astro-reactive/common Patch

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

@fazzaamiarso
Copy link
Member Author

@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.

@ayoayco
Copy link
Member

ayoayco commented Mar 20, 2023

@fazzaamiarso Ah no worries, we can set that up in another time. Thanks for entertaining my request to add a changesets entry

@ayoayco ayoayco marked this pull request as draft March 24, 2023 08:44
@ayoayco
Copy link
Member

ayoayco commented Sep 26, 2023

Sorry it took a while, I updated the dependencies. I'll pick this up next ;)

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.

feat(form): work with popular schema builders
2 participants