-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Update zod 3 to zod 4 support in p5.js dev-2.0 #7872
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
16dd55b
migrate param_validator from zod3 to zod 4
madhav2348 bb0c548
Update test case of param_validator in param_errors
madhav2348 d5b08c2
update zod package version
madhav2348 b9ab468
revert test case of param_validator
madhav2348 495903e
update schema type check for zod function
madhav2348 a97a789
fallback to z.custom() for function validation
madhav2348 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Just curious what test case was generating an undefined schema? This works as a fallback, but I wonder if there's potentially a deeper issue needing fixing that this hides.
Uh oh!
There was an error while loading. Please reload this page.
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.
In Zod 3, an
undefined
somehow works fineschema.optional()
, but in Zod 4 throws aTypeError
, and 181 test cases failed in WebGL, Type and Visual . So usingz.any()
as safeguardThis could work by throw an error here to catch real bugs early instead of hiding them with z.any().
Its just a thought, not sure if this would works,
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 tried adding this:
...and then running just that failing test by changing it to
test.only(...)
, and I see this log:So it looks like
z.function()
is responsible for this? I think we don't want to turn this into anany
type, but rather, figure out how we can keep it a function, but make it possible to be optional. Maybe something is different about how functions are treated in zod 4?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.
My bad. In zod 4, the docs say:
'' The result of z.function() is no longer a Zod schema. Instead, it acts as a standalone "function factory" for defining Zod-validated functions. The API has also changed; you define an input and output schema upfront, instead of using args() and .returns() methods. ''
Do you have any suggestions on this? Because
any()
feels like a brute-force solution.I was considering a custom wrapper using
z.custom()
. But would love to hear your thoughts on thishttps://zod.dev/v4/changelog?id=zfunction
https://zod.dev/api?id=functions
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.
The quick change would be to set the input parameters to any, and the output type to any. We could in theory check the parameters too, but I think that's hard to do at runtime, so I think just making the parameters/return types any will be sufficient.
Uh oh!
There was an error while loading. Please reload this page.
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.
Changes made as suggested.
Let me know if further adjustments are needed!
Thank you
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.
hey sorry for the delay, I think I may have misinterpreted what you said. I think we still want to use a zod function, but where the zod function's return type is
any()
. But I think we still need to usez.function()
rather than immediately returningany()
.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.
@davepagurek, Can you help with this, because falling back to
z.any()
is a viable solution here.According to Zod 4, I use
output()
becausereturns()
methods no longer uses , so not returning zod function's type immediatly toany()
which follow the new API
But encounter some error in zod tuple in
overloadSchemas
and p5 error
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.
Can we try using
z.custom
to just check if the argumentinstanceof Function
? so,z.custom((val) => val instanceof Function)
?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.
That wouldn't check the arguments/return value, but that's likely sufficient.