Search for a command to run...
Last updated June 9, 2026
Ctrovalidate provides a first-class asynchronous validation engine. It handles remote checks (like username availability or ZIP code lookups) with race-condition protection and automatic cancellation of stale requests.
Async rules return a Promise<boolean>. The engine ensures:
AbortController is aborted.pendingClass (browser) or isValidating flags (framework hooks).awaits each async rule before moving to the next.AbortSignal PatternEvery async rule receives an AbortSignal as its 4th parameter. Pass this to fetch so that stale requests are canceled when the field is re-validated.
import { Ctrovalidate } from 'ctrovalidate-browser';
Ctrovalidate.addAsyncRule('checkUsername', async (value, params, element, signal) => {
try {
const response = await fetch(`/api/check?u=${value}`, { signal });
const { available } = await response.json();
return available;
} catch (error) {
// AbortError is expected when re-validation cancels the previous request
if (error.name === 'AbortError') return true;
return false;
}
});AbortErrortrue (no false error shown)The core package provides validateValueAsync and validateAsync for server-side or framework use. Custom rules are passed via the customRules option:
import { validateAsync } from 'ctrovalidate-core';
const results = await validateAsync(
{ username: 'john' },
{ username: 'required|checkUsername' },
{
customRules: {
checkUsername: async (value, params, ctx, signal) => {
const res = await fetch(`/api/check?u=${value}`, { signal });
return (await res.json()).available;
},
},
}
);signal to fetch or other abortable APIs.pendingClass (browser) or isValidating state (React, Vue, Svelte).fetch calls with AbortSignal.timeout() if your server might be slow.AbortError and return true to prevent false error messages.