Fork me on GitHub
#clojure-spec
<
2019-08-26
>
Lone Ranger15:08:04

okay so I'm doing this pattern a lot and I'm wondering if there's a more concise way to do it, perhaps to register a s/def with another s/def at creation time?

(s/def :platform.confirm/display-attribute keyword?)
(s/def :platform.confirm/temp-attribute string?)
(s/def :platform.confirm/id string?)
(s/def :platform.confirm/value string?)
(s/def :platform.confirm/edit-attribute keyword?)

(s/def ::confirm (s/keys :req [:platform.confirm/display-attribute
                               :platform.confirm/temp-attribute
                               :platform.confirm/id
                               :platform.confirm/value
                               :platform.confirm/edit-attribute]))

Lone Ranger15:08:37

and, while I'm at it ... I'm wondering if anyone has any best practices in combining spec with datomic? Since we already go to all the trouble of making the schema for datomic, has anyone looked into using that for spec?

seancorfield17:08:21

@goomba This popped up as the second result on a Bing search for using clojure.spec to create datomic schema for me: https://github.com/Provisdom/spectomic -- seems to be what you're looking for?

Lone Ranger17:08:02

oh man, and great name too

Lone Ranger17:08:33

that's interesting ... they went the opposite way I would've expected, i.e., s/def -> schema

Lone Ranger17:08:52

I would've expected schema-ish -> s/def

seancorfield17:08:04

That makes sense to me. We treat spec as the "system of record" and generate stuff from it.

Lone Ranger17:08:40

makes sense. I'm usually just paranoid about "losing" my schema.

Lone Ranger17:08:24

I like to see them all written out explicitly. But that would be easy enough with eval-and-replace

seancorfield17:08:35

I recently answered a Quora question about using clojure.spec https://twitter.com/seancorfield/status/1164714132647530496 (don't know if you can read the answer without a Quora account?)

๐Ÿ‘ 4
kenny17:08:57

@goomba clojure.spec can express more info than Datomic schema can so it makes more sense to go from spec -> schema.

Lone Ranger17:08:57

wheeee yess! That was another question I had!

Lone Ranger17:08:43

@kenny solid point. I was just expecting something more like a hybrid spec/schema, uh... spec

Lone Ranger17:08:56

have it all in one place

Lone Ranger17:08:13

this is totally fine though

Lone Ranger17:08:11

also got a lot of good answers out of it and experienced @seancorfield do a "let me Bing that for you" which is fantastic ๐Ÿ˜†

seancorfield17:08:35

I hope you didn't think I was being mean with my comment about Bing? You've said you're sometimes not finding search results for stuff so I figured sharing the search string I used might be helpful. Quite a few of my friends ping me about finding stuff because apparently I'm better at searches than them... ๐Ÿ™‚

Lone Ranger18:08:21

no hahaha I thought it was hilarious ๐Ÿ˜„ I loved it

Alex Miller (Clojure team)17:08:23

I've seen people do both directions, and also start from a 3rd "source of truth" and generate the Datomic schema and spec too

Lone Ranger17:08:37

mmm chimeratomic

kenny17:08:23

Attaching schema data directly to the specs is something that interested us too @goomba. From what I remember when we wrote that lib, it wasn't immediately obvious what the best path to doing that would be. Ideally it would've been something simple like attaching metadata to the spec's name (e.g. (def ^{:db/isComponent true} my-spec map?)). That wouldn't work for specs because they are typically defined as keywords which do not accept metadata.

kenny17:08:50

There were 2 other options IIRC. 1) Create a new s/def which accepts a new 3rd parameter. 2) add a function similar to s/with-gen which would attach metadata to the spec itself.

Lone Ranger17:08:52

nod... in my lazy imagination it would be something like

{:db/ident       :boot.user/string
 :db/valueType   :db.type/string
 :db/cardinality :db.cardinality/one
 :spec/id        ::string
 :spec/spec      string?}
and then specs and schema would be gen'd from that. But listen I'm a lazy application layer dev asking for handouts, what you did is great it's just not what I would've thought of immediately

kenny17:08:43

I mean, that's basically a complete schema map.

Lone Ranger17:08:56

I'm not even sold on the :spec/spec with a function there because now that's not even a value anymore

Lone Ranger17:08:32

yeah I'm excited to check out spectomic!

Lone Ranger17:08:49

I do a ton of datascript/spec work so this is great

kenny17:08:42

I haven't looked at this at all but I'm assuming that this sort of thing would be easier to do with Spec2, given Spec2 allows specs to have docstrings.

ikitommi19:08:41

hello all. is there a reason that pos? and neg? donโ€™t have built-in generators?

Alex Miller (Clojure team)19:08:03

yes, they are not specific to one numeric type

Alex Miller (Clojure team)19:08:15

use (s/and int? pos?) or whatever numeric type you need

ikitommi19:08:20

ok, thanks!

Alex Miller (Clojure team)19:08:02

or something like pos-int?

4