Created
January 8, 2026 06:59
-
-
Save Gaurav8757/9acd0549c5f08030f170f189d705ed51 to your computer and use it in GitHub Desktop.
ASYNC VALIDATION — Email Existence Check or other field you can add
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import api from "./api"; // your axios instance | |
| export const checkEmailExists = async (email) => { | |
| try { | |
| const res = await api.post("/auth/check-email", { email }); | |
| return res.data.exists; // true / false | |
| } catch (e) { | |
| return false; | |
| } | |
| }; | |
| // zod message | |
| import { z } from "zod"; | |
| import { checkEmailExists } from "../services/validatorService"; | |
| export const UserSchema = z.object({ | |
| fullName: z.string().min(2, "Name is too short"), | |
| email: z | |
| .string() | |
| .email("Invalid email") | |
| .refine( | |
| async (val) => { | |
| const exists = await checkEmailExists(val); | |
| return !exists; // return false for error | |
| }, | |
| { | |
| message: "Email already exists", | |
| } | |
| ), | |
| password: z.string().min(6, "Password too short"), | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment