huntabyte / shadcn-svelte

shadcn/ui, but for Svelte. ✨
https://shadcn-svelte.com
MIT License
4.13k stars 259 forks source link

Form.Errors will only work if the message is thrown and excludes refinement from zod #1047

Closed rboddy closed 1 month ago

rboddy commented 1 month ago

Describe the bug

If you use refinement to send back a message it doesn't throw, it returns a falsy value as documented here.

Expected Behavior: the message from a refinement is displayed as some kind of message on the frontend, possibly an error

Actual Behavior: My form fields just reset if the refinement requirements are not met, and no message is thrown

I can get around this by creating some kind of custom message on the frontend that is triggered, but I'd prefer the validation takes care of it. I wasn't sure if this was a bug or a feature request, but I thought it'd be more helpful to write it up like a bug.

Reproduction

Sample Schema Code:

export const sessionFormSchema = z
    .object({
        title: z.string().min(5).max(50),
        description: z.string().max(200),
        start: z.coerce.date().refine((data) => data > new Date(), { message: "Date must be in the future" }),
    end: z.coerce.date(),
    })
    .refine((data) => data.start > data.end, {
        message: "That's not how time works",
       });

Logs

No response

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish)
    CPU: (20) x64 Intel(R) Core(TM) i9-10850K CPU @ 3.60GHz
    Memory: 12.98 GB / 15.58 GB
    Container: Yes
    Shell: 5.8.1 - /usr/bin/zsh
  Binaries:
    Node: 20.11.1 - ~/.nvm/versions/node/v20.11.1/bin/node
    npm: 10.2.4 - ~/.nvm/versions/node/v20.11.1/bin/npm
  npmPackages:
    @sveltejs/kit: ^2.0.0 => 2.5.7
    bits-ui: ^0.21.4 => 0.21.4
    formsnap: ^1.0.0 => 1.0.0
    lucide-svelte: ^0.372.0 => 0.372.0
    mode-watcher: ^0.3.0 => 0.3.0
    svelte: ^4.2.7 => 4.2.15
    sveltekit-superforms: ^2.12.6 => 2.12.6

The linux is truish, it's WSL

Severity

annoyance

huntabyte commented 1 month ago

You can apply that message to a specific field which will then show the message.

rboddy commented 1 month ago

Ah, using ?

huntabyte commented 1 month ago

Yes, usually what I'll do is just put the error on one of the fields, whichever one is most relevant, in this case probably the end field using a custom error path, described here: https://zod.dev/?id=customize-error-path

rboddy commented 1 month ago

Gotcha, thanks for the explanation, I'll go ahead and close this