Using zod tuples #287
augustskare
started this conversation in
General
Replies: 1 comment 1 reply
-
That's indeed an interesting usecase. I did a quick test and this should work for what you need: const schema = z.object({
kv: z.tuple([z.string(), z.number()]),
});
const [form, { kv }] = useForm({
defaultValue: {
// This is necessary to make sure `useFieldList` initialized properly with 2 items
kv: ['', ''],
},
onValidate({ formData }) {
return parse(formData, { schema });
},
});
const [key, value] = useFieldList(form.ref, kv);
console.log(key.name) // kv[0]
console.log(value.name) // kv[1] There are 2 minor issues here:
I am still trying to understand the implication and the other possible usecases here. But I agree it's not a bad idea to support this with // instead of this:
const [key, value] = useFieldList(form.ref, kv);
// Maybe we can do this in the future?
const [key, value] = useFieldset(form.ref, kv); |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Might be a odd use cases, but I need to validate a form where the fields need to be grouped in a given order. To achieve this I'm using zod tuples. Validation works fine and all, but I'm not able to access the field (get errors, generated ids etc.).
I also tried to access it using
useFieldset
, but I don't think that's the right use case:Beta Was this translation helpful? Give feedback.
All reactions