Fork me on GitHub
#clojure-spec
<
2016-09-29
>
richiardiandrea01:09:20

Hello all! A question, let's say I have:

(s/def ::event-common (s/keys :req [::nonce ::payload]))
I would like to declare an event that has fixed ::type:
(s/def ::command-event (s/merge ::event-common -- what do I put here? --))

richiardiandrea01:09:21

(I was also wondering if I can use s/and with my ::event-common def)

alexmiller03:09:42

@richiardiandrea not entirely clear what you want to do. maybe: (s/def ::command-event (s/merge ::event-common (s/keys :req [::type]))) ?

alexmiller04:09:29

or are you asking because they can’t both be satisfied that way? I suspect this is the kind of thing where you’re happier using s/multi-spec

richiardiandrea04:09:17

@alexmiller tnx for answering, I have a multi-spec in place so that I can check if the event conforms to my expected :types. Re-reading my question I see that maybe I am looking at it from the wrong angle..I will clarify my own ideas first and ask again

richiardiandrea04:09:23

I think I wanted to be sure that a function returns only a certain :type of events. So I need to combine the ::common-event spec with a predicate...probably an s/and will suffice.

alexmiller04:09:35

Yeah s/and is fine

seancorfield06:09:27

So I still keep running into situations where I have a spec for a domain level entity that is, say, a set of keywords and then the spec for the input level entity is a set of strings that are the names of the keywords... so my gut says to spec the input as (s/and string? (s/conformer (comp keyword str/lower-case)) ::domain-entity) with a generator of (fn [] (g/fmap name (s/gen ::domain-entity)))

seancorfield06:09:05

But @alexmiller has said to avoid s/conformer so I suspect this is an anti-pattern, even tho' it's cropping up over and over again.

seancorfield06:09:07

The other (anti-)pattern I hit is a domain spec for an int (int-in or whatever) and the input spec is string? and parse to long and the domain spec, again with a generator of fmap str over the domain spec generator...

mpenet07:09:23

Thinking about using a set of specs just for json/http params coercion here, then pass the result to domain specs, I think I prefer to keep both clearly separate.

mpenet07:09:25

makes it a bit easier to reuse also maybe

mpenet07:09:05

anyway, still have a ton of Schema code in our codebases, and still undecided on if/how to port it so far.

mpenet11:09:10

is there an equivalent of describe/form that will resolve specs recursively?

mpenet11:09:41

I mean resolve specs from the registry when it's composed from these

mpenet11:09:41

-> apparently not

mpenet11:09:02

(probably another anti-)pattern I encountered: setting a default value if nil when conforming, it's useful at input level too

mpenet11:09:43

(defn default [spec default]
  (s/conformer
   #(-> spec s/nilable (s/conform %) (or default))))

mpenet11:09:30

another annoying quirk, the following won't compile on alpha13: (defn foo [x] :clojure.spec/invalid)

ExceptionInfo Call to clojure.core/defn did not conform to spec:
:clojure.spec/args  (foo [x] :clojure.spec/invalid)
  clojure.core/ex-info (core.clj:4725)

mpenet11:09:55

makes it impossible to write a declared conformer ...

mpenet12:09:11

well you can create an alias first (def invalid :clojure.spec/invalid) and use it instead of :clojure.spec/invalid in the code, but that's kind of ugly

jeroenvandijk16:09:48

Question, do people add docs to specs? I’m making specs for some configuration data and I want to add meta data/documentation to individual specs to give more background information about the specific options

seancorfield18:09:15

I don’t think you can add docs to specs…?

jrheard22:09:03

first time spec user here, trying to wrap my head around the best way of using namespaced keywords, never used ‘em before what’s the more correct pattern: having my keywords have namespaces that match my project’s namespaces, like :myapp.system.collision/foo? or having my keywords have namespaces that correspond to arbitrary concepts, unrelated to my app’s file/namespace structure, like :collision/foo?

jrheard22:09:26

so far i’m going with the latter, and it feels pretty good, but i’d just like to confirm with the experts 🙂

jrheard22:09:48

also - do you folks have any favorite example github repos that use spec, like, completely idiomatically? i’m switching my project over from schema, and some parts of the transition are straightforward, but other parts just feel like they could be done in a variety of different ways and i’d love to have a few canonical examples to look at and learn from

bfabry22:09:46

it's probably too early for that to exist =/

bfabry22:09:09

fwiw I'm leaning toward the latter of your two options as wel

seancorfield23:09:25

@jrheard We’re creating new namespaces specifically for the data structure specs, and then requiring them into code namespaces as we need them. In a few places we have both data/specs and code. It’s whatever seems most appropriate.

seancorfield23:09:23

BTW, if folks have any thoughts on my comments posted late last night https://clojurians.slack.com/archives/clojure-spec/p1475130747003048 (specifically about the pros and cons of using s/conformer)

seancorfield23:09:40

(if you click that link, you’ll see two more follow-on comments)