Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Showing validation issue details in REPL #4

Open
jasalt opened this issue Nov 28, 2024 · 3 comments
Open

Showing validation issue details in REPL #4

jasalt opened this issue Nov 28, 2024 · 3 comments

Comments

@jasalt
Copy link

jasalt commented Nov 28, 2024

I had trouble in REPL not seeing any more detailed validation issue information than just ZodError without any message.

If I understood, the thrown ZodError exception would have to be caught and handled, or safe-parse output be checked instead, both manually, making interactive development not so convenient with the library.

I did not save a screenshot or anything as I was in a hurry switching from the library to a DIY solution, but can add extra details about reproducing if it can be of help.

@smeghead
Copy link
Member

smeghead commented Dec 1, 2024

@jasalt
Thank you.
Currently, the exception message is a fixed string “Zod Error”.
Certainly there is not enough information in the REPL.
Are you saying that you expected the exception message to be a description of the error?

@jasalt
Copy link
Author

jasalt commented Dec 1, 2024

Are you saying that you expected the exception message to be a description of the error?

@smeghead I'm not sure how much is correct to put into exception message. However, some way to provide detailed info in REPL would be useful.

Example of some Clojure Malli Schema error output https://github.com/metabase/metabase/wiki/Backend:-Metabase-Malli-Cheatsheet#validation.

@smeghead
Copy link
Member

smeghead commented Dec 3, 2024

@jasalt
I found that Malli Schema outputs a very nicely formatted message.

As a way to display phel-schema errors in the REPL in a somewhat understandable way, you might use a macro like the following.

(defmacro z-explain [& body]
  `(try
     ,@body
   (catch \Phel\PhelSchema\ZodError e
     (php/-> e (getIssues))))

If ZodError is encountered in z-explain body, return the error that caused it.

phel:1> (ns app
          (:require phel\schema :as z))
1
phel:2> (def my-schema (z/string))
1
phel:15> (z-explain (z/parse my-schema "tuna"))
tuna
phel:16> (z-explain (z/parse my-schema 12))
<PHP-Array [{:message Invalid type :paths []}]>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants