diff --git a/examples/react/src/App.tsx b/examples/react/src/App.tsx
index d564387..51e5800 100644
--- a/examples/react/src/App.tsx
+++ b/examples/react/src/App.tsx
@@ -47,8 +47,8 @@ function App() {
(userState.age = Number(e.target.value))}
/>
Age: {user.age}
diff --git a/src/index.ts b/src/index.ts
index a4befd2..0bc1f74 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -36,6 +36,7 @@ type SchemaMeta = SchemaConfig & {
type PropType = string | number | symbol;
const schemaMeta = new WeakMap, SchemaMeta>();
const pathList = new WeakMap<{}, PropType[]>();
+const isProxySymbol = Symbol('isProxy');
type SchemaReturn> = {
proxy: {
@@ -91,12 +92,18 @@ export const schema = >(
get(target, prop, receiver) {
const value = Reflect.get(target, prop, receiver);
if (isObject(value)) {
- const newPath = parentPath.concat(prop);
- pathList.set(value, newPath);
- return createProxy(value, newPath);
+ if ((value as any)[isProxySymbol]) {
+ return value;
+ } else {
+ const newPath = parentPath.concat(prop);
+ pathList.set(value, newPath);
+ const proxyObj = createProxy(value, newPath);
+ proxyObj[isProxySymbol] = true;
+ return proxyObj;
+ }
} else {
- const pathToSet = [...(pathList.get(target) || []), prop];
- return _.get(valtioProxy, pathToSet, value);
+ const pathToGet = [...(pathList.get(target) || []), prop];
+ return _.get(valtioProxy, pathToGet, value);
}
},
set(target, prop, value, receiver) {
@@ -104,14 +111,14 @@ export const schema = >(
.initialState as z.infer;
const objectToValidate = _.cloneDeep(originalObject);
- const path = (pathList.get(target) || []).concat(prop);
+ const pathToSet = [...(pathList.get(target) || []), prop];
- _.set(objectToValidate, path, value);
+ _.set(objectToValidate, pathToSet, value);
const handleAsyncParse = async () => {
try {
const parsedValue = await zodSchema.parseAsync(objectToValidate);
- _.set(valtioProxy, value, path);
+ _.set(valtioProxy, pathToSet, value);
Reflect.set(target, prop, value, receiver);
return true;
} catch (error) {
@@ -128,7 +135,7 @@ export const schema = >(
if (safeParse) {
const result = zodSchema.safeParse(objectToValidate);
if (result.success) {
- valtioProxy[prop] = value;
+ _.set(valtioProxy, pathToSet, value);
Reflect.set(target, prop, value, receiver);
return true;
} else {
@@ -137,8 +144,8 @@ export const schema = >(
}
} else {
const parsedValue = zodSchema.parse(objectToValidate);
+ _.set(valtioProxy, pathToSet, value);
Reflect.set(target, prop, value, receiver);
- valtioProxy[prop] = value;
return true;
}
} catch (error) {