Meaningful error message for Clojure.Spec validation in :pre

后端 未结 4 1837
耶瑟儿~
耶瑟儿~ 2021-02-05 06:42

I used the last days to dig deeper into clojure.spec in Clojure and ClojureScript.

Until now I find it most useful, to use specs as guards in :pre

4条回答
  •  忘了有多久
    2021-02-05 07:09

    I think the idea is that you use spec/instrument to validate function input and output rather than pre and post conditions.

    There's a good example toward the bottom of this blog post: http://gigasquidsoftware.com/blog/2016/05/29/one-fish-spec-fish/ . Quick summary: you can define a spec for a function, including both input and return values using the :args and :ret keys (thus replacing both pre and post conditions), with spec/fdef, instrument it, and you get output similar to using explain when it fails to meet spec.

    Minimal example derived from that link:

    (spec/fdef your-func
        :args even?
        :ret  string?)
    
    
    (spec/instrument #'your-func)
    

    And that's equivalent to putting a precondition that the function has an integer argument and a postcondition that it returns a string. Except you get much more useful errors, just like you're looking for.

    More details in the official guide: https://clojure.org/guides/spec ---see under the heading "Spec'ing functions".

提交回复
热议问题