@tanstack/form-core 1.6.2 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/types.ts CHANGED
@@ -40,6 +40,17 @@ export type ValidationErrorMap<
40
40
  onServer?: TOnServerReturn
41
41
  }
42
42
 
43
+ /**
44
+ * @private allows tracking the source of the errors in the error map
45
+ */
46
+ export type ValidationErrorMapSource = {
47
+ onMount?: ValidationSource
48
+ onChange?: ValidationSource
49
+ onBlur?: ValidationSource
50
+ onSubmit?: ValidationSource
51
+ onServer?: ValidationSource
52
+ }
53
+
43
54
  /**
44
55
  * @private
45
56
  */
package/src/utils.ts CHANGED
@@ -1,4 +1,9 @@
1
- import type { GlobalFormValidationError, ValidationCause } from './types'
1
+ import type {
2
+ GlobalFormValidationError,
3
+ ValidationCause,
4
+ ValidationError,
5
+ ValidationSource,
6
+ } from './types'
2
7
  import type { FormValidators } from './FormApi'
3
8
  import type { AnyFieldMeta, FieldValidators } from './FieldApi'
4
9
 
@@ -376,3 +381,65 @@ export function shallow<T>(objA: T, objB: T) {
376
381
  }
377
382
  return true
378
383
  }
384
+
385
+ /**
386
+ * Determines the logic for determining the error source and value to set on the field meta within the form level sync/async validation.
387
+ * @private
388
+ */
389
+ export const determineFormLevelErrorSourceAndValue = ({
390
+ newFormValidatorError,
391
+ isPreviousErrorFromFormValidator,
392
+ previousErrorValue,
393
+ }: {
394
+ newFormValidatorError: ValidationError
395
+ isPreviousErrorFromFormValidator: boolean
396
+ previousErrorValue: ValidationError
397
+ }): {
398
+ newErrorValue: ValidationError
399
+ newSource: ValidationSource | undefined
400
+ } => {
401
+ // All falsy values are not considered errors
402
+ if (newFormValidatorError) {
403
+ return { newErrorValue: newFormValidatorError, newSource: 'form' }
404
+ }
405
+
406
+ // Clears form level error since it's now stale
407
+ if (isPreviousErrorFromFormValidator) {
408
+ return { newErrorValue: undefined, newSource: undefined }
409
+ }
410
+
411
+ // At this point, we have a preivous error which must have been set by the field validator, keep as is
412
+ if (previousErrorValue) {
413
+ return { newErrorValue: previousErrorValue, newSource: 'field' }
414
+ }
415
+
416
+ // No new or previous error, clear the error
417
+ return { newErrorValue: undefined, newSource: undefined }
418
+ }
419
+
420
+ /**
421
+ * Determines the logic for determining the error source and value to set on the field meta within the field level sync/async validation.
422
+ * @private
423
+ */
424
+ export const determineFieldLevelErrorSourceAndValue = ({
425
+ formLevelError,
426
+ fieldLevelError,
427
+ }: {
428
+ formLevelError: ValidationError
429
+ fieldLevelError: ValidationError
430
+ }): {
431
+ newErrorValue: ValidationError
432
+ newSource: ValidationSource | undefined
433
+ } => {
434
+ // At field level, we prioritize the field level error
435
+ if (fieldLevelError) {
436
+ return { newErrorValue: fieldLevelError, newSource: 'field' }
437
+ }
438
+
439
+ // If there is no field level error, and there is a form level error, we set the form level error
440
+ if (formLevelError) {
441
+ return { newErrorValue: formLevelError, newSource: 'form' }
442
+ }
443
+
444
+ return { newErrorValue: undefined, newSource: undefined }
445
+ }