TripleSpeederT
Convex Community3y ago
3 replies
TripleSpeeder

verify Table ID type in a form with zod

I want to submit an id of convex type
Id<"part">
with a form. I'm doing form validation with zod and wondering what is the best way to do this. If i use
z.string()
for the part ID, typescript complains that
string
can not be assigned to
Id<"part">
when calling the mutation function. This sounds reasonable, as the mutation indeed expects an argument of type
Id<"part">
.

I get correct types and no typescript error when using
z.custom<Id<"part">>()
to validate the field. But I think I still need to add a validator function to this custom type, because zod says
If you don't provide a validation function, Zod will allow any value.
I can add a manual check to make sure the provided value is indeed a string, and maybe check length and whatnot. But I'm wondering if I'm missing something here... Is there a best practice for a scenario like this?
Was this page helpful?