-
Notifications
You must be signed in to change notification settings - Fork 25
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
Nested objects and arrays #18
Comments
Hey, @02JanDal! Thanks for creating this issue 💪 We don't have support for objects and arrays in the schema yet, but it is high on our priority list to add it :) In the meantime, you can still use Remix Forms for the schema above, but instead of rendering a The server-side validations do work with objects and arrays already, so you're covered on that end. Can you please let us know if this works? |
Hey, @danielweinmann. I tried this with a set off checkboxes, all with the same const schema = z.object({
name: z.string().nonempty(),
age: z.number(),
hobbies: z.array(z.string()).nonempty("Must have at least one hobby")
}); When I have two or more checkboxes checked, I get the correct Update: Changed my Zod schema to: const schema = z.object({
name: z.string().nonempty(),
age: z.number(),
hobbies: z.union([z.string().optional(), z.array(z.string()).optional()]).optional()
.transform(val => typeof val !== "undefined" ? Array.isArray(val) ? val : [val] : [])
}); Now, it submits correctly with a single checkbox or multiple checkboxes checked. But, when I submit with zero checkboxes checked, it focuses (but does not check) the first checkbox without submitting. Oddly, if I submit a second time after the first checkbox is focused, it does submit correctly. Not sure why. Update 2 (Finally got it): const schema = z.object({
name: z.string().nonempty(),
age: z.number(),
hobbies:
z.preprocess(
val => !!val ? Array.isArray(val) ? val : [val] : [],
z.array(z.string())
)
}); |
Hey, @gederer! Can you share a complete example of how you made it work with us? I'm designing an API to deal with nested objects and arrays, and it would be great to contemplate your use case when thinking about it. |
Really interesting library already!
There does not seem to be support for nested objects and arrays yet, AFAICT.
For example based on a schema like this:
It seems like it should be pretty easy to do this using
register
etc. fromreact-hook-form
, but it would be nice to have support for that inremix-forms
.The text was updated successfully, but these errors were encountered: