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
Nullable form field with constraints #454
Comments
Have you read the documentation? Here is an example schema. Try it out in our playground. import * as v from 'valibot';
const FormSchema = v.object({
value: v.union([
v.transform(v.string([v.regex(/^\s*$/u)]), () => null),
v.string([v.toTrimmed(), v.minLength(3), v.maxLength(10)]),
]),
}); |
Of course I read the documentation 🙂 I will try it this way:
And I can simplify it like this:
What do you think about it? |
As good as it looks. It could not make it work with Svelte Superforms. In Zod it looks like this and it works:
My Validbot schema looks like this:
But then Superforms struggles with type problems "null vs. undefined". I can't see where source of |
You can to the same with Valibot. Check it out in our playground. import * as v from 'valibot';
const Schema = v.coerce(
v.nullable(v.string([v.minLength(3)])),
(input) => (typeof input === 'string' && input.trim()) || null
); |
I would like to use Valibot to validate form fields. The input of text fields should be trimmed. If the result is an empty string the result should be
null
. For optional fields this would be the expected and accepted value. If there is text in the input field it should be possible to perform additional checks (i.e. minimum length or should be email). How would I do that?The text was updated successfully, but these errors were encountered: