Skip to content

Commit

Permalink
fixed issue again
Browse files Browse the repository at this point in the history
  • Loading branch information
overthemike committed Sep 1, 2024
1 parent 1fb101d commit 9fb818d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
4 changes: 2 additions & 2 deletions examples/react/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ function App() {
<label htmlFor="age">Age</label>
<input
id="age"
type="number"
value={user.age}
type="text"
value={'' + user.age}
onChange={(e) => (userState.age = Number(e.target.value))}
/>
<p>Age: {user.age}</p>
Expand Down
27 changes: 17 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type SchemaMeta = SchemaConfig & {
type PropType = string | number | symbol;
const schemaMeta = new WeakMap<ZodType<any>, SchemaMeta>();
const pathList = new WeakMap<{}, PropType[]>();
const isProxySymbol = Symbol('isProxy');

type SchemaReturn<T extends ZodType<any>> = {
proxy: {
Expand Down Expand Up @@ -91,27 +92,33 @@ export const schema = <T extends ZodType<any>>(
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) {
const originalObject = schemaMeta.get(zodSchema)!
.initialState as z.infer<T>;

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) {
Expand All @@ -128,7 +135,7 @@ export const schema = <T extends ZodType<any>>(
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 {
Expand All @@ -137,8 +144,8 @@ export const schema = <T extends ZodType<any>>(
}
} else {
const parsedValue = zodSchema.parse(objectToValidate);
_.set(valtioProxy, pathToSet, value);
Reflect.set(target, prop, value, receiver);
valtioProxy[prop] = value;
return true;
}
} catch (error) {
Expand Down

0 comments on commit 9fb818d

Please sign in to comment.