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
jrwdunham 2020-02-10T06:29:03.177400Z

I want a spec that accepts nilable values but does not generate them. So far I'm going with a macro like the following. Is there a better way to do this? Is wanting to do this an indicator that I'm making a bad design decision? The motivation is that I'm working with a db schema that allows NULL but my validation on data coming from users will prevent incoming nil values. I want my generators to simulate user-supplied data going into the DB and not data coming out of the existing DB.

alexmiller 2020-02-10T13:44:06.179100Z

seems ok to me

👍 1
jrwdunham 2020-02-10T06:29:06.177700Z

(defmacro def-spec-nilable-non-nilable-generator
  [kw spec]
  `(s/def ~kw
    (s/with-gen
      (s/nilable ~spec)
      (constantly (s/gen ~spec)))))

thom 2020-02-10T12:54:37.179Z

Does anybody know of anything resembling linear temporal logic (or something weaker but still useful for describing sequences) for spec?

2020-02-10T21:20:11.179400Z

I do not know if this is anywhere near what you are looking for, but a Google search for the terms "clojure linear temporal logic" turned up this project: https://github.com/esb-lwb/lwb

2020-02-10T21:21:04.179900Z

Spec I am sure has nothing built in for this, except the general purpose escape hatch of letting you use arbitrary functions you write that return true/false as specs -- which is a huge enabler for anything you can write code for.

2020-02-10T21:21:39.180100Z

It won't necessarily help you generate random instances satisfying that function -- that is separate work you would need to do if you wanted that capability.