This Is the Data You’re Looking For

To get a feeling for how clojure.spec works, let’s return to our book-store example and imagine that we’re writing code to process our familiar book maps, values that look like this:

 {:title ​"Getting Clojure"​ :author ​"Olsen"​ :copies 1000000}

Further, imagine that we’re getting our book data from a not-terribly-reliable source, and we’ve decided that the first thing we need to do is validate that this value that claims to be a book is in fact a book-shaped value. Clearly we could write a function:

 (​defn​ book? [x]
  (and
  (map? x)
  (string? (:author x))
  (string? (:title x))
  (pos-int? (:copies x))))

While this does work, the code it by hand approach to validating ...

Get Getting Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.