clojure.spec merge is excellent, but do you consider dissoc spec for keys?

369 views
Skip to first unread message

Mamun

unread,
Jul 19, 2016, 12:54:52 PM7/19/16
to Clojure
Hi 

Without dissoc in keys, how to avoid duplicate spec between backend and frontend layer?

As an example 

I have backend service where I defined spec like this 

(s/def ::person-spec (s/keys ::req-un [::id ::fname ::lname]))

Here Id is mandatory for some purpose. 

Now application layer I would like to reuse that backend spec but only id. Look like now it is not possible as there is no dissoc 

As it is application layer 

(s/def ::person-ui-spec (s/merge (s/keys ::req-un [::channel])
                                   ::person-spec
                                   ) )

merge is excellent, as I could reuse exiting spec. But how I dissoc id from exiting spec.

Only way is now to do is define again in application layer.

(s/def ::person-ui-spec (s/keys ::req-un [::channel ::fname ::lname]))


Do you consider dissoc in spec for keys?



Br,
Mamun
bnp paribas groups










 

Alex Miller

unread,
Jul 19, 2016, 1:22:10 PM7/19/16
to Clojure
Well first I'd say it's actually more important here that you are reusing the attribute specs for ::fname ::lname etc across front and back.

And second, perhaps you should be breaking out the common parts into a spec you can reuse instead:

(s/def ::person-shared-spec (s/keys ::req-un [::fname ::lname]))
(s/def ::person-spec (s/merge ::person-shared-spec (s/keys ::req-un [::id])))
(s/def ::person-ui-spec (s/merge ::person-shared-spec (s/keys ::req-un [::channel])))

Or maybe you really want to combine both of these into a single spec:

(s/def ::person-spec (s/keys :req-un [(or ::id ::channel) ::fname ::lname]))

Or you could potentially use multi-spec, but I doubt you need an open spec for this.

We're not going to add anything like dissoc afaik.
Reply all
Reply to author
Forward
0 new messages