addamsson - Hi everyone! 👋 I have a small prob...
Hi everyone! 👋 I have a small problem with Zod. In my code:
I have duplications because in
superRefine
I decode the string, but that decoded value can't be pushed over to transform
(I need to do the split
again). Is there a way to do away with the duplication?12 Replies
You can remove the refine all together, and instead do the checks in the transform. The transform gets the same
ctx
thing as second parameter, same as superRefine
.
Also, as an aside, I would highly recommend using a regex to test and extract the value from your command, rather than includes and split.
With a regex you can be a lot more exact and confident in what you expect to receive as commands
For example:
Unknown User•13mo ago
Message Not Public
Sign In & Join Server To View
Regex matches return an array, the first element being the whole match, and the next ones are the capturing groups. So it's just a matter of using plain ol' array destructuring and skipping the first element 😉
Unknown User•13mo ago
Message Not Public
Sign In & Join Server To View
Now you do 🧙♂️
thanks, i'll try this! i don't know regex well enough to use it confidently, that's why i'm not doing that
Just need to use it more then, and you'll become more confident 😉
Seriously, regardless of what people say about regex, it's an awesome tool to have in your toolbox. You don't need to know it super well, but some level of knowledge about it opens up a lot of things you can do with it. Great for advanced search-replace stuff in code-bases and files for example, validating and picking out stuff from strings (like this case), and other things
Personally, I bought https://www.regexbuddy.com years ago (maybe even decades ago), which I always have installed on my dev computers. Great for learning how to do things. I know there are some websites that offer similar features, but yeah, having it in a program on my computer has helped me a lot with getting more comfy with regex.
RegexBuddy: Learn, Create, Understand, Test, Use and Save Regular E...
RegexBuddy is your perfect companion for working with regular expressions. Learning, creating, understanding, testing, using and saving regular expressions has never been easier.
I'm reluctant to use it mostly because of security considerations (eg: catastrophic backtracking and its ilk)
the code i've written above is much easier to test
also, I only have to write validation code once in a blue moon, so it is not an issue
but i see your point nevertheless
Never bothered to care about that, hehe. Is that even an issue as long as you don't inject user-data into the regexes themselves?
Either way, even if you don't use regex in your application code, it's still a great tool to have as a developer, or even just as a computer user in general. I have saved a ton of time renaming files for example
i'll keep this in mind 👍
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View