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 label in zod type Close #1767 #1902

Closed
wants to merge 2 commits into from

Conversation

Rishabhg71
Copy link

@Rishabhg71 Rishabhg71 commented Jan 18, 2023

resolves #1767

as you asked i, have made a working draft #1902

z.object({
  first: z.string({ label: "First Name", }).min(3),
})

it's only working for z.object({ first: z.string({ label: "First Name", }).min(3), }) at the moment but you will get an idea of how this is gonna + I have also added labeled messages in "en.ts" (all the messages are not good)

In answering your question about DX if we add labeled errors I think it will be great. when I used Zod for the first time this was a single feature I thought must exist to make this library complete

Without Labeled error

z.object({
  first: z.string().min(3),
}).safeParse({ first: "AB" })
// error message
// {
//   "code": "too_small",
//   "minimum": 3,
//   "type": "string",
//   "inclusive": true,
//   "exact": false,
//   "message": "Invalid input",
//   "path": [
//     "first"
//   ]
// }

we cant send it to a client or show it to a user directly we have to add customized errors everywhere even though if its a really simple message like "First name is Required" since "Invalid input" cant be sent

z.object({
  first: z.string({ invalid_type_error: "first name should be of String", required_error: "First name is required" }).min(3, { message: "first name should be more than 3 char" }),
})

just to add really simple messages it becomes mandatory to include a "message" for every check

With Labeled Errors

if the label is passed all those messages can be removed and that error can be passed directly to the client/user
even if they want to customize the message further they can do so

z.object({
  first: z.string({ label: "First Name" }).min(3),
}).safeParse({ first: "AB" })
// {
//   "code": "too_small",
//   "minimum": 3,
//   "type": "string",
//   "inclusive": true,
//   "exact": false,
//   "message": "First Name must be at least 3 character(s)",
//   "label": "First Name",
//   "path": [
//     "first"
//   ]
// }

if you think this feature is worth added do you mind assigning it to me

@netlify
Copy link

netlify bot commented Jan 18, 2023

Deploy Preview for guileless-rolypoly-866f8a ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit b6538e0
🔍 Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/63c81409b2b34e000b31b320
😎 Deploy Preview https://deploy-preview-1902--guileless-rolypoly-866f8a.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@Rishabhg71 Rishabhg71 changed the title added label in zod type added label in zod type close #1767 Jan 18, 2023
@Rishabhg71 Rishabhg71 changed the title added label in zod type close #1767 added label in zod type Close #1767 Jan 18, 2023
@stale
Copy link

stale bot commented Jun 16, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale No activity in last 60 days label Jun 16, 2023
@stale stale bot closed this Jul 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale No activity in last 60 days
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a label function as like yup
1 participant