Meaningful error message for Clojure.Spec validation in :pre

后端 未结 4 1835
耶瑟儿~
耶瑟儿~ 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:23

    In newer alphas, there is now s/assert which can be used to assert that an input or return value matches a spec. If valid, the original value is returned. If invalid, an assertion error is thrown with the explain result. Assertions can be turned on or off and can even optionally be omitted from the compiled code entirely to have 0 production impact.

    (s/def ::first-name string?)
    (s/def ::last-name string?)
    (s/def ::person (s/keys :req [::first-name ::last-name]))
    (defn person-name [person]
      (s/assert ::person person)
      (s/assert string? (str (::first-name person) " " (::last-name person))))
    
    (s/check-asserts true)
    
    (person-name 10)
    => CompilerException clojure.lang.ExceptionInfo: Spec assertion failed
    val: 10 fails predicate: map?
    :clojure.spec/failure  :assertion-failed
     #:clojure.spec{:problems [{:path [], :pred map?, :val 10, :via [], :in []}], :failure :assertion-failed}
    

提交回复
热议问题