Z
Zod13mo ago
addamsson

addamsson - Hi everyone! 👋 I have a small prob...

Hi everyone! 👋 I have a small problem with Zod. In my code:
z
.string()
.superRefine((value, ctx) => {
if (value.includes(":") === false) {
ctx.addIssue(wrongFormatIssue);
return;
}
const [commandName, userId, teamId] = value.split(":");
if (!commandName || !userId || !teamId) {
ctx.addIssue(wrongFormatIssue);
}
})
.transform((value) => {
const [commandName, userId, teamId] = value.split(":");
return {
commandName,
userId,
teamId,
};
})
.pipe(
z.object({
commandName: z.string(),
userId: z.string().uuid(),
teamId: z.string().uuid(),
})
)
z
.string()
.superRefine((value, ctx) => {
if (value.includes(":") === false) {
ctx.addIssue(wrongFormatIssue);
return;
}
const [commandName, userId, teamId] = value.split(":");
if (!commandName || !userId || !teamId) {
ctx.addIssue(wrongFormatIssue);
}
})
.transform((value) => {
const [commandName, userId, teamId] = value.split(":");
return {
commandName,
userId,
teamId,
};
})
.pipe(
z.object({
commandName: z.string(),
userId: z.string().uuid(),
teamId: z.string().uuid(),
})
)
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
Svish
Svish13mo ago
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:
const result = value.match(/^([a-z]+):(\d+):(\d+)$/)

if (result == null) {
ctx.addIssue(...)
return z.NEVER
}

const [, commandName, userId, teamId] = result
return { commandName, userId, teamId }
const result = value.match(/^([a-z]+):(\d+):(\d+)$/)

if (result == null) {
ctx.addIssue(...)
return z.NEVER
}

const [, commandName, userId, teamId] = result
return { commandName, userId, teamId }
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Svish
Svish13mo ago
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
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Svish
Svish13mo ago
Now you do 🧙‍♂️
addamsson
addamssonOP12mo ago
thanks, i'll try this! i don't know regex well enough to use it confidently, that's why i'm not doing that
Svish
Svish12mo ago
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
Svish
Svish12mo ago
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.
addamsson
addamssonOP12mo ago
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
Svish
Svish12mo ago
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
addamsson
addamssonOP12mo ago
i'll keep this in mind 👍
Unknown User
Unknown User12mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server