Fork me on GitHub
#clojure-spec
<
2019-03-29
>
kenny01:03:40

@borkdude @mrevelle I've run into problems using s/valid? in production. If my specs use fspecs, it will throw in prod because no test.check. We work around this by never passing a function to a function that uses s/valid? but seems a bit dangerous. Would be nice to be able to disable fspec generative checking in certain production.

ikitommi08:03:00

Is there a way to extract the fdef specs, given one has a function at hand? e.g. need to get the symbol out of the function to be able to look up for it’s spec. Functions are quite opaque in clojure (or I’m just doing it wrong)

ikitommi08:03:22

something like this, but for real (and without eval):

(defn kikka [])

(-> kikka
    str
    (str/replace #"@.*" "")
    (str/replace #"_" "-")
    (str/split #"\$")
    (->> (apply symbol)))
; user/kikka

Alex Miller (Clojure team)12:03:12

You can demunge the class name

Alex Miller (Clojure team)12:03:36

But won’t always work

Alex Miller (Clojure team)12:03:16

In general, it’s better if you can start from a symbol than start from a function object

ikitommi13:03:42

Thanks. In my case, I just don’t have the symbols available, just functions. Knowing the arity of the functions at runtime would be useful too to be able to fail fast in case of invalid arity of the (framework) functions (middleware, interceptors etc). Any change of getting the function symbol & arity queryable for runtime?

Alex Miller (Clojure team)13:03:45

function objects are just function objects - the fact that a particular var is referring to them is in some ways incidental.

Alex Miller (Clojure team)13:03:25

same for arity, although that one in particular is one that affects code evolution over time (it's common for arities to expand but still be backwards compatible)

Alex Miller (Clojure team)13:03:57

I know these seem like obvious things to have on a function, but I think the implications are probably a lot more far reaching and subtle than you expect

Alex Miller (Clojure team)13:03:34

there are a lot of good reasons to fight this kind of introspection and instead try to drive from symbols or vars

Alex Miller (Clojure team)13:03:53

so, in short, no, not eagerly looking to make these changes

drone14:03:55

@kenny we don’t use fdef because of this. And I agree, disabling the test generator req would help. I think it was mentioned as possibly coming in spec2

stathissideris14:03:48

a bit of a philosophical question: I know spec uses qualified keywords, but do people normally go for the :com.my-company.invoice/total style of keywords or the :invoice/total style?

Alex Miller (Clojure team)14:03:33

if you are writing a public library, then I think you endeavor to start your qualifier with something you control (by trademark, domain name, etc).

Alex Miller (Clojure team)14:03:52

if you are writing a private app, then you should make qualifiers "sufficiently unique"

Alex Miller (Clojure team)14:03:26

whether that means including company, department, product, whatever is relevant

stathissideris15:03:09

@alexmiller thanks, that’s a balanced view which I think makes sense

dominicm18:03:29

I'm starting to regret using particularly unique keywords that include the company name and sub project as a prefix for an application. I'm considering adding aliases to human written edn because of it.

carocad19:03:56

hey guys, does somebody knows what is the url of the api docs for spec-alpha2 ? The link seems to be broken in https://github.com/clojure/spec-alpha2 😕

Alex Miller (Clojure team)19:03:27

I'm not sure I've ever built it

Alex Miller (Clojure team)19:03:46

the doc builder works off of a release and we haven't released a version of it yet

Alex Miller (Clojure team)19:03:16

in other words, the link is right, the target is absent :)

Alex Miller (Clojure team)19:03:29

I can try to build it but I'm probably not going to have time today

Alex Miller (Clojure team)19:03:01

Posted a writeup on the (still in work) schema and select in spec 2: https://github.com/clojure/spec-alpha2/wiki/Schema-and-select

sheepy 28
❤️ 64
bellissimo 4
carocad19:03:59

thanks a lot 🙂

seancorfield20:03:30

I know what I'll be doing this weekend (while my wife's in China)... 🙂

eggsyntax14:03:34

Looking good, thanks Alex! Couple of thoughts/questions on that document: - on first encounter, it seems confusing (and complecting 😉 ) to treat schemas sometimes as though they're specs (`s/def`, same registry) and sometimes not (can't be used for valid? etc). Why not either a) let them act as specs (maybe a schema acts like (s/select my-schema), ie all optional, when treated as a spec?) or b) make them fully separate (separate registry, can't use s/def to register them)? - What do you mean by 'SPI'? 'Serial-parallel interface'? :thinking_face: - In the "get-movie-times" example, is there a way to say that address is optional, but if present it must include zip? I know I've encountered use cases like that in practice.

seancorfield20:03:50

Yes, omit address from the vector but still provide the map for the sub-select

user=> (require '[clojure.spec-alpha2 :as s])
nil
user=> (s/def ::name string?)
:user/name
user=> (s/def ::street string?)
:user/street
user=> (s/def ::city string?)
:user/city
user=> (s/def ::zip string?)
:user/zip
user=> (s/def ::addr (s/schema [::street ::city ::zip]))
:user/addr
user=> (s/def ::user (s/schema [::name ::addr]))
:user/user
user=> (s/def ::data (s/select ::user [::name {::addr [::zip]}]))
:user/data
user=> (s/valid? ::data {::name "Sean"}) ; ::addr is optional
true
user=> (s/valid? ::data {::name "Sean" ::addr {}}) : but when provided it must contain ::zip
false
user=> (s/valid? ::data {::name "Sean" ::addr {::zip "94546"}})
true
user=> 

👍 8
Alex Miller (Clojure team)03:04:27

@U077BEWNQ - schemas as specs would be pretty useless since map specs are open (any map would validate) so we decided to make them different. There are some real subtle questions in the idea of wanting to have both for different purposes though and I haven't had a chance to talk through it with Rich yet. might change. - SPI = service provider interface, for those building new kinds of specs/schemas, vs API which is what consumers use. probably should have explained that more clearly.

👍 4