builders icon indicating copy to clipboard operation
builders copied to clipboard

Validation errors reported by zod are not useful

Open Fleny113 opened this issue 3 years ago • 10 comments

Issue description

Calling toJSON() from an instance of a SlashCommandBuilder without setting required parameters (name and description) throw an error that seem from zod that isn't useful at all to understand the problem because of the size of the error and don't say what parameter is missing.

Original message: When creating a SlashCommandBuilder and doing toJSON() zod thrown about an recived undefined and expect a string.

Step to reproduce:

  1. Import SlashCommandBuilder from @discordjs/builders
  2. Create a new Builder and call toJSON()
  3. zod should throw an error that isn't useful at all

zodError.log

Code sample

import { SlashCommandBuilder } from "@discordjs/builders";
new SlashCommandBuilder().setName("test").toJSON();

@discordjs/builders version

0.9.0

Node.js version

node.js v17.2.0 | Typescript 4.5.4

Operating system

Windows 11

Priority this issue should have

High (immediate attention needed)

Fleny113 avatar Dec 16 '21 17:12 Fleny113

Is your code..minified/bundled at all? Because it definitely throws an error, as seen at the end, but a really, REALLY not helpful one

vladfrangu avatar Dec 16 '21 18:12 vladfrangu

The issue is you didn't provide a description but the description is required.

Khasms avatar Dec 16 '21 20:12 Khasms

Is your code..minified/bundled at all? Because it definitely throws an error, as seen at the end, but a really, REALLY not helpful one

No, i having this issue from typescript (and tsc) but i tried directly from nodejs and still throw error

The issue is you didn't provide a description but the description is required.

Sorry, i added it.

Fleny113 avatar Dec 16 '21 21:12 Fleny113

That's not what @Khasms meant haha. You need to call .setDescription on the builder you created! For further questions, feel free to ask in our Discord server

vladfrangu avatar Dec 16 '21 21:12 vladfrangu

That's not what @Khasms meant haha. You need to call .setDescription on the builder you created! For further questions, feel free to ask in our Discord server

I known that is not he mean but really, i don't known what to write, the errore is useless. Anyway, thanks for help.

Fleny113 avatar Dec 17 '21 06:12 Fleny113

Hrmm, true true, the errors could be better.. I'll be reopening this but with a different title 👍

vladfrangu avatar Dec 17 '21 13:12 vladfrangu

Hrmm, true true, the errors could be better.. I'll be reopening this but with a different title 👍

I updated the description to made that more consistent with title

Fleny113 avatar Dec 17 '21 15:12 Fleny113

Hi there, one possible alternative is to wrap parsing with a custom error that unsets the stacktrace, resulting in the below:

image

I'm willing to PR this if it seems like a good tradeoff. ValidationError can also be exported so it can be caught by consumers.

hampuskraft avatar Dec 17 '21 19:12 hampuskraft

I can't reproduce the code dump at the beginning of the error, it outputs as it should on my end. But, as far as the error not being helpful, I opened #61 a week ago to improve them, so give some feedback on that if you see any other messages that need to be improved.

Khasms avatar Dec 17 '21 23:12 Khasms

@hampuskraft Not a good tradeoff since that makes parsing +2x slower, which adds up to how slow Zod's validation already is, see https://github.com/discordjs/discord.js/pull/7067#issuecomment-989502034

kyranet avatar Dec 19 '21 06:12 kyranet