A
arktype•2mo ago
SynthLuvr

ArkType mutates original input?

I was always under the assumption that ArkType leaves the original input intact. However:
import { type } from "arktype";

const original = { value: "100" };
const Parser = type({ value: type("string").pipe(BigInt) });
Parser.assert(original);
console.log(original);
import { type } from "arktype";

const original = { value: "100" };
const Parser = type({ value: type("string").pipe(BigInt) });
Parser.assert(original);
console.log(original);
Outputs:
{ value: 100n }
{ value: 100n }
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
ssalbdivad
ssalbdivad•2mo ago
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
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
ssalbdivad
ssalbdivad•2mo ago
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
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
ssalbdivad
ssalbdivad•2mo ago
Documentation definitely needs to be better- working on morphs now!
Unknown User
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
ssalbdivad
ssalbdivad•2mo ago
That seems more like a weakness of TS in general to me. Within ArkType, the outputs are precisely inferred
Unknown User
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
ssalbdivad
ssalbdivad•2mo ago
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
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
ssalbdivad
ssalbdivad•2mo ago
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
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
ssalbdivad
ssalbdivad•2mo ago
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
Unknown User•2mo ago
Message Not Public
Sign In & Join Server To View
ssalbdivad
ssalbdivad•2mo ago
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"
Want results from more Discord servers?
Add your server