Skip to content

Commit 9fb818d

Browse files
committed
fixed issue again
1 parent 1fb101d commit 9fb818d

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

examples/react/src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ function App() {
4747
<label htmlFor="age">Age</label>
4848
<input
4949
id="age"
50-
type="number"
51-
value={user.age}
50+
type="text"
51+
value={'' + user.age}
5252
onChange={(e) => (userState.age = Number(e.target.value))}
5353
/>
5454
<p>Age: {user.age}</p>

src/index.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type SchemaMeta = SchemaConfig & {
3636
type PropType = string | number | symbol;
3737
const schemaMeta = new WeakMap<ZodType<any>, SchemaMeta>();
3838
const pathList = new WeakMap<{}, PropType[]>();
39+
const isProxySymbol = Symbol('isProxy');
3940

4041
type SchemaReturn<T extends ZodType<any>> = {
4142
proxy: {
@@ -91,27 +92,33 @@ export const schema = <T extends ZodType<any>>(
9192
get(target, prop, receiver) {
9293
const value = Reflect.get(target, prop, receiver);
9394
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+
}
97104
} 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);
100107
}
101108
},
102109
set(target, prop, value, receiver) {
103110
const originalObject = schemaMeta.get(zodSchema)!
104111
.initialState as z.infer<T>;
105112

106113
const objectToValidate = _.cloneDeep(originalObject);
107-
const path = (pathList.get(target) || []).concat(prop);
114+
const pathToSet = [...(pathList.get(target) || []), prop];
108115

109-
_.set(objectToValidate, path, value);
116+
_.set(objectToValidate, pathToSet, value);
110117

111118
const handleAsyncParse = async () => {
112119
try {
113120
const parsedValue = await zodSchema.parseAsync(objectToValidate);
114-
_.set(valtioProxy, value, path);
121+
_.set(valtioProxy, pathToSet, value);
115122
Reflect.set(target, prop, value, receiver);
116123
return true;
117124
} catch (error) {
@@ -128,7 +135,7 @@ export const schema = <T extends ZodType<any>>(
128135
if (safeParse) {
129136
const result = zodSchema.safeParse(objectToValidate);
130137
if (result.success) {
131-
valtioProxy[prop] = value;
138+
_.set(valtioProxy, pathToSet, value);
132139
Reflect.set(target, prop, value, receiver);
133140
return true;
134141
} else {
@@ -137,8 +144,8 @@ export const schema = <T extends ZodType<any>>(
137144
}
138145
} else {
139146
const parsedValue = zodSchema.parse(objectToValidate);
147+
_.set(valtioProxy, pathToSet, value);
140148
Reflect.set(target, prop, value, receiver);
141-
valtioProxy[prop] = value;
142149
return true;
143150
}
144151
} catch (error) {

0 commit comments

Comments
 (0)