clojure-spec

About: http://clojure.org/about/spec Guide: http://clojure.org/guides/spec API: https://clojure.github.io/spec.alpha/clojure.spec.alpha-api.html
practicalli-john 2020-06-20T01:29:21.395500Z

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}

2020-06-25T13:23:21.406200Z

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

practicalli-john 2020-06-25T13:44:05.406400Z

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).

2020-06-28T14:02:17.411Z

🙂

2020-06-20T02:03:34.396200Z

check the test.chuck (no typo) library

👍 2
seancorfield 2020-06-20T02:06:44.396800Z

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

👍 3
borkdude 2020-06-20T08:44:21.398Z

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

👍 3