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

Confusing difference between s/explain and phrase on success #21

Closed
marco-m opened this issue Jul 13, 2018 · 2 comments
Closed

Confusing difference between s/explain and phrase on success #21

marco-m opened this issue Jul 13, 2018 · 2 comments

Comments

@marco-m
Copy link

marco-m commented Jul 13, 2018

Hello, thanks for phrase!

I spent hours trying to debug a nil from phrase-first until I realized that there is a behavior difference that I find confusing. Consider the following:

(s/def ::simple int?)

(phrase/defphraser int? [_ _] "Expected an integer")

(deftest simplest-possible-spec-for-phrase
  (is (= "val: 1.0 fails spec: :grape.phrase-test/simple predicate: int?\n"
         (s/explain-str ::simple 1.0)))
  (is (= "Expected an integer"
         (phrase/phrase-first {} ::simple 1.0))))

(deftest difference-between-explain-and-phrase-when-success
  (is (= "Success!\n"
         (s/explain-str ::simple 1)))
  (is (= nil
         (phrase/phrase-first {} ::simple 1))))

First test shows that phrase is for end-user and s/explain is for developers, everything is as expected :-)

Second test shows that, when the spec is actually valid, s/explain returns string "Success!" while phrase returns nil. Although I read the docstring for phrase-first: "Returns nil if x is valid or no phraser was found." I was still confused for hours :-)

In my opinion, there is a big difference between a valid spec and no phraser found. Is there a reason for always returning nil or would it make sense to conform (pun intended) with explain here and return "Success!" ?

@marco-m marco-m changed the title Confusing difference between s/explain and phrase Confusing difference between s/explain and phrase on success Jul 13, 2018
@alexanderkiel
Copy link
Owner

Thanks for asking. I clarified things in aaa3687. The function phrase-first is just a shortcut for phrasing a problem directly with phrase. If there is no problem, you can't phrase anything. So phrase-first returns nil on success. The messages returned by phrase are supposed to be error messages. So "Success!" wouldn't be useful to present to the user. But you can implement your expected behavior using the default phraser.

@marco-m
Copy link
Author

marco-m commented Jul 17, 2018

Ok, thanks for the explanation.

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