fabian-hiller / valibot

The modular and type safe schema library for validating structural data 🤖
https://valibot.dev
MIT License
5.6k stars 169 forks source link

Empty email field validation #697

Open robodna opened 4 days ago

robodna commented 4 days ago

I have a form with 2 email fields. I added the following validation:

email_work: v.pipe(v.string(), v.nonEmpty(), v.email()), email_home: v.nullish(v.pipe(v.string(), v.email())),

I'm using ModularForms with SolidJS.

When I add an exclamation mark to the email_home field, it shows an error as I expect. If i delete the exclamation mark and leave the email_home field empty, an error remains showing Invalid email: Received "". I expect no validation error as I used v.nullish.

Any ideas on what I may be doing wrong or does this look like a bug?

Thanks,

fabian-hiller commented 4 days ago

Here is a fix. Try it out in our playground.

const Schema = v.object({
  email_work: v.pipe(v.string(), v.email()),
  email_home: v.union([v.literal(''), v.pipe(v.string(), v.email())]),
});