Zod, compare two input fields #178424
Unanswered
Fred638
asked this question in
Programming Help
Replies: 3 comments 3 replies
This comment was marked as off-topic.
This comment was marked as off-topic.
-
|
Why was the comment removed? What was off-topic? It helped me, but I had a follow up question. Something not according to the guidelines? You people are too fast with "cleaning up". |
Beta Was this translation helpful? Give feedback.
1 reply
-
|
To compare the values of two input fields using Zod—ensuring the values are not the same (e.g., password and another field should differ)—you can use Zod’s import { z } from "zod";
const schema = z.object({
password: z.string(),
otherField: z.string(),
}).refine(
(data) => data.password !== data.otherField,
{
message: "The values cannot be the same.",
path: ["otherField"], // Error will appear on otherField
}
);Usage Example: const result = schema.safeParse({
password: "mySecret",
otherField: "mySecret",
});
console.log(result.success); // false
console.log(result.error?.issues); // Shows the custom errorExplanation:
Tip:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Body
How can one compare the values of two input fields using Zod? Similar to password and confirm password, but the opposite, meaning that the values cannot be the same?
Guidelines
Beta Was this translation helpful? Give feedback.
All reactions