-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Optional support for tuples #149
Comments
Huh never actually considered this use case. This is on the roadmap. For now I recommend creating a union of tuple schemas, instead of a single tuple of optional schemas. |
I toyed around with the implementation - and surprisingly it just seems to work more naturally with a single tuple with 2 input parameters, the required tuple, and the optional tuple where you supply the elements in non-optional form. Then changes to the implementation. Transform the second tuple into optionals (partial) In parse - modify the lower bound check of count to ensure at least count of required Ends up looking like : const p2or3 = z.tuple([z.number(), z.null()] as const, [z.string()] as const)
p2or3.parse([1, null])
p2or3.parse([1, null, "test"])
p2or3.parse([1, null, undefined]) The typescript types for it will probably need Typescript 4 |
This will need #152 in order for the api (+ implementation) to be as nature as possible |
Unfortunately I don't think this is worth bumping the minimum version to v4. There may be a way to provide TS4-specific functionality using project references...I'll look into it. If I can figure that out I'll provide an improved version of the ZodTuple that supports this. |
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. |
Hey guys, is this still a work in progress? Would be nice ti have optionals support in tuples 😞 |
My usecase is defining a list of data for image in md/x frontmatter. I would like to define tuples with the img src and an optional img alt (or title) string: .md/x
.js:
I would find optional elements in tuples very useful! |
Well, my use case is I am defining function argument schemas for Socket.IO event handlers (basically a schema for |
Same use case, same problem 🥱 |
@colinhacks You said it's on the roadmap but the issue is marked as wontfix by the bot. |
Oh no, this is missed out |
@colinhacks can team support this issue, please? |
ping @colinhacks can you re-open ? |
Just came across this issue as I was trying to do the same thing. As a workaround (based upon op's original code sample), I was able to define the following: const schema =
z.tuple([
z.number(),
]).or(
z.tuple([
z.number(),
z.record(z.any()),
]),
); |
I have tried everything I can think of - however something is preventing me from writing a function type for variable number of parameters.
I can very easily write a tuple in typescript that support a variable number of elements
However I cannot figure out how to do the same with zod.
It appears that it is not supported - what is involved in supporting it?
Note that since my use case is function schemas - i need to support:
The text was updated successfully, but these errors were encountered: