Skip to main content

AsyncValidatorFn

type AsyncValidatorFn<T> = (value, options?) => Promise<ValidationError | null>;

Defined in: core/types/index.ts:81

Асинхронная функция Π²Π°Π»ΠΈΠ΄Π°Ρ†ΠΈΠΈ

Type Parameters​

T​

T = FormValue

Parameters​

value​

T

Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ для Π²Π°Π»ΠΈΠ΄Π°Ρ†ΠΈΠΈ

options?​

AsyncValidatorOptions

ΠžΠΏΡ†ΠΈΠΈ Π²Π°Π»ΠΈΠ΄Π°Ρ†ΠΈΠΈ (ΠΎΠΏΡ†ΠΈΠΎΠ½Π°Π»ΡŒΠ½ΠΎ)

Returns​

Promise<ValidationError | null>

Promise с ошибкой Π²Π°Π»ΠΈΠ΄Π°Ρ†ΠΈΠΈ ΠΈΠ»ΠΈ null Ссли Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π²Π°Π»ΠΈΠ΄Π½ΠΎ

Example​

// ΠŸΡ€ΠΎΡΡ‚ΠΎΠΉ Π²Π°Π»ΠΈΠ΄Π°Ρ‚ΠΎΡ€ (Π±Π΅Π· ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠΈ ΠΎΡ‚ΠΌΠ΅Π½Ρ‹)
const emailExists: AsyncValidatorFn<string> = async (value) => {
const exists = await checkEmail(value);
return exists ? { code: 'exists', message: 'Email already exists' } : null;
};

// Π’Π°Π»ΠΈΠ΄Π°Ρ‚ΠΎΡ€ с ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠΎΠΉ ΠΎΡ‚ΠΌΠ΅Π½Ρ‹
const emailExistsAbortable: AsyncValidatorFn<string> = async (value, options) => {
const exists = await fetch(`/api/check-email?email=${value}`, {
signal: options?.signal // ΠŸΠ΅Ρ€Π΅Π΄Π°Ρ‘ΠΌ signal Π² fetch для ΠΎΡ‚ΠΌΠ΅Π½Ρ‹ запроса
});
return exists ? { code: 'exists', message: 'Email already exists' } : null;
};