[zod-form-data] - Schema based on checkbox state #255
-
This is using So I am building a login form with the usual username/password fields. Additionally, we have MFA. const BaseLogin = z.object({
action: z.literal(Actions.PASSWORD),
email: z
.string()
.min(5, { message: "Email address must be at least 5 characters long" })
.email("Please enter a valid email"),
password: z
.string()
.min(6, { message: "password must be at least 6 characters long" })
});
const loginSchema = z.union([
BaseLogin.merge(z.object({
mfa: zfd.checkbox().refine(v => v),
login_code: z
.string()
.length(6, { message: "mfa code must be at 6 digits long" })
.refine(
val => /^\d{6}$/.test(val),
"mfa code must be at 6 digits long"
)
})),
BaseLogin.merge(z.object({
mfa: zfd.checkbox().refine(v => !v)
})),
z.object({ action: z.literal(Actions.LOGOUT) })
]); However, this doesn't seem to work. I feel like there should be a simple solution, but after a few days of bashing my head, I thought I would reach out and ask. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Completely my own fault, the name on the However, if anyone has any ideas on how to simplify this, I would be interested. |
Beta Was this translation helpful? Give feedback.
Completely my own fault, the name on the
login_code
input was wrong.However, if anyone has any ideas on how to simplify this, I would be interested.