Skip to content

Instantly share code, notes, and snippets.

@bhb
Created June 21, 2017 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhb/7eefe3034a0622b6499a5b5dd2f7e52a to your computer and use it in GitHub Desktop.
Save bhb/7eefe3034a0622b6499a5b5dd2f7e52a to your computer and use it in GitHub Desktop.
Should I be able to provide a default spec with the `:default` implementation of my multimethod?
;; [org.clojure/clojurescript "1.9.229"]
(s/def :event/type keyword?)
(s/def :search/url string?)
(s/def :error/code int?)
(defmulti event-type :event/type)
(defmethod event-type :event/search [_]
(s/keys :req [:event/type :search/url]))
(defmethod event-type :event/error [_]
(s/keys :req [:event/type :error/code]))
(defmethod event-type :default [_] (s/keys))
(s/def :event/event (s/multi-spec event-type :event/type))
(comment
(s/valid? :event/event {:event/type :event/error :error/code 1}) ; Valid
;; event-type does return a spec for unknown types
(s/form (event-type {:event/type :event/unknown})) ; => (cljs.spec/keys)
;; But the unknown event is not valid
(s/valid? :event/event {:event/type :event/unknown}) ; => false
(s/explain :event/event {:event/type :event/unknown}) ; val: {:event/type :event/unknown} fails spec: :event/event at: [:event/unknown] predicate: event-type, no method
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment