Skip to content

Instantly share code, notes, and snippets.

@Gaurav8757
Created January 8, 2026 06:59
Show Gist options
  • Select an option

  • Save Gaurav8757/9acd0549c5f08030f170f189d705ed51 to your computer and use it in GitHub Desktop.

Select an option

Save Gaurav8757/9acd0549c5f08030f170f189d705ed51 to your computer and use it in GitHub Desktop.
ASYNC VALIDATION — Email Existence Check or other field you can add
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