@@ -36,6 +36,7 @@ type SchemaMeta = SchemaConfig & {
36
36
type PropType = string | number | symbol ;
37
37
const schemaMeta = new WeakMap < ZodType < any > , SchemaMeta > ( ) ;
38
38
const pathList = new WeakMap < { } , PropType [ ] > ( ) ;
39
+ const isProxySymbol = Symbol ( 'isProxy' ) ;
39
40
40
41
type SchemaReturn < T extends ZodType < any > > = {
41
42
proxy : {
@@ -91,27 +92,33 @@ export const schema = <T extends ZodType<any>>(
91
92
get ( target , prop , receiver ) {
92
93
const value = Reflect . get ( target , prop , receiver ) ;
93
94
if ( isObject ( value ) ) {
94
- const newPath = parentPath . concat ( prop ) ;
95
- pathList . set ( value , newPath ) ;
96
- return createProxy ( value , newPath ) ;
95
+ if ( ( value as any ) [ isProxySymbol ] ) {
96
+ return value ;
97
+ } else {
98
+ const newPath = parentPath . concat ( prop ) ;
99
+ pathList . set ( value , newPath ) ;
100
+ const proxyObj = createProxy ( value , newPath ) ;
101
+ proxyObj [ isProxySymbol ] = true ;
102
+ return proxyObj ;
103
+ }
97
104
} else {
98
- const pathToSet = [ ...( pathList . get ( target ) || [ ] ) , prop ] ;
99
- return _ . get ( valtioProxy , pathToSet , value ) ;
105
+ const pathToGet = [ ...( pathList . get ( target ) || [ ] ) , prop ] ;
106
+ return _ . get ( valtioProxy , pathToGet , value ) ;
100
107
}
101
108
} ,
102
109
set ( target , prop , value , receiver ) {
103
110
const originalObject = schemaMeta . get ( zodSchema ) !
104
111
. initialState as z . infer < T > ;
105
112
106
113
const objectToValidate = _ . cloneDeep ( originalObject ) ;
107
- const path = ( pathList . get ( target ) || [ ] ) . concat ( prop ) ;
114
+ const pathToSet = [ ... ( pathList . get ( target ) || [ ] ) , prop ] ;
108
115
109
- _ . set ( objectToValidate , path , value ) ;
116
+ _ . set ( objectToValidate , pathToSet , value ) ;
110
117
111
118
const handleAsyncParse = async ( ) => {
112
119
try {
113
120
const parsedValue = await zodSchema . parseAsync ( objectToValidate ) ;
114
- _ . set ( valtioProxy , value , path ) ;
121
+ _ . set ( valtioProxy , pathToSet , value ) ;
115
122
Reflect . set ( target , prop , value , receiver ) ;
116
123
return true ;
117
124
} catch ( error ) {
@@ -128,7 +135,7 @@ export const schema = <T extends ZodType<any>>(
128
135
if ( safeParse ) {
129
136
const result = zodSchema . safeParse ( objectToValidate ) ;
130
137
if ( result . success ) {
131
- valtioProxy [ prop ] = value ;
138
+ _ . set ( valtioProxy , pathToSet , value ) ;
132
139
Reflect . set ( target , prop , value , receiver ) ;
133
140
return true ;
134
141
} else {
@@ -137,8 +144,8 @@ export const schema = <T extends ZodType<any>>(
137
144
}
138
145
} else {
139
146
const parsedValue = zodSchema . parse ( objectToValidate ) ;
147
+ _ . set ( valtioProxy , pathToSet , value ) ;
140
148
Reflect . set ( target , prop , value , receiver ) ;
141
- valtioProxy [ prop ] = value ;
142
149
return true ;
143
150
}
144
151
} catch ( error ) {
0 commit comments