Fork me on GitHub
#clojure-spec
<
2018-10-09
>
jrychter16:10:30

I wish there was a book I could buy, titled "Data Modeling with clojure.spec".

jrychter16:10:52

Here's an example of a practical question. Should I do (a) or (b)?

;; A:
(ns testing.spec
  (:require [testing.id :as id]))

(s/def ::id ::id/id)
(s/def ::data-structure (s/keys :req [::id]))

;; B:
(ns testing.spec
  (:require [testing.id :as id]))

(s/def ::data-structure (s/keys :req [::id/id]))

alexmiller16:10:56

You should do whichever one matches your data

jrychter16:10:19

Hence my wish for the book 🙂

mattly18:10:34

personally I would define the spec for the id in the same ns as the id

mattly18:10:12

unless the testing.spec ns is the only place you plan to refer to it

jrychter18:10:39

:: is a spec for an UUID in a certain form, this gets used all over the place. Question is, do I alias it locally using ::id? In other words, who "owns" the ::id definition?

jrychter18:10:00

And Alex's Zen Master answer is actually spot on. My current thinking is that I should use local ::id, since this is really a property of the data that I'm defining locally, and only coincidentally an UUID whose properties are defined elsewhere.

alexmiller18:10:01

I think about it as two levels - it’s useful to have specs that capture common type/shapes/formats domain things - phone numbers, invoice numbers, etc. then there are entities that collect attributes and those may have their own names. Like you have a :foo.domain/phone and then you have :foo.company/phone

alexmiller18:10:19

(and the latter would just be an alias for the former)

alexmiller18:10:06

that adds weight (and there are some bugs around overriding generators for aliased specs) so it may or may not be worth it

alexmiller18:10:49

phone number is probably something with extra constraints, but if you have something adequately represented as int? then I would not pull it out as a domain type as that doesn’t seem worth it

alexmiller18:10:15

some day we’ll consider doing a 2nd edition of Clojure Applied and think about some of this stuff :)

orestis13:10:29

I’d love a 2nd edition of Clojure Applied — or perhaps a 2nd volume? 😄

alexmiller14:10:22

based on my time availability, I don’t think it’s happening anytime soon

orestis14:10:39

Understandable, of course. Thanks for the first edition in any case!

jrychter18:10:42

Right. This is similar to what I've been thinking, too.

jrychter18:10:36

A book would be very welcome. I would really like to learn about how people approach modeling data, preferably from people who actually build real systems. Spec seems very flexible, but I found that if you deviate from a certain path, you'll be swimming against the current (for example, if you insist on using unqualified keys).

jrychter18:10:27

BTW, I solved my coercion problems for the time being by judiciously removing every s/conform from my code and writing a tiny piece of code that maintains registries of coercions (multiple registries, as it turns out: one for converting to the domain model, and one for converting to db form). This seems to work very well and apart from limitations of unqualified keys in specs solves the problem for me.