@@ -37,12 +37,8 @@ function toeObj<TData extends Record<string, any>>(
37
37
for ( const key of Object . keys ( data ) ) {
38
38
const value = data [ key ] ;
39
39
if ( keys . includes ( key ) ) {
40
- // Guaranteed to have at least one entry
41
- const filteredErrors = errors . filter ( ( e ) => e . path [ depth ] === key ) ;
42
-
43
- if ( value === null ) {
44
- // CONSIDER: error wrap? E.g. so it's `instanceof Error`?
45
- const error = filteredErrors [ 0 ] ;
40
+ if ( value == null ) {
41
+ const error = errors . find ( ( e ) => e . path [ depth ] === key ) ;
46
42
// This is where the error is!
47
43
// obj[key] = value;
48
44
Object . defineProperty ( obj , key , {
@@ -52,12 +48,12 @@ function toeObj<TData extends Record<string, any>>(
52
48
} ,
53
49
} ) ;
54
50
} else {
51
+ // Guaranteed to have at least one entry
52
+ const filteredErrors = errors . filter ( ( e ) => e . path [ depth ] === key ) ;
55
53
// Recurse
56
- if ( Array . isArray ( value ) ) {
57
- obj [ key ] = toeArr ( value , depth + 1 , filteredErrors ) as any ;
58
- } else {
59
- obj [ key ] = toeObj ( value , depth + 1 , filteredErrors ) ;
60
- }
54
+ obj [ key ] = Array . isArray ( value )
55
+ ? ( toeArr ( value , depth + 1 , filteredErrors ) as any )
56
+ : toeObj ( value , depth + 1 , filteredErrors ) ;
61
57
}
62
58
} else {
63
59
obj [ key ] = value ;
@@ -77,12 +73,8 @@ function toeArr<TData>(
77
73
for ( let index = 0 , l = data . length ; index < l ; index ++ ) {
78
74
const value = data [ index ] ;
79
75
if ( keys . includes ( index ) ) {
80
- // Guaranteed to have at least one entry
81
- const filteredErrors = errors . filter ( ( e ) => e . path [ depth ] === index ) ;
82
-
83
- if ( value === null ) {
84
- // CONSIDER: error wrap? E.g. so it's `instanceof Error`?
85
- const error = filteredErrors [ 0 ] ;
76
+ if ( value == null ) {
77
+ const error = errors . find ( ( e ) => e . path [ depth ] === index ) ;
86
78
// This is where the error is!
87
79
// arr[index] = value;
88
80
Object . defineProperty ( arr , index , {
@@ -92,12 +84,12 @@ function toeArr<TData>(
92
84
} ,
93
85
} ) ;
94
86
} else {
87
+ // Guaranteed to have at least one entry
88
+ const filteredErrors = errors . filter ( ( e ) => e . path [ depth ] === index ) ;
95
89
// Recurse
96
- if ( Array . isArray ( value ) ) {
97
- arr [ index ] = toeArr ( value , depth + 1 , filteredErrors ) ;
98
- } else {
99
- arr [ index ] = toeObj ( value as any , depth + 1 , filteredErrors ) ;
100
- }
90
+ arr [ index ] = Array . isArray ( value )
91
+ ? toeArr ( value , depth + 1 , filteredErrors )
92
+ : toeObj ( value as any , depth + 1 , filteredErrors ) ;
101
93
}
102
94
} else {
103
95
arr [ index ] = value ;
0 commit comments