1- import { createStore , type Store } from '@tanstack/store'
1+ import { batch , createStore } from '@tanstack/store'
22import {
33 isStandardSchemaValidator ,
44 standardSchemaValidators ,
55} from './standardSchemaValidator'
6- import { defaultFieldMeta } from './metaHelper'
76import {
87 determineFieldLevelErrorSourceAndValue ,
98 evaluate ,
@@ -14,6 +13,7 @@ import {
1413 mergeOpts ,
1514} from './utils'
1615import { defaultValidationLogic } from './ValidationLogic'
16+ import type { ReadonlyStore } from '@tanstack/store' ;
1717import type { DeepKeys , DeepValue , UnwrapOneLevelOfArray } from './util-types'
1818import type {
1919 StandardSchemaV1 ,
@@ -1091,7 +1091,7 @@ export class FieldApi<
10911091 /**
10921092 * The field state store.
10931093 */
1094- store ! : Store <
1094+ store ! : ReadonlyStore <
10951095 FieldState <
10961096 TParentData ,
10971097 TName ,
@@ -1709,62 +1709,62 @@ export class FieldApi<
17091709 // Needs type cast as eslint errantly believes this is always falsy
17101710 let hasErrored = false as boolean
17111711
1712- // batch(() => {
1713- const validateFieldFn = (
1714- field : AnyFieldApi ,
1715- validateObj : SyncValidator < any > ,
1716- ) => {
1717- const errorMapKey = getErrorMapKey ( validateObj . cause )
1712+ batch ( ( ) => {
1713+ const validateFieldFn = (
1714+ field : AnyFieldApi ,
1715+ validateObj : SyncValidator < any > ,
1716+ ) => {
1717+ const errorMapKey = getErrorMapKey ( validateObj . cause )
1718+
1719+ const fieldLevelError = validateObj . validate
1720+ ? normalizeError (
1721+ field . runValidator ( {
1722+ validate : validateObj . validate ,
1723+ value : {
1724+ value : field . store . state . value ,
1725+ validationSource : 'field' ,
1726+ fieldApi : field ,
1727+ } ,
1728+ type : 'validate' ,
1729+ } ) ,
1730+ )
1731+ : undefined
17181732
1719- const fieldLevelError = validateObj . validate
1720- ? normalizeError (
1721- field . runValidator ( {
1722- validate : validateObj . validate ,
1723- value : {
1724- value : field . store . state . value ,
1725- validationSource : 'field' ,
1726- fieldApi : field ,
1727- } ,
1728- type : 'validate' ,
1729- } ) ,
1730- )
1731- : undefined
1733+ const formLevelError = errorFromForm [ errorMapKey ]
17321734
1733- const formLevelError = errorFromForm [ errorMapKey ]
1735+ const { newErrorValue, newSource } =
1736+ determineFieldLevelErrorSourceAndValue ( {
1737+ formLevelError,
1738+ fieldLevelError,
1739+ } )
17341740
1735- const { newErrorValue, newSource } =
1736- determineFieldLevelErrorSourceAndValue ( {
1737- formLevelError,
1738- fieldLevelError,
1739- } )
1741+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1742+ if ( field . state . meta . errorMap ?. [ errorMapKey ] !== newErrorValue ) {
1743+ field . setMeta ( ( prev ) => ( {
1744+ ...prev ,
1745+ errorMap : {
1746+ ...prev . errorMap ,
1747+ [ errorMapKey ] : newErrorValue ,
1748+ } ,
1749+ errorSourceMap : {
1750+ ...prev . errorSourceMap ,
1751+ [ errorMapKey ] : newSource ,
1752+ } ,
1753+ } ) )
1754+ }
1755+ if ( newErrorValue ) {
1756+ hasErrored = true
1757+ }
1758+ }
17401759
1741- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1742- if ( field . state . meta . errorMap ?. [ errorMapKey ] !== newErrorValue ) {
1743- field . setMeta ( ( prev ) => ( {
1744- ...prev ,
1745- errorMap : {
1746- ...prev . errorMap ,
1747- [ errorMapKey ] : newErrorValue ,
1748- } ,
1749- errorSourceMap : {
1750- ...prev . errorSourceMap ,
1751- [ errorMapKey ] : newSource ,
1752- } ,
1753- } ) )
1760+ for ( const validateObj of validates ) {
1761+ validateFieldFn ( this , validateObj )
17541762 }
1755- if ( newErrorValue ) {
1756- hasErrored = true
1763+ for ( const fieldValitateObj of linkedFieldValidates ) {
1764+ if ( ! fieldValitateObj . validate ) continue
1765+ validateFieldFn ( fieldValitateObj . field , fieldValitateObj )
17571766 }
1758- }
1759-
1760- for ( const validateObj of validates ) {
1761- validateFieldFn ( this , validateObj )
1762- }
1763- for ( const fieldValitateObj of linkedFieldValidates ) {
1764- if ( ! fieldValitateObj . validate ) continue
1765- validateFieldFn ( fieldValitateObj . field , fieldValitateObj )
1766- }
1767- // })
1767+ } )
17681768
17691769 /**
17701770 * when we have an error for onSubmit in the state, we want
0 commit comments