MCPcopy
hub / github.com/final-form/final-form / runValidation

Function runValidation

src/FinalForm.ts:400–555  ·  view source on GitHub ↗
(fieldChanged: string | undefined, callback: () => void)

Source from the content-addressed store, hash-verified

398 };
399
400 const runValidation = (fieldChanged: string | undefined, callback: () => void) => {
401 if (validationPaused) {
402 validationBlocked = true;
403 callback();
404 return;
405 }
406
407 const { fields, formState } = state;
408 const safeFields = { ...fields };
409 let fieldKeys = Object.keys(safeFields);
410 if (
411 !validate &&
412 !fieldKeys.some((key) => getValidators(safeFields[key]).length)
413 ) {
414 callback();
415 return; // no validation rules
416 }
417
418 // pare down field keys to actually validate
419 let limitedFieldLevelValidation = false;
420 if (fieldChanged) {
421 const changedField = safeFields[fieldChanged];
422 if (changedField) {
423 const { validateFields } = changedField;
424 if (validateFields) {
425 limitedFieldLevelValidation = true;
426 fieldKeys = validateFields.length
427 ? validateFields.concat(fieldChanged)
428 : [fieldChanged];
429 }
430 }
431 }
432
433 let recordLevelErrors: AnyObject = {};
434 let asyncRecordLevelErrors: AnyObject = {};
435 const fieldLevelErrors: Record<string, any> = {};
436
437 const promises = [
438 ...runRecordLevelValidation((errors, wasAsync: boolean) => {
439 if (wasAsync) {
440 asyncRecordLevelErrors = errors || {};
441 } else {
442 recordLevelErrors = errors || {};
443 }
444 }),
445 ...fieldKeys.reduce(
446 (result, name) =>
447 result.concat(
448 runFieldLevelValidation(fields[name], (error: any) => {
449 fieldLevelErrors[name] = error;
450 }),
451 ),
452 [],
453 ),
454 ];
455
456 const hasAsyncValidations = promises.length > 0;
457 const asyncValidationPromiseKey = ++nextAsyncValidationKey;

Callers 2

getMutatorApiFunction · 0.85
createFormFunction · 0.85

Calls 6

callbackFunction · 0.85
getValidatorsFunction · 0.85
runRecordLevelValidationFunction · 0.85
runFieldLevelValidationFunction · 0.85
processErrorsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…