unovue / shadcn-vue

Vue port of shadcn-ui
https://www.shadcn-vue.com/
MIT License
4.59k stars 267 forks source link

[Feature]: Add Support for `z.union()` in Validation Schemas #791

Open zkhan93 opened 3 days ago

zkhan93 commented 3 days ago

Describe the feature

Please add support for z.union() in validation schemas for shadcn Vue. Currently, using z.union() to handle multiple types (e.g., z.string().email() and z.literal('') for optional/nullable fields) is not supported, which limits flexibility.

For example, this schema for an optional email:

email: z.union([z.literal(''), z.string().email().optional()])

renders nothing, likely skipping the field altogether.

Using this:

email: z.string().email().optional()

renders an email field, but when the value is removed, it still shows an invalid email error.

This issue on the Zod repo explains how to define an optional email field using z.union() for proper validation. Screenshot 2024-09-30 at 5 27 09 PM

Additional information