Skip to content
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

Conditionally validate #573

Closed
kido1991 opened this issue May 10, 2024 · 2 comments
Closed

Conditionally validate #573

kido1991 opened this issue May 10, 2024 · 2 comments
Assignees
Labels
question Further information is requested

Comments

@kido1991
Copy link

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

@fabian-hiller
Copy link
Owner

Yes it is. Here is an example:

import * as v from 'valibot';

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

@fabian-hiller fabian-hiller self-assigned this May 10, 2024
@fabian-hiller fabian-hiller added the question Further information is requested label May 10, 2024
@kido1991
Copy link
Author

Thanks! it's working now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants