auto-form-validator
Version:
A smart, zero-setup form validation library for React with automatic rule detection, i18n support, async validation, and UI integration helpers.
25 lines (24 loc) • 771 B
JavaScript
// src/utils/asyncValidators.ts
var createUsernameAvailableValidator = (checkFn, message) => {
return async (username) => {
if (!username.trim()) return null;
try {
const isAvailable = await checkFn(username);
return isAvailable ? null : message || "Username is already taken";
} catch {
return null;
}
};
};
var createEmailDomainValidator = (allowedDomains, message) => {
return async (email) => {
if (!email.includes("@")) return null;
const domain = email.split("@")[1].toLowerCase();
const isAllowed = allowedDomains.some((d) => d.toLowerCase() === domain);
return isAllowed ? null : message || "Email domain is not allowed";
};
};
export {
createEmailDomainValidator,
createUsernameAvailableValidator
};