Fork me on GitHub
#clojure-spec
<
2018-08-04
>
PB05:08:21

Hey guys, sorry for the slow reply. I had ended up with this:

(s/def ::age (s/with-gen (s/and integer? pos? #(< % 100)) (clojure.spec.gen.alpha/large-integer* {:min 0 :max 100})))

seancorfield05:08:33

@petr Did you see my suggestion of using s/int-in?

seancorfield05:08:05

user=> (require '[clojure.spec.alpha :as s])
nil
user=> (s/def ::age (s/int-in 1 100))
:user/age
user=> (s/exercise ::age)
([1 1] [1 1] [2 2] [2 2] [1 1] [6 6] [5 5] [1 1] [16 16] [7 7])
user=> (s/exercise ::age)
([1 1] [2 2] [1 1] [3 3] [8 8] [2 2] [2 2] [9 9] [2 2] [69 69])
user=>

PB14:08:12

I did thanks!

4
misha06:08:18

what is the practical purpose of s/key’s :opt and :opt-un? - documentation(?) - include :opt keys during s/exercise - anything else? does s/conform conforms all known keys in a map, or just listed in :req/:opt/-un?

valerauko07:08:40

in my understanding it's that the presence of the key is optional, but if it's present, its shape has to fulfill its spec

guy07:08:07

Thats what ive used it for before

misha08:08:34

@vale s/keys checks against all the known key-specs, you dont have to list them in the s/keys. empty s/keys will test all the registered keys

valerauko08:08:18

are you sure about that?

misha08:08:50

this is one of the goals or benefits of having global specs registry and namespaced keywords

misha08:08:18

(s/def :foo/bar int?)
=> :foo/bar
(s/valid? (s/keys) {:foo/bar 1})
=> true
(s/valid? (s/keys) {:foo/bar "1"})
=> false

💯 12
valerauko08:08:38

i see... well it's still meaningful for the un ones where you have to be explicit

valerauko08:08:53

otherwise the opt is probably just for what you said

misha08:08:49

anything else?

4
guy08:08:02

>you dont have to list them in the s/keys. I thought part of the reason u list them in s/keys is to show easily what the shape of the data is?

guy08:08:29

using (s/keys) like you did seems to be interesting, i’ve never seen that before

misha08:08:01

@guy “show” is documentation + exercise I mentioned above

guy08:08:27

oh right sorry

guy08:08:29

:thumbsup:

guy08:08:44

yeah i think uve covered it imo

misha08:08:58

I’d not use empty s/keys, it is just an illustration

✔️ 4
ackerleytng13:08:17

does anyone have a good way of writing specs for ring middleware?

ackerleytng13:08:20

for every middleware function along the chain, the inputs and outputs change

ackerleytng13:08:35

seems tedious to spec all the middleware functions

gfredericks13:08:25

you shouldn't have to fully describe the req/resp at each point though; you could describe just what's pertinent to that piece

ackerleytng13:08:15

do you mean like... if this middleware augments the request with a new key value pair

ackerleytng13:08:35

then perhaps the :args request should be just map?

ackerleytng13:08:17

and the :ret is like a function with :args including just the new key-value pair?

ackerleytng13:08:34

sort of like only writing spec for the change in this particular middleware?

gfredericks14:08:12

you could also write a :fn saying everything else gets passed through