Fork me on GitHub
#clojure-spec
<
2019-06-25
>
miguelb01:06:08

Hi everyone, I’m trying to to write a spect that has “relationships” to other specs. (apologies for the language, I’m still new to spec). For example, a group has a total number of people, number of active people and number of inactive people. active-people + inactive-people = total num of people. I can write a spec for the total number of people but how do I use that spec for in the spec for both inactive and active. Also how does that work with generators? Ultimately I would like to generate a group where total, active and inactive are set.

seancorfield01:06:56

@miguelb Are these specs all used together in a map?

miguelb01:06:25

yea eventually, right now i’m defining each with their own s/def

miguelb01:06:06

not sure if this is the right way to go about it, define each part and then compose together

seancorfield01:06:18

(s/and (s/keys :req [::active-people ::inactive-people ::total-people]) #(= (::total-people %) (+ (::active-people %) (::inactive-people %))))

seancorfield01:06:45

You can only apply relationship predicates to something that contains all the various related keys.

seancorfield01:06:40

However, I would question the model design: since that's an invariant that should always hold, you don't need all three values (and probably should not try to have all three). Any two gives you all the information you need.

miguelb01:06:03

good point

miguelb01:06:18

my end goal here is to make a generator that will generate groups

miguelb01:06:34

I was aiming to have the person count part of a “valid” group

ghadi01:06:55

you'll end up needing to manually control the generator

ghadi01:06:22

the default spec generators will not find something that satisfies the invariant within 100 tries

ghadi01:06:36

unless you're feeling really really lucky

seancorfield01:06:46

Yes, that's another reason that I think it would be easier without the constrained total-people number

seancorfield01:06:22

If you just have active and inactive, then regular generators will work

seancorfield01:06:33

(since there's no need for an additional constraint)

miguelb01:06:51

I’ll try that way, makes way more sense that what I was about to try

miguelb01:06:53

ty very much!