4 ms·
Alternatively, don't validate and then use the original. Instead, pull out the acceptable input and use that. Even better, compare that to the original and fai
by sfink 3y ago
Alternatively, don't validate and then use the original. Instead, pull out the acceptable input and use that.
Even better, compare that to the original and fail validation if they're not identical, but that requires maintaining a higher level of paranoia than may be reasonable to expect.
- Akronymus 3y ago> Alternatively, don't validate and then use the original. Instead, pull out the acceptable input and use that. Parse don't validate https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/ https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-va...
- sfink 3y agoHeh. I wrote up my comment, and then thought "hey, I bet that's what that 'Parse don't validate' article meant, the one I never quite got around to reading." So I pulled it up — great article! — but then didn't post the link because it uses the type system to record the results of the parse. Whereas here, you'd probably parse from a string into another string. But philosophically I agree, that's exactly the relevant advice.
- Akronymus 3y agoparsing from a string to a string runs the risk of erroneously assigning the original value to the new string. Which kinda defeats the whole parsing, not validating. What would work is having a small object holding a readonly string which parses the original on creation, then becomes immutable.