Fork me on GitHub
#clojure-spec
<
2021-06-14
>
bortexz16:06:26

Hi, I know spec2 is still in alpha/experimental phase and buggy. Still, I have been playing around with it to model the domain of an experimental project I have, where I want to test out the capabilities of domain modelling with schema/select. Is it possible to have a multi-spec return a schema, and do select on the multi-spec? It doesn’t seem to work on current git-sha, I wonder if it’s a bug or is it intended that this is not possible:

(spec/def :account/id string?)
(spec/def :service/id string?)
(spec/def :service-1/field string?)
(spec/def :service-2/field string?)

(defmulti account-schema :service/id)

(defmethod account-schema :service-1
  [_]
  (spec/schema [:account/id :service-1/field]))

(defmethod account-schema :service-2
  [_]
  (spec/schema [:account/id :service-2/field]))

(spec/def :account/account (spec/multi-spec account-schema :service/id))

(gen/generate (spec/gen :account/account))

(spec/valid? (spec/select :account/account [:account/id :service-1/field :service/id])
             {:service/id :service-1
              :account/id "id#1"
              :service-1/field "abcd"})
The generator works correctly, but not the validation, that fails with:
; Execution error (IllegalArgumentException) at clojure.alpha.spec.protocols/eval5667$fn$G (protocols.clj:20).
; No implementation of method: :keyspecs* of protocol: #'clojure.alpha.spec.protocols/Schema found for class: clojure.alpha.spec.impl$multi_spec_impl$reify__7055

Alex Miller (Clojure team)17:06:02

undecided stuff in this area about what can act as a schema provider

Alex Miller (Clojure team)17:06:39

if you have a use case where this would be useful, would be helpful to have an https://ask.clojure.org question with request tag

bortexz17:06:35

Thanks Alex, I will open the question with a more detailed use case