Fork me on GitHub
#clojure-spec
<
2018-07-06
>
tschady16:07:37

I’d like to try and do this without custom generators… any ideas? I want to spec out a string of digits, with each having certain restrictions. e.g. each digit is (s/int-in 2 10) but I’d like 3 in a row catted into a string. using regexes like #(re-matches #"[2-9]{3}" %) requires me to make a custom generator I believe. Struggling with the syntax.

tschady16:07:24

I can get an s/tuple easy enough, but not sure how to jam that into a string inside the s/def

tschady17:07:34

so I have

(s/def ::digit (s/int-in 2 10))
(s/def ::code (s/tuple ::digit ::digit ::digit))
but instead of a tuple/vector I want them jammed into a string

noisesmith17:07:15

I can't answer this question, but as an aside, spec is explicitly not for parsing

tschady17:07:35

It’s not to parse it’s to validate data. (And generate it)

noisesmith17:07:16

why not use a regex as your spec validator, and write a custom generator?

guy17:07:17

You can give a spec a predicate and a generator

tschady17:07:20

I could. Just wanted to know if there was a cool way to do without

guy17:07:24

so just make a custom predicate

guy17:07:30

let me find an example 2 secs

guy17:07:02

so you can have say

guy17:07:21

(s/def ::digit my-pred-fn my-gen)

guy17:07:54

(defn my-pred-fn [x] your logic)

guy17:07:00

if that makes sense?

guy17:07:18

but you need a generator that would then fulfil that predicate

tschady17:07:34

Yes thanks. I’m familiar with custom generators. It just seems like I’m codifying the same rule in two places (pred and gen) which I would like to avoid if I could.

👍 4
tschady17:07:02

But I’ll just move ahead with a custom gen.

guy17:07:18

@tws oh sorry just read your first line facepalm

guy17:07:24

>I’d like to try and do this without custom generators

tschady17:07:55

Communication is hard!

guy17:07:09

haha nah i just need more coffee so i can read better 😞