Skip to content

Instantly share code, notes, and snippets.

@jeroenvandijk
Last active September 8, 2016 15:17
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 jeroenvandijk/2748b6af74c6ba6f8fd7dffaed5cc390 to your computer and use it in GitHub Desktop.
Save jeroenvandijk/2748b6af74c6ba6f8fd7dffaed5cc390 to your computer and use it in GitHub Desktop.
multi-spec on vectors
(require '[clojure.spec :as s])
;; WARNING: be careful with testing in the repl as reloading doesn't work AFAIK (e.g. changing the dispatch fn). You have to change the multimethod name
(do
(s/def ::action-type #{:CREATE :DELETE})
(s/def ::entity-id any?)
(s/def ::label-action (s/cat :type ::action-type
:entity-type #{:label}
:entity-id ::entity-id))
(s/def ::template-action (s/cat :type ::action-type
:entity-type #{:template}
:entity-id ::entity-id))
(defmulti foo-action second)
(defmethod foo-action :label [_]
::label-action)
(defmethod foo-action :template [_]
::template-action)
;; REVIEW haven't figured out how the retagging works yet, is that related to s/unform?
(s/def ::foo-action (s/multi-spec foo-action :entity-type))
(s/conform ::foo-action [:CREATE :template 1 ]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment