This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-10-21
Channels
- # announcements (1)
- # aws (18)
- # babashka (5)
- # beginners (72)
- # biff (2)
- # calva (38)
- # cider (2)
- # clj-commons (6)
- # clj-yaml (2)
- # clojars (7)
- # clojure (41)
- # clojure-austin (5)
- # clojure-europe (78)
- # clojure-nl (1)
- # clojure-norway (18)
- # clojure-uk (3)
- # clojurescript (13)
- # component (9)
- # cursive (37)
- # datahike (3)
- # datomic (7)
- # fulcro (7)
- # graphql (3)
- # holy-lambda (2)
- # honeysql (8)
- # introduce-yourself (1)
- # jobs (1)
- # kaocha (1)
- # leiningen (19)
- # lsp (104)
- # malli (5)
- # nbb (8)
- # off-topic (60)
- # polylith (22)
- # portal (2)
- # reagent (24)
- # reveal (1)
- # shadow-cljs (126)
- # test-check (11)
- # tools-build (39)
- # vim (23)
- # xtdb (10)
I'm currently implementing a custom schema (for reference types).
My question: Is it idiomatic to call (m/validate ?schema ?subject {:registry ?my-local-reg})
within the body of a content-dependent schema like in:
(let [;; self-checking keyword spec is necessary to make `[:my/ref :asset/id] work
local-registry
(merge
;; self-validating schemas
{:asset/id (m/schema [:fn #(= :asset/id %)])
:grommet/id (m/schema [:fn #(= :grommet/id %)])}
;; for ID values
{:or (m/-or-schema)
:int (m/-int-schema)
:string (m/-string-schema)
:uuid (m/-uuid-schema)
:id [:or :int :string :uuid]})
;; content-dependent schema
Ref (m/-simple-schema (fn [props children]
(log/spy :info children)
{:type :my/ref
:pred (fn [x]
(let [attr-schema (first children)
[id-attr id-val] (first x)]
(and (map? x)
(= 1 (count x))
;; ============== ;;
;; Q: is this ok? ;;
;; ============== ;;
(m/validate :id id-val {:registry local-registry})
(m/validate attr-schema id-attr {:registry local-registry}))))
;; Don't forget the children count constraints!
:min 1
:max 1}))
custom-reg {:my/ref Ref}
registry (mr/composite-registry
m/default-registry
local-registry
custom-reg)]
[(m/validate [:my/ref :asset/id] {:asset/id 123} {:registry registry})
(m/validate [:my/ref :asset/id] {:grommet/id 123} {:registry registry})
(m/validate [:my/ref [:or :asset/id :grommet/id]] {:asset/id 123} {:registry registry})
(m/validate [:my/ref [:or :asset/id :grommet/id]] {:other/id 123} {:registry registry})
])
Is there perhaps another approach preferable?
Thanks 🙏I guess a better version would look somethat like -maybe-schema
instead of using simple-schema
Have stumbled few times on writing custom reference schemas, not trivial. No best practices, but we have been adding new configuration options to schemas, (e.g. just added :pred
to m/-map-schema
), so if there is an existing schema type which is almost what you need and would be exactly what you need with adding an configuration option - I’m open to hearing & maybe adding that.
but, prefer using a cached m/validator
instead of m/validate
if possible, it’s much faster.
Yeah you're right, it's not trivial. I'm trying to get my head around the essence of what makes it so difficult, there is some kind of circularity in the problem, it seems.