Z
Zod12mo ago
OMB

OMB - hmm... I know this issue was closed way b...

hmm... I know this issue was closed way back, but why does this work
const passwordSchema = z.object({
password: z.string().min(8),
confirmPassword: z.string()
}).refine((data) => data.password === data.confirmPassword, {
message: "Password must match",
path: ["confirmPassword"]
})

const userSchema = z.object({
emailAddress: z.string().email(),
userName: z.string(),
userType: z.string(),
firstName: z.string(),
lastName: z.string(),
})

const formSchema = z.intersection(passwordSchema, userSchema)
const passwordSchema = z.object({
password: z.string().min(8),
confirmPassword: z.string()
}).refine((data) => data.password === data.confirmPassword, {
message: "Password must match",
path: ["confirmPassword"]
})

const userSchema = z.object({
emailAddress: z.string().email(),
userName: z.string(),
userType: z.string(),
firstName: z.string(),
lastName: z.string(),
})

const formSchema = z.intersection(passwordSchema, userSchema)
and not this?
const passwordSchema = z.object({
emailAddress: z.string().email(),
userName: z.string(),
userType: Z.string(),
firstName: z.string(),
lastName: z.string(),
password: z.string().min(8),
confirmPassword: z.string()
}).refine((data) => data.password === data.confirmPassword, {
message: "Password must match",
path: ["confirmPassword"]
})
const passwordSchema = z.object({
emailAddress: z.string().email(),
userName: z.string(),
userType: Z.string(),
firstName: z.string(),
lastName: z.string(),
password: z.string().min(8),
confirmPassword: z.string()
}).refine((data) => data.password === data.confirmPassword, {
message: "Password must match",
path: ["confirmPassword"]
})
note that the feature in question is the .refine(), the former works well and as expected, but the latter... it should work, but the .refine() is not triggered... can someone enlighten me with this?
Solution:
Message Not Public
Sign In & Join Server To View
Jump to solution
24 Replies
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
OMB
OMBOP12mo ago
can you reiterate, i don't understand what you mean by object im passing, forgive me
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
OMB
OMBOP12mo ago
ahh, but it does not run even if email is valid let me show you some samples
OMB
OMBOP12mo ago
this is when Im using the latter code,
No description
No description
OMB
OMBOP12mo ago
this is when I use the former. No errors on both, no warnings, no indicator what-so-ever
No description
No description
OMB
OMBOP12mo ago
to be honest i'm quite perplexed as to why separating the two schemas and joining them works
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
OMB
OMBOP12mo ago
this is the "not splitted" schema
No description
No description
OMB
OMBOP12mo ago
this is the "splitted" schema
No description
No description
OMB
OMBOP12mo ago
both seem to return false, which is intended this just leaves us now with the .refine()
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
OMB
OMBOP12mo ago
top one is the "not-splitted"
No description
OMB
OMBOP12mo ago
this is the error object on the top one expanded
No description
OMB
OMBOP12mo ago
this is for the bottom one
No description
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Scott Trinh
Scott Trinh12mo ago
Looks like your values are undefined here, which will fail to run the refine until the object passes validation.
OMB
OMBOP12mo ago
so this means that before refine will run, each object should pass the validation? i.e. firstname, lastname, etc. ?
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
OMB
OMBOP12mo ago
if that's the case, then it makes sense now since if we split the password validation from the other stuff, it would check on it's own, effectively ignoring the other validators that could prevent it from running the refine
Solution
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
OMB
OMBOP12mo ago
oh! now i see what you mean earlier, sorry for not being able to understand it
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
OMB
OMBOP12mo ago
thank you! I'm new to these kinds of stuff, I learned alot from this short exchange ❤️
Want results from more Discord servers?
Add your server