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
lassemaatta 2020-11-19T06:52:10.389700Z

A question concerning multi-spec : Suppose I have a spec similar to the one given in the clojure spec guide (ie. :event/event), where different events contain a :event/type key. How can I generate sample events of a particular type, for example :event/error events? Or refer to the spec of a particular event somewhere (e.g. in a s/fdef). What I've tried so far: If I replace the spec from the corresponding defmethod call into a s/def definition, I can obtain the generator for it and I get all the fields I expect but the :event/type key will be a random keyword. Another hack was to generate events from :event/event and then use s/and + predicate to only accepts events which have the correct type, but this "lacks elegance" 🙂

lassemaatta 2020-11-19T07:01:17.389800Z

One might argue that it reflects poor design if one has to refer to particular type like this, but still it might be nice to know if & how this is possible :thinking_face:

alexmiller 2020-11-19T14:23:27.390400Z

I think something like what you describe is how most people are approaching it right now