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

Added Zod integration (closes #1159) #1169

Merged
merged 19 commits into from
Oct 18, 2022
Merged

Added Zod integration (closes #1159) #1169

merged 19 commits into from
Oct 18, 2022

Conversation

radekmie
Copy link
Contributor

@radekmie radekmie commented Oct 4, 2022

In this pull request, I added a new package: uniforms-bridge-zod, closing #1159. The implementation is really straightforward and documented in a step-by-step tutorial on my blog: "On uniforms Integration With Zod".

Open topics:

  1. How to switch between sync and async validation? Zod allows both, but before you actually run it, you don't know if it's going to be async or not. Doing it async by default is not ideal either, as it results in multiple renders for sync schemas.
    • We can handle it with an option passed to the ZodBridge constructor.
  2. Is ZodError the only accepted error format? I think it does - it's well documented and typed. But maybe we should handle others as well?
  3. How to implement getErrorMessages? Right now it does the same thing as getErrorMessage, resulting in messages like "Value should be greater than or equal to 10000" which makes sense for inline errors, but doesn't work for the ErrorsField component.
    • We can prefix them with the field's label, but it won't be useful for list fields. But maybe that's OK?
  4. What additional schema-specific props are useful? For now I added maxCount/minCount for arrays and max/min/step for numbers. I'm thinking about things like .email() for string schemas being mapped to type: 'email'. It may be handy for some themes but problematic in others.
  5. How to pass custom props through the schema? There's no thing like that in Zod, or at least nothing I found out of the box. There's Data representation of schema? colinhacks/zod#507, and while it's not entirely that use case, I agree that it doesn't make sense to add it there.
    • A workaround for now is to provide the props directly in the JSX or subclassing the ZodBridge and extending the getProps function.

I've marked is at v3.x, but I'd rather see it as a v4.0 beta thing, due to bridge methods (see comments in the code).

@radekmie radekmie added this to the v4.0 milestone Oct 4, 2022
@github-actions github-actions bot added Area: Docs Affects the documentation or reproductions seed Area: Infra Affects the repository itself (e.g., CI, dependencies) labels Oct 4, 2022
@radekmie radekmie added Type: Feature New features and feature requests Area: Bridge Affects some of the bridge packages labels Oct 4, 2022
@codecov
Copy link

codecov bot commented Oct 4, 2022

Codecov Report

Merging #1169 (ee67bd3) into master (74cd151) will increase coverage by 0.05%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #1169      +/-   ##
==========================================
+ Coverage   98.49%   98.54%   +0.05%     
==========================================
  Files         228      231       +3     
  Lines        3643     3769     +126     
  Branches      948      997      +49     
==========================================
+ Hits         3588     3714     +126     
  Misses          4        4              
  Partials       51       51              
Impacted Files Coverage Δ
packages/uniforms-bridge-zod/src/ZodBridge.ts 100.00% <100.00%> (ø)
packages/uniforms-bridge-zod/src/index.ts 100.00% <100.00%> (ø)
packages/uniforms-bridge-zod/src/register.ts 100.00% <100.00%> (ø)
packages/uniforms-material/src/DateField.tsx 90.90% <0.00%> (-1.10%) ⬇️
packages/uniforms/src/useForm.ts 100.00% <0.00%> (ø)
packages/uniforms/src/BaseForm.tsx 100.00% <0.00%> (ø)
packages/uniforms-mui/src/DateField.tsx 100.00% <0.00%> (ø)
packages/uniforms-semantic/src/DateField.tsx 100.00% <0.00%> (ø)
packages/uniforms-unstyled/src/DateField.tsx 100.00% <0.00%> (ø)
packages/uniforms-bootstrap3/src/DateField.tsx 100.00% <0.00%> (ø)
... and 2 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@github-actions github-actions bot removed the Area: Bridge Affects some of the bridge packages label Oct 4, 2022
@radekmie radekmie added the Bridge: Zod Affects the uniforms-bridge-zod package label Oct 8, 2022
wadamek65
wadamek65 previously approved these changes Oct 13, 2022
@radekmie radekmie merged commit fc9fa3d into master Oct 18, 2022
@radekmie radekmie deleted the zodbridge branch October 18, 2022 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Docs Affects the documentation or reproductions seed Area: Infra Affects the repository itself (e.g., CI, dependencies) Bridge: Zod Affects the uniforms-bridge-zod package Type: Feature New features and feature requests
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants