Accessing Valibot schema params?
I have a schema:
Now want to do something like this:
Is it possible?
import * as v from 'valibot';
export const LoginSchema = v.object({
emailAddress: v.pipe(
v.string(),
v.email(),
v.nonEmpty('Email address is required'),
v.minLength(3, 'Must be a length of at least 3'),
v.maxLength(30, 'Email is too long')
),
password: v.pipe(
v.string(),
v.nonEmpty('Password is required'),
v.email(),
v.minLength(6, 'Must be a length of at least 6'),
v.maxLength(12, 'Password is too long'),
v.regex(/[A-Z]/, 'Must contain at least one capital letter'),
v.regex(
/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/,
'Password must contain at least one special character'
)
),
});
import * as v from 'valibot';
export const LoginSchema = v.object({
emailAddress: v.pipe(
v.string(),
v.email(),
v.nonEmpty('Email address is required'),
v.minLength(3, 'Must be a length of at least 3'),
v.maxLength(30, 'Email is too long')
),
password: v.pipe(
v.string(),
v.nonEmpty('Password is required'),
v.email(),
v.minLength(6, 'Must be a length of at least 6'),
v.maxLength(12, 'Password is too long'),
v.regex(/[A-Z]/, 'Must contain at least one capital letter'),
v.regex(
/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/,
'Password must contain at least one special character'
)
),
});
<form.Field
name="emailAddress"
validators={{
onChange: LoginSchema.email,
}}
>
<form.Field
name="emailAddress"
validators={{
onChange: LoginSchema.email,
}}
>
3 Replies
wise-whiteOP•12mo ago
I know I can do the following instead:
But having it in 1 object instead of 2 exports is much nicer...
export const LoginEmailAddressSchema = v.pipe(
v.string(),
v.email(),
v.nonEmpty('Email address is required'),
v.minLength(3, 'Must be a length of at least 3'),
v.maxLength(30, 'Email is too long')
);
export const LoginPasswordSchema = v.pipe(
v.string(),
v.nonEmpty('Password is required'),
v.email(),
v.minLength(6, 'Must be a length of at least 6'),
v.maxLength(12, 'Password is too long'),
v.regex(/[A-Z]/, 'Must contain at least one capital letter'),
v.regex(
/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/,
'Password must contain at least one special character'
)
);
export const LoginEmailAddressSchema = v.pipe(
v.string(),
v.email(),
v.nonEmpty('Email address is required'),
v.minLength(3, 'Must be a length of at least 3'),
v.maxLength(30, 'Email is too long')
);
export const LoginPasswordSchema = v.pipe(
v.string(),
v.nonEmpty('Password is required'),
v.email(),
v.minLength(6, 'Must be a length of at least 6'),
v.maxLength(12, 'Password is too long'),
v.regex(/[A-Z]/, 'Must contain at least one capital letter'),
v.regex(
/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/,
'Password must contain at least one special character'
)
);
<form.Field
name="emailAddress"
validators={{
onChange: LoginEmailAddressSchema,
}}
>
<form.Field
name="password"
validators={{
onChange: LoginPasswordSchema,
}}
>
<form.Field
name="emailAddress"
validators={{
onChange: LoginEmailAddressSchema,
}}
>
<form.Field
name="password"
validators={{
onChange: LoginPasswordSchema,
}}
>
fair-rose•12mo ago
In Zod it should be
LoginSchema.shape.emailAddress - so with Valibot you may try LoginSchema.entries.emailAddresswise-whiteOP•12mo ago
Thanks for taking the time to answer. That was it. So much nicer!