@@ -892,27 +892,28 @@ async function validateField<T extends AnyZodObject, M>(
892892 }
893893
894894 let value = options . value ;
895+ let shouldUpdate = true ;
895896 const currentData = get ( data ) ;
896897
897898 if ( ! ( 'value' in options ) ) {
899+ // Use value from data
898900 const dataToValidate = traversePath (
899901 currentData ,
900902 path as FieldPath < typeof currentData >
901903 ) ;
902904
903- if ( ! dataToValidate ) {
904- throw new SuperFormError ( 'Validation data not found: ' + path ) ;
905- }
906-
907- value = dataToValidate . value ;
905+ value = dataToValidate ?. value ;
908906 } else if ( options . update === true || options . update === 'value' ) {
907+ // Value should be updating the data
909908 data . update (
910909 ( $data ) => {
911910 setPaths ( $data , [ path ] , value ) ;
912911 return $data ;
913912 } ,
914913 { taint : options . taint }
915914 ) ;
915+ } else {
916+ shouldUpdate = false ;
916917 }
917918
918919 //console.log('🚀 ~ file: index.ts:871 ~ validate:', path, value);
@@ -951,7 +952,7 @@ async function validateField<T extends AnyZodObject, M>(
951952 if ( 'safeParseAsync' in validators ) {
952953 // Zod validator
953954 // Check if any effects exist for the path, then parse the entire schema.
954- const leaf = traversePath (
955+ const noEffects = traversePath (
955956 validators ,
956957 validationPath as FieldPath < typeof validators > ,
957958 ( pathData ) => {
@@ -962,10 +963,10 @@ async function validateField<T extends AnyZodObject, M>(
962963 }
963964 ) ;
964965
965- if ( leaf ) {
966+ if ( noEffects ) {
966967 const validator = extractValidator (
967- unwrapZodType ( leaf . parent ) ,
968- leaf . key
968+ unwrapZodType ( noEffects . parent ) ,
969+ noEffects . key
969970 ) ;
970971 if ( validator ) {
971972 //console.log('🚀 ~ file: index.ts:972 ~ no effects:', validator);
@@ -982,14 +983,22 @@ async function validateField<T extends AnyZodObject, M>(
982983 //console.log('🚀 ~ file: index.ts:983 ~ Effects found, validating all');
983984
984985 // Effects are found, validate entire data, unfortunately
986+ let dataToValidate = currentData ;
987+
988+ if ( ! shouldUpdate ) {
989+ // If value shouldn't update, clone and set the new value
990+ dataToValidate = clone ( currentData ) ;
991+ setPaths ( dataToValidate , [ path ] , value ) ;
992+ }
993+
985994 const result = await ( validators as ZodTypeAny ) . safeParseAsync (
986- get ( data )
995+ dataToValidate
987996 ) ;
988997
989998 if ( ! result . success ) {
990999 const errors = result . error . format ( ) ;
9911000 const current = traversePath ( errors , path as FieldPath < typeof errors > ) ;
992- return setError ( current ?. value ?. _errors ) ;
1001+ return setError ( options . errors ?? current ?. value ?. _errors ) ;
9931002 } else {
9941003 return setError ( undefined ) ;
9951004 }
0 commit comments