This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-07-27
Channels
- # babashka (35)
- # beginners (85)
- # cider (14)
- # clojure (115)
- # clojure-europe (15)
- # clojure-norway (2)
- # clojure-portugal (9)
- # clojure-uk (3)
- # clojurescript (28)
- # conjure (35)
- # data-science (10)
- # datomic (4)
- # graalvm (28)
- # holy-lambda (7)
- # hyperfiddle (2)
- # jobs (2)
- # joker (4)
- # malli (9)
- # meander (6)
- # nbb (6)
- # off-topic (23)
- # pathom (3)
- # remote-jobs (1)
- # shadow-cljs (158)
- # sql (15)
- # tools-deps (35)
- # vim (5)
Is there a way to represent this as a map?
{id: string?
size1: my-schema1
size2: my-schema1
size3: my-schema1}
Where I know that id
will be present all the time and size1
and others are dynamic.
i.e. they can exist or they can have completely different names.
I could use map-of
for the second part but I would like to use map
for the id
part.Interesting question @U028ART884X
Instead I hardcoded the keys. It was simpler than think and solve for truly any key 😄
(let [{:keys [additionalProperties]} form
req-fields (->> required (mapv keyword) (into #{}))
all-fields (concat props req-fields)]
(if-not additionalProperties
(make-map props req-fields)
[:and
[:fn (fn [m]
(->>
(apply dissoc m all-fields)
(m/validate (m/schema [:map-of 'string? 'string?]))))]
(make-map props req-fields)]))
that's code i use to transform a json schema map to act something like what you described
[:and
[:fn (fn [m]
(->>
(dissoc m :optional :required)
(m/validate (m/schema [:map-of keyword? string?]))))]
[:map
[:required [:enum :a :b]]
[:optional {:optional true} [:enum :a :b]]
]]
ends up making something like that