Fork me on GitHub
#clojure-spec
<
2020-02-10
>
jrwdunham06:02:03

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.

jrwdunham06:02:06

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

thom12:02:37

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

andy.fingerhut21:02:11

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

andy.fingerhut21:02:04

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.

andy.fingerhut21:02:39

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.