Fork me on GitHub
#clojure-spec
<
2017-07-22
>
danielcompton01:07:22

Looks neat, I see there is "Coercion overrides map to specify contextual coercions" on the TODOs, will that let you specify different contexts like a JSON payload or form encoding, e.t.c.?

wilkerlucio03:07:53

@danielcompton thanks for the interest 🙂 for that I was thinking more like when you do generator overrides, like you do on specs, I think the main use case would be to set a custom parser for a different date/time format depending on the source

thedavidmeister05:07:33

hey, is there a way to and additional predicates onto existing keywords when used in spec/keys for a new def?

thedavidmeister05:07:35

e.g. i have :db/id which can be any int? but when i have a :db/id in a (spec/def :item/new ...) i also want to ensure that it is the specific int -1

Alex Miller (Clojure team)05:07:07

No, but you can s/and additional constraints over the s/keys

thedavidmeister06:07:31

(spec/def ::item
 (spec/and
  (spec/keys :req [:db/id])
  (comp #(spec/valid? ::id %) :db/id)))

thedavidmeister06:07:43

that's a little different to what i asked

thedavidmeister06:07:56

(spec/def ::id
 (spec/or
  ::id--new #{new-item-id}
  ::id--existing pos-int?))

thedavidmeister06:07:05

but seems to be what i want when i run exercise, thanks 🙂

Alex Miller (Clojure team)06:07:41

I would try to avoid the comp valid? … and instead state it as an additional spec or predicate

Alex Miller (Clojure team)06:07:32

you can also use s/merge instead of s/and to combine two map specs together

Alex Miller (Clojure team)06:07:47

one general, and one more specific

Alex Miller (Clojure team)06:07:52

s/keys already checks that the value for the key conforms to the spec, so the extra check in your ::item is duplicating that

thedavidmeister06:07:02

@alexmiller ah, i'm pretty new to this, could you give me a simple example of that?

thedavidmeister06:07:35

(spec/def :db/id int?) this is :db/id

thedavidmeister06:07:47

so that's any int

thedavidmeister06:07:05

but the item id has to be either a positive int or new-item-id (which is -1)

thedavidmeister06:07:12

it can't be just any negative int

Alex Miller (Clojure team)06:07:20

I think you mostly have it above already

Alex Miller (Clojure team)06:07:03

and then just (spec/def ::item (spec/keys :req [::id])) is sufficient

Alex Miller (Clojure team)06:07:37

spec/keys checks that the map has the required keys and that every registered key spec has a value that conforms to that spec

thedavidmeister06:07:49

except that when i do exercise it generates maps with ::id instead of :db/id

thedavidmeister06:07:44

i can't actually pass it ::id because the key :db/id is coming from upstream

thedavidmeister06:07:03

so, related question

Alex Miller (Clojure team)06:07:03

sorry, it was unclear whether those were the same or different things

thedavidmeister06:07:18

i've got something related

thedavidmeister06:07:23

i have a :project/id

thedavidmeister06:07:32

and i want to reference it with :item/project

thedavidmeister06:07:54

they have the same predicate so i thought i could do (spec/def :item/project :project/id)

thedavidmeister06:07:59

but it doesn't like that

Alex Miller (Clojure team)06:07:11

should work fine - what’s not working?

thedavidmeister06:07:38

Unable to resolve spec: :project/id

Alex Miller (Clojure team)06:07:01

could you give a larger example that produces that?

thedavidmeister06:07:14

(spec/def :project/id uuid?)
(spec/def :item/project :project/id)

thedavidmeister06:07:32

but when :project/id is in a different ns (that i required) it gives the error

Alex Miller (Clojure team)06:07:51

can you give that example?

Alex Miller (Clojure team)06:07:44

there is a known issue right now with ordering of something like that such that the aliased spec needs to be defined first

Alex Miller (Clojure team)06:07:56

are you running into something like that?

thedavidmeister06:07:13

i'm stopping and starting my test runner now

thedavidmeister06:07:19

maybe it got confused with me changing things

thedavidmeister06:07:13

yeah it seems fine now

thedavidmeister06:07:17

so i cannot reproduce

Alex Miller (Clojure team)06:07:39

probably an ordering issue

Alex Miller (Clojure team)06:07:50

I’m taking off! Later…

thedavidmeister06:07:45

thanks for the help!

ikitommi07:07:30

ok @alexmiller, added the current changes as a patch too if that helps the conversation with Rich.