-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Option to create params from input in custom validation #88
Comments
Great idea. I'll definitely do something like this. Might leave the |
For a more advanced use, I would also like to have the method with a single parameter:
with this, the customs validations can be reused more easily:
That is only a draft to present the option |
@FranckBontemps check this one out #132 I think that this can be even more powerful when the |
I have a related use-case, where I'd like to use information from the validation function in the error message. E.g. a simple database definition where we can define a record of column names and a primary key array: z.object({
columns: z.record(z.string()),
primaryKey: z.array(apiString({}).nonempty()).nonempty()
}) in this case the The custom validation function could look like this: .refine(
(data) => {
for (const pkItem of data.primaryKey) {
if (!(pkItem in data.columns)) {
// We'd like to include pkItem in the error message
return false;
}
}
return true;
},
{
message: `the primaryKey array must only contain keys from the columns record`,
path: ['primaryKey']
}
); In this function we already have the logic to find the item that violates the check. It would be great if we could easily reuse this in the error-message. I hope that this can also be considered when working on a solution for this issue. |
This is now possible in Zod 2 (now in beta) const dynamicRefine = z.string().refine(
val => val === val.toUpperCase(),
val => ({ params: { val } }),
);
console.log(dynamicRefine.safeParse('asdf')); returns
@tmtron this is a pretty advanced use case but it is possible. Internally Some other context: in Zod 2 the const apiString = z.string();
z.object({
columns: z.record(z.string()),
primaryKey: z.array(apiString.nonempty()).nonempty(),
})._refinement((data, ctx) => {
const invalidPks = data.primaryKey.filter(
pk => !Object.keys(data.columns).includes(pk),
);
if (invalidPks.length) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `Invalid PKs: ${invalidPks.join(', ')}`,
});
}
}); As you can see there's no need to return anything from You need to manually provide the error code using the @brabeji The approach above also enables what you were trying to achieve with |
zod 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
The text was updated successfully, but these errors were encountered: