clojure-spec

About: http://clojure.org/about/spec Guide: http://clojure.org/guides/spec API: https://clojure.github.io/spec.alpha/clojure.spec.alpha-api.html
bertofer 2021-06-14T16:42:26.099100Z

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

alexmiller 2021-06-14T17:09:02.100200Z

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

alexmiller 2021-06-14T17:09:39.100800Z

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

bertofer 2021-06-14T17:12:35.101Z

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