Option to create params from input in custom validation
See original GitHub issuezod custom validation allows params to be passed in to the error object. It would be nice if these could be created based on the initial data.
That is, instead of this type signature:
.refine(validator: (data:T)=>any, params?: RefineParams)
zod could use:
.refine(validator: (data:T)=>any, params?: RefineParams | (data: T) => RefineParams
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Pass input params to custom validation function ruby
1 Answer 1 ; # app/models/model_name.rb validate :validation_fcn_for_attr_1 validate :validation_fcn_for_attr_2 ; # app/models/model_name.rb ...
Read more >Custom Validator with Parameters in Angular - TekTutorialsHub
We learned how to pass a parameter to a custom validator. First, we create a factory function, which accepts the parameter. The factory...
Read more >Spring MVC Custom Validation - Baeldung
In this tutorial, we'll do just that; we'll create a custom validator to validate a form with a phone number field, and then...
Read more >Add custom validations and actions - Testim overview
Using an Add custom validation step or an Add custom action step, you can create custom steps where you input your own parameters...
Read more >Custom Rules - VeeValidate
You can recieve an object instead of an array for your validation rule by providing a paramNames array in the extend options (third...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is now possible in Zod 2 (now in beta)
returns
@tmtron this is a pretty advanced use case but it is possible. Internally
.refine
delegates to the more advanced._refinement
method which you can use to implement this. You can use this method without worrying about breaking changes, I consider it a part of the public API; it only starts with an underscore mostly because it’s a “power user” feature.Some other context: in Zod 2 the
errors
property of a ZodError has been renamed toissues
. This clarifies the confusing “error” vs “suberror” aspect of the previous naming.As you can see there’s no need to return anything from
_refinement()
. Ifctx.addIssue
is called at any point during the execution of the refinement, the parse will fail. You can callctx.addIssue
multiple times to create multiple errors. You can also make this function async if you want to do your database fetches inside your refinement.You need to manually provide the error code using the
z.ZodErrorCode
enum. This isn’t required in.refine
because.refine
only every throws a singleZodCustomIssue
whereas `refinement lets you throw arbitrarily many issues of any kind. The error handling guide has more information on this.@brabeji The approach above also enables what you were trying to achieve with
.dynamicRefinement
. Great job on that PR 👍Great idea. I’ll definitely do something like this. Might leave the
.refine
method as is but add a.refinement
for more advanced use cases.