ArkType mutates original input?
I was always under the assumption that ArkType leaves the original input intact. However:
Outputs:
My assumption was that value would remain as string, and the result returned by
Parser
would be this value. But based on this, it seems like I was under the wrong assumption.46 Replies
Yeah there are quite a few discussions on this. If you want to avoid it, you can pipe to the clone operator of your choice. At some point I'll add a built-in config option to clone before morphing by default.
To me feels like a separation of concerns thing. Can be much faster without copying every object you validate, plus cloning can not always be done safely, so if it's going to happen best make it opt-in/semi-explicit.
Open to feedback on this though, potentially making the default to clone before morphing
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Yes this is often the case, but ArkType still supports parsing cyclic data which makes cloning much more complex and requires more assumptions to implement on our end.
It's also just much slower in many cases where most often you don't need the input once you've parsed it to the correct shape anyways
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Documentation definitely needs to be better- working on morphs now!
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
That seems more like a weakness of TS in general to me. Within ArkType, the outputs are precisely inferred
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Well I'll continue to listen to feedback on that. I'm sure it is unintuitive for people coming from most other validation libraries, but I want to make sure I that's not my primary motivation for the behavior I end up finalizing
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
Once there is a global config available for this I think it will mitigate it quite a lot. I am curious to hear more of the concrete cases where things have actually broken as a result of mutating the input-not just "I didn't expect this".
I've heard a few cases, but I'd guess the vast majority of the time once you parse the raw input, you're done with it
Haha I don't take it as harsh criticism there are a lot of trade offs here and it's good to hear peoples' intuitions about them
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
But something very similar applies here to your comment about prototypes etc.- that if it's internal data it's probably already been valdiated
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
I'm not sure about this, it accepts
data
of type unknown
- there's no way for now in TS to say "mutable or primitive"