Type/Data runtime validation on state change #921
Replies: 3 comments 5 replies
-
Just now found this: #297. Didn't look like it got much traction, though the implementation never seemed to get thought through. I'm wondering if there's really even a need to modify how proxy() works at all and maybe just put a new different proxy that handles this sort of thing as a layer in front of |
Beta Was this translation helpful? Give feedback.
-
Thinking out loud here...what if this kept it's own track of the structure of whatever state is passed in, creates the proxy for that state and stores the proxy(ies) as well so that when a property is mutated, it will validate the value based on it's own structure first, and if it passed, it then does the same mutation on the proxy object. Downside is that we would essentially be duplicating the store's structure (though not the state). Does that make sense? |
Beta Was this translation helpful? Give feedback.
-
So I looked into this a bit more in depth and have come to the conclusion that trying to implement generic validation that's not specific to a library might not be the best way to do this. Each library has their own rules and parsing methods and valtio's proxy methodology might work with some without issues with some libraries and might require extra code for others. I think it would either be overly-complicated to both use and maintain if we tried to implement generic work arounds for everything instead of just creating a different wrapper library for different validation libraries. I would probably prioritize Yup and Zod first as I believe they are probably the most widely used ones and then maybe a version that allows users to create their own custom validation logic. I actually have already started experimenting with something that I think would fit really well for that last one. I'd be happy to try and get things started once you send me an invite and we can probably move this discussion over there. |
Beta Was this translation helpful? Give feedback.
-
I had a thought of maybe adding a chainable "schema" function that could be defined before the proxy call that would allow for using something like zod to validate the values as they are changed and revert back to a previous state and/or throw an error if failed.
Here's a idea of what I mean:
I'm thinking the schema function could return a Proxy that will have have set traps to test the input before passing it on to the state proxy to do the rest of it's magic.
I haven't really thought through this too deeply so I'm not sure if it's really even feasible. Many heads are better than one.
Beta Was this translation helpful? Give feedback.
All reactions