fabian-hiller / valibot

The modular and type safe schema library for validating structural data 🤖

Home Page:https://valibot.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Conditionally validate

kido1991 opened this issue · comments

import * as v from 'valibot';

const Schema = v.object({
  email: v.optional(v.string([v.email()])),
  password: v.string([v.minLength(1), v.minLength(8)]),
});

const result = v.safeParse(Schema, {
  email: '',
  password: '12345678',
});

console.log(result);

Hi,

From the example above, is it possible that if the email is empty do not validate with email() but only when it is not null or not empty string only then validate it?

Thanks

Yes it is. Here is an example:

import * as v from 'valibot';

const Schema = v.union([v.literal(''), v.string([v.email()])]);

Thanks! it's working now!