@@ -49,7 +49,8 @@ import {
4949 setPaths ,
5050 pathExists ,
5151 type ZodTypeInfo ,
52- traversePaths
52+ traversePaths ,
53+ isInvalidPath
5354} from '../traversal.js' ;
5455import { fieldProxy } from './proxies.js' ;
5556import { clone } from '../utils.js' ;
@@ -642,7 +643,19 @@ export function superForm<
642643 const errorContent = get ( Errors ) ;
643644
644645 const errorNode = errorContent
645- ? pathExists ( errorContent , path )
646+ ? pathExists ( errorContent , path , {
647+ modifier : ( pathData ) => {
648+ // Check if we have found a string in an error array.
649+ if ( isInvalidPath ( path , pathData ) ) {
650+ throw new SuperFormError (
651+ 'Errors can only be added to form fields, not to arrays or objects in the schema. Path: ' +
652+ pathData . path . slice ( 0 , - 1 )
653+ ) ;
654+ }
655+
656+ return pathData . value ;
657+ }
658+ } )
646659 : undefined ;
647660
648661 // Need a special check here, since if the error has never existed,
@@ -1089,7 +1102,12 @@ async function validateField<T extends AnyZodObject, M>(
10891102 errors ,
10901103 path as FieldPath < typeof errors > ,
10911104 ( node ) => {
1092- if ( node . value === undefined ) {
1105+ if ( isInvalidPath ( path , node ) ) {
1106+ throw new SuperFormError (
1107+ 'Errors can only be added to form fields, not to arrays or objects in the schema. Path: ' +
1108+ node . path . slice ( 0 , - 1 )
1109+ ) ;
1110+ } else if ( node . value === undefined ) {
10931111 node . parent [ node . key ] = { } ;
10941112 return node . parent [ node . key ] ;
10951113 } else {
@@ -1251,7 +1269,7 @@ async function validateField<T extends AnyZodObject, M>(
12511269 // We validated the whole data structure, so clear all errors on success
12521270 // but also set the current path to undefined, so it will be used in the tainted+error
12531271 // check in oninput.
1254- Errors_clear ( { undefinePath : path , clearFormLevelErrors : false } ) ;
1272+ Errors_clear ( { undefinePath : path , clearFormLevelErrors : true } ) ;
12551273 return undefined ;
12561274 }
12571275 } else {
0 commit comments