Skip to content

Instantly share code, notes, and snippets.

@eslick
Last active May 6, 2016 02:56
Show Gist options
  • Save eslick/6af3320804075981eb31 to your computer and use it in GitHub Desktop.
Save eslick/6af3320804075981eb31 to your computer and use it in GitHub Desktop.
One solution to integrating Schema and Datomic
(in-ns test
(:require
[schema.macros :as macros]
[schema.utils :as sutils]))
;; Wrapper type needed because Entity values do not implement
;; IPersistentMap interface
(defrecord EntitySchema
[schema]
Schema
(walker [this]
(let [map-checker (s/subschema-walker schema)]
(fn [e]
(or (when-not (sd/entity? e)
(macros/validation-error this e
(list 'instance? datomic.Entity (sutils/value-name e))))
(map-checker (into {} (seq e)))))))
(explain [this]
(list 'entity 'datomic.Entity (explain (:schema this)))))
(defn entity-schema?
[schema]
(= (type schema) EntitySchema))
(defn model-base-schema
"Generates a schema for Datomic entity with model tag 'type'"
[type]
{:pre [(keyword? type)]}
(EntitySchema. {:model/id s/Uuid
:model/type (s/both s/Keyword (s/eq type))}))
@jcf
Copy link

jcf commented Sep 14, 2015

As sd/entity? is not defined you can use (instance? datomic.Entity e) in its place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment