12: what about cycles?
(ns reifyhealth.specmonstah-tutorial.12
(:require [clojure.spec.alpha :as s]
[clojure.spec.gen.alpha :as gen]
[reifyhealth.specmonstah.core :as sm]
[reifyhealth.specmonstah.spec-gen :as sg]))
(def id-seq (atom 0))
;; This spec uses a generated such that each time a value is generated
;; it returns the incremented value of the atom id-seq
(s/def ::id (s/with-gen pos-int?
#(gen/fmap (fn [_] (swap! id-seq inc))
(gen/return nil))))
(s/def ::topic-id ::id)
(s/def ::first-post-id ::id)
(s/def ::post (s/keys :req-un [::id ::topic-id]))
(s/def ::topic (s/keys :req-un [::id ::first-post-id]))
(def schema
{:topic {:prefix :t
:spec ::topic
:relations {:first-post-id [:post :id]}
:conform {:cycle-keys #{:first-post-id}}}
:post {:prefix :p
:relations {:topic-id [:topic :id]}
:constraints {:topic-id #{:required}}
:spec ::post}})
(defn ex-01
[]
(sm/view (sm/add-ents {:schema schema} {:post [[3]]})))
(ex-01)
Last updated