T
TanStack•13mo ago
other-emerald

Is there a way I can define my defaultValues with a schema

import {
email,
InferInput,
minLength,
nonEmpty,
object,
pipe,
regex,
string,
} from 'valibot'

export const LoginSchema = object({
email: pipe(
string('Email is required'),
nonEmpty('Please type your email'),
email('Invalid email format'),
),
password: pipe(
string('Password is required'),
nonEmpty('Please type your password'),
minLength(8, 'Password must be at least 8 characters long'),
regex(
/^[a-zA-Z]+[0-9!@#$%^&*]+$/,
'Password must contain at least one uppercase letter, one lowercase letter, one number, and one special character',
),
),
})
export type LoginFormFields = InferInput<typeof LoginSchema>
import {
email,
InferInput,
minLength,
nonEmpty,
object,
pipe,
regex,
string,
} from 'valibot'

export const LoginSchema = object({
email: pipe(
string('Email is required'),
nonEmpty('Please type your email'),
email('Invalid email format'),
),
password: pipe(
string('Password is required'),
nonEmpty('Please type your password'),
minLength(8, 'Password must be at least 8 characters long'),
regex(
/^[a-zA-Z]+[0-9!@#$%^&*]+$/,
'Password must contain at least one uppercase letter, one lowercase letter, one number, and one special character',
),
),
})
export type LoginFormFields = InferInput<typeof LoginSchema>
I want to create my values like so
1 Reply
like-gold
like-gold•13mo ago
I think this question is more about how to get a default object from a valibot schema 🤔 Unless your schema is huuge, I'd say doing it manually sounds like a quick task and easy to maintain.

Did you find this page helpful?