Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Clojure Spec with Datomic entities

Say I have the following specs:

(s/def :person/age number?)
(s/def :person/name string?)

(s/def ::person (s/keys :req [:person/name :person/age]))

Then I fetch an entity from Datomic:

(def person-entity (d/entity (d/db conn) [:person/name "Mr entity"]))

If I try to check conformance with the spec it fails because entities are not maps:

(s/explain ::person person-entity)

val: #:db{:id 17592186069950} fails spec: :some-ns/person predicate: map?

My app has functions that take entities as arguments and would like to avoid having to reify entities to maps everywhere just to get spec instrumentation to work in development.

How should I go about validating entities through spec?

like image 828
Odinodin Avatar asked Oct 11 '16 06:10

Odinodin


1 Answers

Although it is true that your namespaces are a bit off (you should be using :person/name instead of ::name), there is also a restriction in spec that could be lifted. Currently s/keys requires that all input collections conform to the clojure.core/keys predicate. Datomic entities are not maps, and so do not pass that check (and hence the error about the map? predicate).

I've filed a bug report here: http://dev.clojure.org/jira/browse/CLJ-2041 Feel free to upvote/follow the ticket if you wish.

like image 134
Timothy Baldridge Avatar answered Oct 31 '22 11:10

Timothy Baldridge