Fork me on GitHub
#clojure-spec
<
2020-06-20
>
practicalli-johnny01:06:21

Any suggestions on how to create a custom generator for this specification for an email address (or an alternative specification for an email address that can be used - I could just use string until I get chance to think about it properly...)

(def email-regex #"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$")
(s/def ::email-type (s/and string? #(re-matches email-regex %)))
When I use clojure.spec.gen.alpha/generate on the returned generator from (clojure.spec.alpha/gen ::email-type) I get an exception (my other specs will all generate data, just not this. So I am assuming I need a custom generator (it will be my first) The error I get from generate is
Unhandled clojure.lang.ExceptionInfo
   Couldn't satisfy such-that predicate after 100 tries.
   {:pred #function[clojure.spec.alpha/gensub/fn--1876],
    :gen {:gen #function[clojure.test.check.generators/such-that/fn--6372]},
    :max-tries 100}

djtango13:06:21

though if you're just doing this for fun, then w/e

practicalli-johnny13:06:05

Everything I do is for fun, unless someone is paying me a 6 figure sum to do otherwise (although I did still have some fun working at the bank).

gfredericks02:06:34

check the test.chuck (no typo) library

👍 6
seancorfield02:06:44

Yeah, a big +1 for test.chuck -- we use the regex generator a lot!

👍 9
borkdude08:06:21

@jr0cket Additionally, lambdaisland/regal also has generators for regexes

👍 9