This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-05-16
Channels
- # announcements (22)
- # beginners (4)
- # biff (4)
- # cider (5)
- # clerk (3)
- # clojure (28)
- # clojure-chennai (1)
- # clojure-europe (23)
- # clojure-gamedev (7)
- # clojure-korea (5)
- # clojure-madison (3)
- # clojure-my (1)
- # clojure-nl (1)
- # clojure-norway (49)
- # clojure-sweden (7)
- # clojure-uk (4)
- # clojuredesign-podcast (14)
- # clojurescript (10)
- # clr (5)
- # cursive (4)
- # datascript (17)
- # datomic (2)
- # events (1)
- # garden (1)
- # introduce-yourself (2)
- # jobs-discuss (14)
- # lsp (23)
- # malli (14)
- # missionary (9)
- # off-topic (109)
- # overtone (7)
- # polylith (5)
- # releases (5)
- # shadow-cljs (7)
- # sql (13)
- # testing (30)
- # xtdb (10)
- # yamlscript (44)
Q: How can we create a schema for Tuples (see https://docs.datomic.com/schema/schema-reference.html#composite-tuples) taking into account :db/valueType
set to :db.type/tuple
should imply either Composite Tuple
, Heterogeneous tuple
or Homogeneus tuple
. Is there a syntax in malli to support this kind of linkage?
Hope the answer is not multi
schema with hand written enumeration of every combination 😂
;; implications of keys is not very well supported. can hack it together with :and+:map+:map-of.
[:multi {:dispatch :db/valueType}
[:db.type/tuple [:map [:db/tupleType {:optional true} ...]
[:db/tupleAttrs {:optional true} ...]
[:db/tupleTypes {:optional true} ...]]]]
This is directly supported in https://github.com/metosin/malli/pull/1025:
[:multi {:dispatch :db/valueType}
[:db.type/tuple [:map {:xor [:db/tupleType :db/tupleAttrs :db/tupleTypes]}
[:db/tupleType {:optional true} ...]
[:db/tupleAttrs {:optional true} ...]
[:db/tupleTypes {:optional true} ...]]]]
;; this is kind of hack I'm alluding to
[:multi {:dispatch :db/valueType}
[:db.type/tuple [:and
[:map [:db/tupleType {:optional true} ...]
[:db/tupleAttrs {:optional true} ...]
[:db/tupleTypes {:optional true} ...]]
[:fn #(= 1 (count (filter #{:db/tupleType :db/tupleAttrs :db/tupleTypes} (keys %))))]]
I wonder if I can directly add :dispatch
as a keyset constraint like:
[:map {:dispatch [:db/valueType
[:db.type/tuple [:xor :db/tupleType :db/tupleAttrs :db/tupleTypes]]]
[:db.type/string ...]
[:db.type/ref ...]}
[:db/tupleType {:optional true} ...]
[:db/tupleAttrs {:optional true} ...]
[:db/tupleTypes {:optional true} ...]]
actually that works really well https://github.com/metosin/malli/pull/1025/commits/4f56c61d23138f1512adc3f27c37b9d65d2bdf7d
Hm, what about chaining many :fn in :and? I think it might work really well to add schema contraints? What is your opinion?
IMO it only works well for validation. it might make generators fail, and you need to add custom error messages. but it's your only choice atm (e.g., the "hack" schema I posted 3 messages ago).
Thank you @U055XFK8V! I will patiently wait till this use case is more widely supported upstream!
Is there/I can't find a way to apply a custom generator to a whole schema... ie. I have property A which I want to generator with wither a 1
or a 2
and then have property B generate with an X
or a Y
based on that generated A property.
(defn custom-generator
[]
(gen/let [prop-a (gen/elements [1 2])]
{:A prop-a
:B (if (= 1 prop-a) "X" "Y")}))
(def my-schema
[:map
{:gen custom-generator}
[:A [:enum 1 2]]
[:B [:enum "X" "Y"]]])
(mg/generate my-schema)
This doesn't work, I will get something like {:A 1 :B "Y"}
unfortunately I don't think it can be a fn? atm https://github.com/metosin/malli/blob/9f5b8095e1bc72b04ce8c006ae8951b73a0afcf9/src/malli/generator.cljc#L501-L505
just had to use it as a function huh.. Thanks! I got what I needed
some related discussion here https://github.com/metosin/malli/pull/1043