This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-12-22
Channels
- # adventofcode (78)
- # announcements (12)
- # babashka (2)
- # beginners (116)
- # calva (20)
- # cider (17)
- # clj-kondo (15)
- # cljs-dev (51)
- # clojure (32)
- # clojure-android (1)
- # clojure-dev (4)
- # clojure-europe (91)
- # clojure-gamedev (1)
- # clojure-italy (2)
- # clojure-nl (1)
- # clojure-spec (12)
- # clojure-taiwan (1)
- # clojure-uk (10)
- # clojurescript (9)
- # conjure (3)
- # cryogen (4)
- # cursive (4)
- # data-science (1)
- # datomic (5)
- # depstar (5)
- # fulcro (39)
- # google-cloud (2)
- # kaocha (2)
- # malli (7)
- # off-topic (3)
- # pathom (3)
- # pedestal (5)
- # re-frame (19)
- # rewrite-clj (54)
- # ring (3)
- # shadow-cljs (12)
- # spacemacs (12)
- # specter (3)
- # tools-deps (63)
Hi, Is it possible to write a schema for this vector:
[{:type :work
:value ""}
{:type :personal
:value ""}]
the vector should only contain those 2 items, where the type is kind of static, the value can change.
i thought about using this schema:
[:vector [:map [:type keyword?] [:value string?]]]
but it’s not enough for my use case, because i want when i inspect the schema to know how many items i can have in the vector, and their types (the :type
)
is this possible?
Thank you for the great lib 🙏@U6AE62UCT I can think of two ways:
(def data-schema [:map
[:type [:enum :work :personal]]
[:value string?]])
for the maps and then
(def schema [:vector {:min 2 :max 2} data-schema])
or maybe
(def schema [:tuple data-schema data-schema])
for putting together the schema for the vectorI was thinking about something like
[:vector
[:map [:type :work] [:value string?]]
[:map [:type :personal] [:value string?]]]
but it didn’t work
Thank you @U6JS7B99S i’ll probably go with your first suggestionif the :type
defines the shape of the data, you can try :multi
:
(require '[malli.generator :as mg])
(mg/sample
[:vector {:min 2, :max 2}
[:multi {:dispatch :type}
[:work [:map [:type [:= :work]] [:value string?]]]
[:personal [:map [:type [:= :personal]] [:value string?]]]]])
(mg/sample
[:tuple {:registry
{::element
[:multi {:dispatch :type}
[:work [:map [:type [:= :work]] [:value string?]]]
[:personal [:map [:type [:= :personal]] [:value string?]]]]}}
::element ::element])
both emit results like:
([{:type :personal, :value ""} {:type :work, :value ""}]
[{:type :work, :value "l"} {:type :personal, :value ""}]
[{:type :personal, :value "z1"} {:type :personal, :value ""}]
[{:type :personal, :value "ZG"} {:type :personal, :value "N"}]
[{:type :personal, :value "qQ6"} {:type :work, :value "A8h"}]
[{:type :personal, :value "2588"} {:type :personal, :value "Djv12"}]
[{:type :work, :value ""} {:type :personal, :value "c"}]
[{:type :work, :value "U2uHMI"} {:type :personal, :value "376ihr"}]
[{:type :personal, :value "n6Ct4d"} {:type :personal, :value "V09xL8"}]
[{:type :work, :value "9"} {:type :personal, :value ""}])
if the first map is always work and second personal, :tuple
for the win:
[:tuple
[:map [:type :work] [:value string?]]
[:map [:type :personal] [:value string?]]]
if the :type
defines the shape of the data, you can try :multi
:
(require '[malli.generator :as mg])
(mg/sample
[:vector {:min 2, :max 2}
[:multi {:dispatch :type}
[:work [:map [:type [:= :work]] [:value string?]]]
[:personal [:map [:type [:= :personal]] [:value string?]]]]])
(mg/sample
[:tuple {:registry
{::element
[:multi {:dispatch :type}
[:work [:map [:type [:= :work]] [:value string?]]]
[:personal [:map [:type [:= :personal]] [:value string?]]]]}}
::element ::element])